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-Length: 9");
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();
工具类:
- package com.http.util;
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.io.OutputStreamWriter;
- import java.io.PrintWriter;
- import java.net.Socket;
- import java.net.UnknownHostException;
- import com.http.bean.HttpRequestBean;
- import com.io.hw.util.FileUtils;
- public class HttpSocketUtil
- {
- private static String httpPOSTSocket(String ip, String realm, int port, String url, String params)
- throws UnknownHostException, IOException
- {
- Socket s = new Socket(ip, port);
- PrintWriter pw = new PrintWriter(new OutputStreamWriter(s.getOutputStream()));
- if (url.startsWith("/"))
- {
- url = url.substring(1);
- }
- int index2=url.indexOf("?");
- if(index2>0){
- url=url.substring(0, index2);
- }
- pw.println("POST /" + url + " HTTP/1.1");
- pw.println("Host:" + realm);
- 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");
- 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);
- br.close();
- pw.close();
- s.close();
- return content;
- }
- public static String httpSocket(HttpRequestBean httpBean) throws UnknownHostException, IOException
- {
- if (httpBean.isPOST())
- {
- System.out.println("POST");
- return httpPOSTSocket(httpBean);
- }
- else
- {
- System.out.println("GET...");
- return httpGETSocket(httpBean);
- }
- }
- private static String httpGETSocket(String ip, String realm, int port, String url) throws UnknownHostException,
- IOException
- {
- Socket s = new Socket(ip, port);
- PrintWriter pw = new PrintWriter(new OutputStreamWriter(s.getOutputStream()));
- if (url.startsWith("/"))
- {
- url = url.substring(1);
- }
- pw.println("GET /" + url + " HTTP/1.1");
- pw.println("Host:" + realm);
- 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: text/http");
- pw.println();
- pw.flush();
- BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream()));
- String content = FileUtils.getFullContent(br);
- br.close();
- pw.close();
- s.close();
- return content;
- }
- private static String httpPOSTSocket(HttpRequestBean httpBean) throws UnknownHostException, IOException
- {
- return httpPOSTSocket(httpBean.getIp(), httpBean.getRealm(), httpBean.getPort(), httpBean.getUrl(), httpBean
- .getParams());
- }
- private static String httpGETSocket(HttpRequestBean httpBean) throws UnknownHostException, IOException
- {
- return httpGETSocket(httpBean.getIp(), httpBean.getRealm(), httpBean.getPort(), httpBean.getUrl());
- }
- public static void main(String[] args)
- {
- String content = null;
- HttpRequestBean httpBean = new HttpRequestBean();
- String ip = "127.0.0.1";
- httpBean.setIp(ip);
- httpBean.setParams("title=MSN");
- httpBean.setPort(8083);
- httpBean.setUrl("pass/FindServlet?title=MSN");
- httpBean.setPOST(true);
- httpBean.setRealm(ip);
- try
- {
- content = httpSocket(httpBean);
- }
- catch (UnknownHostException e)
- {
- e.printStackTrace();
- }
- catch (IOException e)
- {
- e.printStackTrace();
- }
- System.out.println(content);
- }
- }
----------------------------------------------------------
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方法略
}
-------------------------------------------------------------------------------------