一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|JAVA教程|ASP教程|

服務器之家 - 編程語言 - JAVA教程 - java使用httpclient模擬post請求和get請求示例

java使用httpclient模擬post請求和get請求示例

2019-11-10 15:15java教程網 JAVA教程

這篇文章主要介紹了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的實例
  HttpClient httpclient = new HttpClient();

  //創建get方法的實例
  GetMethod getMethod = new GetMethod("http://jb51.net");
  //使用系統提供的默認恢復策略
//  getMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler());

  

  //創建post方法實例
  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{
   //執行GET方法
//   int statusCode = httpclient.executeMethod(getMethod);

   //執行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";   
  }
 }

}

 

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 欧美午夜寂寞影院安卓列表 | avav男人天堂 | yy6080午夜国产免费福利 | 99国产在线视频 | 耽美双性| 天天欲色成人综合网站 | 久久精品久久久 | 天堂69亚洲精品中文字幕 | 亚洲欧美另类在线观看 | 桃乃木香奈ipx在线播放 | 久久精品123 | 久久精品成人免费网站 | 国产精品露脸国语对白手机视频 | 日本漫画工囗全彩内番e绅 日本伦理动漫在线观看 | 91午夜在线观看 | 二次元美女脱裤子让男人桶爽 | poren18美女| 国产香蕉一区二区在线网站 | 被巨大黑人的翻白眼 | www.97cao| 日韩精品一区二区三区中文字幕 | 精品久久久久久综合网 | 国产午夜精品一区二区三区 | 波多野 在线 | 日韩中文字幕在线不卡 | 日本强不卡在线观看 | 97伊人久久精品亚洲午夜 | 黄网在线观看免费网站台湾swag | 黑人开嫩苞| 亚洲无限| 99任你躁精品视频 | 本土自拍 | 美女逼逼喷水 | 亚洲一区二区精品视频 | 亚洲欧美视频在线播放 | 日本中文字幕在线观看视频 | 日韩去日本高清在线 | 免费看隐私男生网站 | 成年人免费观看的视频 | 91视频免费网站 | 9lporm自拍视频在线 |