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;
}
        抓包结果如下:
         

解决方案 »

  1.   

    如何在发送的数据包中带上header呢?
      

  2.   

    回5楼:这是客户端发post的方法
    回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也是一样
      

  3.   

    牛人有牛人的逻辑:
    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中加内容,真是太牛了.
      

  4.   

    7楼教训的是
    不过这个应该不影响发包吧。
    感觉
    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的内容发出去
      

  5.   

    肯定会的,首先你要确认ht中有内容,另外将url_con.setDoOutput(true); 
    提到url_con.setRequestMethod("POST"); 这一行上面试试