HTTP请求的POST
1、自己要运行的话把paramValue换成用httpwatch抓到的request文本
2、PrintWriter是写文本文件、流文件的类
import java.io.BufferedReader;
import java.io.InputStreamReader;import java.io.PrintWriter;
import java.net.*;
public class JavaHTTPHandle {
public static void main(String[] args) {
// TODO Auto-generated method stub
JavaHTTPHandle jhh = new JavaHTTPHandle();
jhh.sendPost("https://passport.csdn.net/account/login;jsessionid=52C34ADF27D7F89D459507FE1EB11EAC.tomcat1?from=http://my.csdn.net/my/mycsdn", "username=admin&password=123456<=LT-21772-6OpT4r2IEleScWYxx3Q6X7Es52balz&execution=e1s1&_eventId=submit");
}
public void sendPost(String urlAddress,String paramValue) {
try {
//建立连接并发送请求
HttpURLConnection urlConnection = null;
URL url = new URL(urlAddress);
//强制转化
urlConnection = (HttpURLConnection) url.openConnection();
//超时时间
urlConnection.setConnectTimeout(5000);
//设置读的超时时间,5s还未获取到响应就放弃
urlConnection.setReadTimeout(5000);
//是否使用缓存
urlConnection.setUseCaches(false);
urlConnection.setDoOutput(true);
urlConnection.connect();
PrintWriter pw = new PrintWriter(urlConnection.getOutputStream());
pw.print(paramValue);
pw.flush();
//获取响应 urlConnection.getInputStream()获取缓存,从输入流中读
BufferedReader bf = new BufferedReader(new InputStreamReader(urlConnection.getInputStream(),"UTF-8"));
String line = bf.readLine();//读取一行
while (line != null) {
System.out.println(line);
line = bf.readLine();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}