java使用httpclient模擬post請求和get請求示例
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import org.apache.commons.httpclient.DefaultHttpMethodRetryHandler;
import org.apache.commons.httpclient.Header;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.cookie.CookiePolicy;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.params.HttpMethodParams;
public class TestHttpClient {
public static void main(String[] args) {
// TODO Auto-generated method stub
//定義httpClient的實(shí)例
HttpClient httpclient = new HttpClient();
//創(chuàng)建get方法的實(shí)例
GetMethod getMethod = new GetMethod("http://jb51.net");
//使用系統(tǒng)提供的默認(rèn)恢復(fù)策略
// getMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler());
//創(chuàng)建post方法實(shí)例
PostMethod postMethod = new UTF8PostMethod("http://jb51.net");
//
// //填入各個表單域的值
// NameValuePair[] data = {new NameValuePair("user_name", "user_name"),new NameValuePair("password","password")};
//
// //將表單的值放入到post方法中
// postMethod.setRequestBody(data);
//
// postMethod.getParams().setParameter(
// "http.protocol.cookie-policy",CookiePolicy.BROWSER_COMPATIBILITY);
// postMethod.setRequestHeader("Referer", "http://jb51.net");
try{
//執(zhí)行GET方法
// int statusCode = httpclient.executeMethod(getMethod);
//執(zhí)行post方法
int statusCode = httpclient.executeMethod(postMethod);
if(statusCode == HttpStatus.SC_MOVED_TEMPORARILY){
Header locationHeader = postMethod.getResponseHeader("Location");
String location = null;
if(locationHeader != null){
location = locationHeader.getValue();
}
postMethod = new PostMethod(location);
postMethod.setRequestHeader("Referer", "http://jb51.net/login");
NameValuePair[] data1 = {new NameValuePair("user_name", "user_name"),new NameValuePair("password","password")};
postMethod.setRequestBody(data1);
postMethod.getParams().setParameter(
"http.protocol.cookie-policy",CookiePolicy.BROWSER_COMPATIBILITY);
int statusCode1 = httpclient.executeMethod(postMethod);
if(statusCode1 != HttpStatus.SC_OK){
System.out.println("Method is wrong " + postMethod.getStatusLine());
}
}
if(statusCode != HttpStatus.SC_OK){
System.out.println("Method is wrong " + postMethod.getStatusLine());
}
InputStream responseBody = postMethod.getResponseBodyAsStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(responseBody,"utf-8"));
String line = reader.readLine();
while(line != null){
System.out.println(new String(line.getBytes()));
line = reader.readLine();
}
}
catch (HttpException e) {
// TODO: handle exception
System.out.println("Please check your provided http address!");
e.printStackTrace();
}catch (IOException e) {
// TODO: handle exception
System.out.println("the line is wrong!");
e.printStackTrace();
}finally{
getMethod.releaseConnection();//釋放鏈接
postMethod.releaseConnection();
}
}
//Inner class for UTF-8 support
public static class UTF8PostMethod extends PostMethod{
public UTF8PostMethod(String url){
super(url);
}
@Override
public String getRequestCharSet() {
//return super.getRequestCharSet();
return "UTF-8";
}
}
}
相關(guān)文章
解決異常:Invalid?keystore?format,springboot配置ssl證書格式不合法問題
這篇文章主要介紹了解決異常:Invalid?keystore?format,springboot配置ssl證書格式不合法問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-03-03
Java設(shè)計(jì)模式之中介模式(Mediator模式)介紹
這篇文章主要介紹了Java設(shè)計(jì)模式之中介模式(Mediator模式)介紹,本文講解了為何使用Mediator模式、如何使用中介模式等內(nèi)容,需要的朋友可以參考下2015-03-03
使用Apache POI在Java中實(shí)現(xiàn)Excel單元格的合并
在日常工作中,Excel是一個不可或缺的工具,尤其是在處理大量數(shù)據(jù)時,本文將介紹如何使用 Apache POI 庫在 Java 中實(shí)現(xiàn) Excel 單元格的合并,需要的可以了解下2025-03-03
Spring Boot jpa Service層代碼實(shí)例
這篇文章主要介紹了Spring Boot jpa Service層代碼實(shí)例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2019-10-10
mybatisplus報(bào)Invalid bound statement (not found)錯誤的解決方法
搭建項(xiàng)目時使用了mybatisplus,項(xiàng)目能夠正常啟動,但在調(diào)用mapper方法查詢數(shù)據(jù)庫時報(bào)Invalid bound statement (not found)錯誤。本文給大家分享解決方案,感興趣的朋友跟隨小編一起看看吧2020-08-08
SWT(JFace) 圖片瀏覽器 實(shí)現(xiàn)代碼
SWT(JFace)小制作:圖片瀏覽器2009-06-06

