java 中,可以使用socket 发送POST请求,范例:

 
Socket s=new Socket("127.0.0.1",8083);
        PrintWriter pw=new PrintWriter(new OutputStreamWriter(s.getOutputStream()));
        String url="pass//FindServlet";
        pw.println("POST /"+url+" HTTP/1.1");
        pw.println("Host:localhost");
        pw.println("Accept-Language: zh-cn,zh;q=0.5");
        pw.println("Accept-Encoding: gzip,deflate");
        pw.println("Accept-Charset: gb2312,utf-8;q=0.7,*;q=0.7");
        pw.println("Connection: keep-alive");
        pw.println("Cookie: ASPSESSIONIDCSAATTCD=DOMMILABJOPANJPNNAKAMCPK");
        pw.println("Content-Type: application/x-www-form-urlencoded");
        pw.println("Content-Length9");
        pw.println();
        
        pw.println("title=MSN");
        pw.println();
        pw.flush();
        BufferedReader br=new BufferedReader(new InputStreamReader(s.getInputStream()));
        String content=FileUtils.getFullContent(br);
        System.out.println(content);
        FileUtils.writeToFile("./abc.html", content);
        br.close();
        pw.close();
        s.close();
 
注意:Content-Length后面一定要有一个换行,Content-Length指定的长度一定要与后面的参数一致。
最佳实践:
 Socket s=new Socket("127.0.0.1",8083);
        PrintWriter pw=new PrintWriter(new OutputStreamWriter(s.getOutputStream()));
        String url="pass//FindServlet";
        pw.println("POST /"+url+" HTTP/1.1");
        pw.println("Host:localhost");
        pw.println("Content-Type: application/x-www-form-urlencoded");
        String params="title=MSN";
        int leng=params.length();
        pw.println("Content-Length: "+leng);
        pw.println();//千万不能少
        pw.println(params);
        pw.println();
        pw.flush();
        BufferedReader br=new BufferedReader(new InputStreamReader(s.getInputStream()));
        String content=FileUtils.getFullContent(br);
        System.out.println(content);
        FileUtils.writeToFile("./abc.html", content);
        br.close();
        pw.close();
        s.close();

工具类:
 
  1. package com.http.util; 
  2.  
  3. import java.io.BufferedReader; 
  4. import java.io.IOException; 
  5. import java.io.InputStreamReader; 
  6. import java.io.OutputStreamWriter; 
  7. import java.io.PrintWriter; 
  8. import java.net.Socket; 
  9. import java.net.UnknownHostException; 
  10.  
  11. import com.http.bean.HttpRequestBean; 
  12. import com.io.hw.util.FileUtils; 
  13.  
  14. public class HttpSocketUtil 
  15.     private static String httpPOSTSocket(String ip, String realm, int port, String url, String params) 
  16.             throws UnknownHostException, IOException 
  17.     { 
  18.         Socket s = new Socket(ip, port); 
  19.         PrintWriter pw = new PrintWriter(new OutputStreamWriter(s.getOutputStream())); 
  20.         if (url.startsWith("/")) 
  21.         { 
  22.             url = url.substring(1); 
  23.         } 
  24.         int index2=url.indexOf("?"); 
  25.         if(index2>0){ 
  26.             url=url.substring(0, index2); 
  27.         } 
  28.         pw.println("POST /" + url + " HTTP/1.1"); 
  29.         pw.println("Host:" + realm); 
  30.         pw.println("Accept-Language: zh-cn,zh;q=0.5"); 
  31.         pw.println("Accept-Encoding: gzip,deflate"); 
  32.         pw.println("Accept-Charset: gb2312,utf-8;q=0.7,*;q=0.7"); 
  33.         pw.println("Connection: keep-alive"); 
  34.         pw.println("Cookie: ASPSESSIONIDCSAATTCD=DOMMILABJOPANJPNNAKAMCPK"); 
  35.         pw.println("Content-Type: application/x-www-form-urlencoded"); 
  36.         int leng = params.length(); 
  37.         pw.println("Content-Length: " + leng); 
  38.         pw.println(); 
  39.         pw.println(params); 
  40.         pw.println(); 
  41.         pw.flush(); 
  42.         BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream())); 
  43.         String content = FileUtils.getFullContent(br); 
  44.         br.close(); 
  45.         pw.close(); 
  46.         s.close(); 
  47.         return content; 
  48.     } 
  49.  
  50.     public static String httpSocket(HttpRequestBean httpBean) throws UnknownHostException, IOException 
  51.     { 
  52.         if (httpBean.isPOST()) 
  53.         { 
  54.             System.out.println("POST"); 
  55.             return httpPOSTSocket(httpBean); 
  56.         } 
  57.         else 
  58.         { 
  59.             System.out.println("GET..."); 
  60.             return httpGETSocket(httpBean); 
  61.         } 
  62.     } 
  63.  
  64.     private static String httpGETSocket(String ip, String realm, int port, String url) throws UnknownHostException, 
  65.             IOException 
  66.     { 
  67.         Socket s = new Socket(ip, port); 
  68.         PrintWriter pw = new PrintWriter(new OutputStreamWriter(s.getOutputStream())); 
  69.         if (url.startsWith("/")) 
  70.         { 
  71.             url = url.substring(1); 
  72.         } 
  73.         pw.println("GET /" + url + " HTTP/1.1"); 
  74.         pw.println("Host:" + realm); 
  75.         pw.println("Accept-Language: zh-cn,zh;q=0.5"); 
  76.         pw.println("Accept-Encoding: gzip,deflate"); 
  77.         pw.println("Accept-Charset: gb2312,utf-8;q=0.7,*;q=0.7"); 
  78.         pw.println("Connection: keep-alive"); 
  79.         //        pw.println("Cookie: ASPSESSIONIDCSAATTCD=DOMMILABJOPANJPNNAKAMCPK"); 
  80.         pw.println("Content-Type: text/http"); 
  81.         pw.println(); 
  82.         pw.flush(); 
  83.         BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream())); 
  84.         String content = FileUtils.getFullContent(br); 
  85.         br.close(); 
  86.         pw.close(); 
  87.         s.close(); 
  88.         return content; 
  89.     } 
  90.  
  91.     private static String httpPOSTSocket(HttpRequestBean httpBean) throws UnknownHostException, IOException 
  92.     { 
  93.         return httpPOSTSocket(httpBean.getIp(), httpBean.getRealm(), httpBean.getPort(), httpBean.getUrl(), httpBean 
  94.                 .getParams()); 
  95.     } 
  96.  
  97.     private static String httpGETSocket(HttpRequestBean httpBean) throws UnknownHostException, IOException 
  98.     { 
  99.         return httpGETSocket(httpBean.getIp(), httpBean.getRealm(), httpBean.getPort(), httpBean.getUrl()); 
  100.     } 
  101.  
  102.     public static void main(String[] args) 
  103.     { 
  104.         String content = null
  105.         HttpRequestBean httpBean = new HttpRequestBean(); 
  106.         String ip = "127.0.0.1"
  107.         httpBean.setIp(ip); 
  108.                 httpBean.setParams("title=MSN"); 
  109.         httpBean.setPort(8083); 
  110.         httpBean.setUrl("pass/FindServlet?title=MSN"); 
  111.         httpBean.setPOST(true); 
  112.         httpBean.setRealm(ip); 
  113.         try 
  114.         { 
  115.             content = httpSocket(httpBean); 
  116.         } 
  117.         catch (UnknownHostException e) 
  118.         { 
  119.             e.printStackTrace(); 
  120.         } 
  121.         catch (IOException e) 
  122.         { 
  123.             e.printStackTrace(); 
  124.         } 
  125.         System.out.println(content); 
  126.     } 
----------------------------------------------------------
bean:HttpRequestBean
package com.http.bean;
 
public class HttpRequestBean
{
    private String ip;
    private String realm;
    private int port;
    private String url;
    private String params;
    private boolean isPOST;
//getter 、setter方法略
}
-------------------------------------------------------------------------------------