doPost方法源码如下(reqUrl为post的目标,parameters为要post的表单,recvEncoding为编码,ht为header、包括cookie的内容)
调用以后,用抓包软件发现post到服务器的包分成两个,一个只带header的内容,一个是我发送的数据 public String doPost(String reqUrl, Map parameters, String recvEncoding,Hashtable ht) {
HttpURLConnection url_con = null;
String responseContent = null;
try {
StringBuffer params = new StringBuffer();
for (Iterator iter = parameters.entrySet().iterator(); iter
.hasNext();) {
Entry element = (Entry) iter.next();
params.append(element.getKey().toString());
params.append("=");
params.append(URLEncoder.encode(element.getValue().toString(),
recvEncoding));
System.out.println(URLEncoder.encode(element.getValue().toString(),
recvEncoding));
params.append("&");
}
if (params.length() > 0) {
params = params.deleteCharAt(params.length() - 1);
}
System.out.println(params);
byte[] b = params.toString().getBytes(); URL url = new URL(reqUrl);
url_con = (HttpURLConnection) url.openConnection(); url_con.setRequestMethod("POST");
url_con.setConnectTimeout(5000);
url_con.setReadTimeout(5000);
//设置header
if (ht!=null){
Enumeration enu = ht.keys();
while (enu.hasMoreElements()){
try{
String f = (String)enu.nextElement();
String v = (String)ht.get(f);
System.out.println(String.valueOf(f)+"="+String.valueOf(v));
if (v!=null)
url_con.setRequestProperty(f,v);
}catch(Exception e){}
}
ht.put("Content-Length", String.valueOf(b.length));
}
url_con.setDoOutput(true);
OutputStream httpOutputStream = url_con.getOutputStream();
httpOutputStream.write(b);
httpOutputStream.flush();
httpOutputStream.close();
InputStream in = url_con.getInputStream(); BufferedReader rd = new BufferedReader(new InputStreamReader(in,
recvEncoding));
String tempLine = rd.readLine();
StringBuffer tempStr = new StringBuffer();
String crlf = System.getProperty("line.separator");
while (tempLine != null) {
tempStr.append(tempLine);
tempStr.append(crlf);
tempLine = rd.readLine();
}
responseContent = tempStr.toString();
rd.close();
in.close();
} catch (IOException e) {
e.printStackTrace();
return "";
}catch(Exception e){
e.printStackTrace();
return "";
} finally {
if (url_con != null) {
url_con.disconnect();
}
}
return responseContent;
}
抓包结果如下:
调用以后,用抓包软件发现post到服务器的包分成两个,一个只带header的内容,一个是我发送的数据 public String doPost(String reqUrl, Map parameters, String recvEncoding,Hashtable ht) {
HttpURLConnection url_con = null;
String responseContent = null;
try {
StringBuffer params = new StringBuffer();
for (Iterator iter = parameters.entrySet().iterator(); iter
.hasNext();) {
Entry element = (Entry) iter.next();
params.append(element.getKey().toString());
params.append("=");
params.append(URLEncoder.encode(element.getValue().toString(),
recvEncoding));
System.out.println(URLEncoder.encode(element.getValue().toString(),
recvEncoding));
params.append("&");
}
if (params.length() > 0) {
params = params.deleteCharAt(params.length() - 1);
}
System.out.println(params);
byte[] b = params.toString().getBytes(); URL url = new URL(reqUrl);
url_con = (HttpURLConnection) url.openConnection(); url_con.setRequestMethod("POST");
url_con.setConnectTimeout(5000);
url_con.setReadTimeout(5000);
//设置header
if (ht!=null){
Enumeration enu = ht.keys();
while (enu.hasMoreElements()){
try{
String f = (String)enu.nextElement();
String v = (String)ht.get(f);
System.out.println(String.valueOf(f)+"="+String.valueOf(v));
if (v!=null)
url_con.setRequestProperty(f,v);
}catch(Exception e){}
}
ht.put("Content-Length", String.valueOf(b.length));
}
url_con.setDoOutput(true);
OutputStream httpOutputStream = url_con.getOutputStream();
httpOutputStream.write(b);
httpOutputStream.flush();
httpOutputStream.close();
InputStream in = url_con.getInputStream(); BufferedReader rd = new BufferedReader(new InputStreamReader(in,
recvEncoding));
String tempLine = rd.readLine();
StringBuffer tempStr = new StringBuffer();
String crlf = System.getProperty("line.separator");
while (tempLine != null) {
tempStr.append(tempLine);
tempStr.append(crlf);
tempLine = rd.readLine();
}
responseContent = tempStr.toString();
rd.close();
in.close();
} catch (IOException e) {
e.printStackTrace();
return "";
}catch(Exception e){
e.printStackTrace();
return "";
} finally {
if (url_con != null) {
url_con.disconnect();
}
}
return responseContent;
}
抓包结果如下:
解决方案 »
- 【100分】求教。图像识别方面的技术
- jdk1.6相对于jdk1.5关于多线程这块,有哪些新的特性,哪些新的api?
- javaEE新手不要用jdk1.6,建议用1.5或1.4
- 问一个图形用户界面的一个基本问题
- Hibernate中如何取类中的前100条数据
- 关于多线程中sleep()的理解
- 想提高图片的显示速度..请大家帮帮忙!
- 学校关于科技立项--征求项目!!
- 运行服务器端出现java.net.BindException: Address in use: bind 这是什么错误?
- java的前途如何,这么慢的运行速度
- 求《Java参考大全(J2SE5 Edition)》书上源码下载地址
- 这行代码什么意思
回4楼:
if (ht!=null){
Enumeration enu = ht.keys();
while (enu.hasMoreElements()){
try{
String f = (String)enu.nextElement();
String v = (String)ht.get(f);
System.out.println(String.valueOf(f)+"="+String.valueOf(v));
if (v!=null)
url_con.setRequestProperty(f,v);
}catch(Exception e){}
}
ht.put("Content-Length", String.valueOf(b.length));
}
ht里面就是header的内容,已经用setRequestProperty放进去了开始以为是jdk1.5的问题,换了1.6也是一样
if (ht!=null){
Enumeration enu = ht.keys();
while (enu.hasMoreElements()){
try{
String f = (String)enu.nextElement();
String v = (String)ht.get(f);
System.out.println(String.valueOf(f)+"="+String.valueOf(v));
if (v!=null)
url_con.setRequestProperty(f,v);
}catch(Exception e){}
}
ht.put("Content-Length", String.valueOf(b.length));
} 先将ht中内容提交然后向ht中加内容,真是太牛了.
不过这个应该不影响发包吧。
感觉
url_con.setDoOutput(true);
OutputStream httpOutputStream = url_con.getOutputStream(); httpOutputStream.write(b);
httpOutputStream.flush();
httpOutputStream.close(); InputStream in = url_con.getInputStream();
这一段,url_con.getOutputStream()得到的OutputStream好像不会自动把setRequestProperty的内容发出去
提到url_con.setRequestMethod("POST"); 这一行上面试试