欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

 更新時間:2014年02月26日 09:41:07   作者:  
這篇文章主要介紹了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();

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

  

  //創(chuàng)建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{
   //執(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)文章

  • Java中冒泡排序的原生實現(xiàn)方法(正序與逆序)

    Java中冒泡排序的原生實現(xiàn)方法(正序與逆序)

    這篇文章主要給大家介紹了關(guān)于Java中冒泡排序的原生實現(xiàn)方法(正序與逆序)的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2020-11-11
  • javax.validation自定義日期范圍校驗注解操作

    javax.validation自定義日期范圍校驗注解操作

    這篇文章主要介紹了javax.validation自定義日期范圍校驗注解操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-09-09
  • MyBatisPlus使用${ew.customSqlSegment}別名問題解決

    MyBatisPlus使用${ew.customSqlSegment}別名問題解決

    在使用MyBatisPlus進行連表查詢時,可能遇到因${ew.customSqlSegment}無法加別名的問題,本文就來介紹一下如何解決,感興趣的可以了解一下
    2024-10-10
  • java定時任務(wù)實現(xiàn)的4種方式小結(jié)

    java定時任務(wù)實現(xiàn)的4種方式小結(jié)

    這篇文章主要介紹了java定時任務(wù)實現(xiàn)的4種方式小結(jié),具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-09-09
  • SpringBoot中FailureAnalyzer的使用詳解

    SpringBoot中FailureAnalyzer的使用詳解

    這篇文章主要介紹了SpringBoot中FailureAnalyzer的使用詳解,FailureAnalyzer攔截啟動時異常,將異常轉(zhuǎn)換成更加易讀的信息并包裝成org.springframework.boot.diagnostics.FailureAnalysis對象,監(jiān)控應(yīng)用啟動過程,需要的朋友可以參考下
    2023-12-12
  • JAVA-NIO之Socket/ServerSocket Channel(詳解)

    JAVA-NIO之Socket/ServerSocket Channel(詳解)

    下面小編就為大家?guī)硪黄狫AVA-NIO之Socket/ServerSocket Channel(詳解)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-06-06
  • linux系統(tǒng)下查看jdk版本、路徑及配置環(huán)境變量

    linux系統(tǒng)下查看jdk版本、路徑及配置環(huán)境變量

    在Linux系統(tǒng)中,配置JDK環(huán)境變量是非常重要的,它可以讓你在終端中直接使用Java命令,這篇文章主要給大家介紹了關(guān)于linux系統(tǒng)下查看jdk版本、路徑及配置環(huán)境變量的相關(guān)資料,需要的朋友可以參考下
    2024-01-01
  • 散列表的原理與Java實現(xiàn)方法詳解

    散列表的原理與Java實現(xiàn)方法詳解

    這篇文章主要介紹了散列表的原理與Java實現(xiàn)方法,詳細分析了散列表的原理,并結(jié)合實例形式分析了java實現(xiàn)散列表相關(guān)操作技巧,需要的朋友可以參考下
    2019-09-09
  • java開發(fā)使用BigDecimal避坑四則

    java開發(fā)使用BigDecimal避坑四則

    這篇文章主要為大家介紹了java開發(fā)使用BigDecimal的避坑四則,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-07-07
  • 如何用Springboot Admin監(jiān)控你的微服務(wù)應(yīng)用

    如何用Springboot Admin監(jiān)控你的微服務(wù)應(yīng)用

    這篇文章主要介紹了如何用Springboot Admin監(jiān)控你的微服務(wù)應(yīng)用,幫助大家更好的理解和使用springboot框架,感興趣的朋友可以了解下。
    2021-01-01

最新評論