最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

 更新時間:2014年02月26日 09:41:07   作者:  
這篇文章主要介紹了java使用httpclient模擬post請求和get請求示例,需要的朋友可以參考下

復(fù)制代碼 代碼如下:

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)提供的默認(rèn)恢復(fù)策略
//  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)文章

  • 解決異常:Invalid?keystore?format,springboot配置ssl證書格式不合法問題

    解決異常:Invalid?keystore?format,springboot配置ssl證書格式不合法問題

    這篇文章主要介紹了解決異常:Invalid?keystore?format,springboot配置ssl證書格式不合法問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-03-03
  • Java設(shè)計模式之中介模式(Mediator模式)介紹

    Java設(shè)計模式之中介模式(Mediator模式)介紹

    這篇文章主要介紹了Java設(shè)計模式之中介模式(Mediator模式)介紹,本文講解了為何使用Mediator模式、如何使用中介模式等內(nèi)容,需要的朋友可以參考下
    2015-03-03
  • 使用Apache POI在Java中實現(xiàn)Excel單元格的合并

    使用Apache POI在Java中實現(xiàn)Excel單元格的合并

    在日常工作中,Excel是一個不可或缺的工具,尤其是在處理大量數(shù)據(jù)時,本文將介紹如何使用 Apache POI 庫在 Java 中實現(xiàn) Excel 單元格的合并,需要的可以了解下
    2025-03-03
  • Java進(jìn)階教程之異常處理

    Java進(jìn)階教程之異常處理

    這篇文章主要介紹了Java進(jìn)階教程之異常處理,本文講解了JAVA的異常處理機制、異常的類型、拋出異常、自定義異常等內(nèi)容,需要的朋友可以參考下
    2014-09-09
  • Java page cache回寫機制案例詳解

    Java page cache回寫機制案例詳解

    這篇文章主要介紹了Java page cache回寫機制案例詳解,本篇文章通過簡要的案例,講解了該項技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下
    2021-09-09
  • Spring Boot jpa Service層代碼實例

    Spring Boot jpa Service層代碼實例

    這篇文章主要介紹了Spring Boot jpa Service層代碼實例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2019-10-10
  • mybatisplus報Invalid bound statement (not found)錯誤的解決方法

    mybatisplus報Invalid bound statement (not found)錯誤的解決方法

    搭建項目時使用了mybatisplus,項目能夠正常啟動,但在調(diào)用mapper方法查詢數(shù)據(jù)庫時報Invalid bound statement (not found)錯誤。本文給大家分享解決方案,感興趣的朋友跟隨小編一起看看吧
    2020-08-08
  • Seata?AT模式啟動過程圖文示例詳解

    Seata?AT模式啟動過程圖文示例詳解

    這篇文章主要為大家介紹了Seata?AT模式啟動過程圖文示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-09-09
  • JavaWeb文件上傳與下載功能解析

    JavaWeb文件上傳與下載功能解析

    這篇文章主要為大家詳細(xì)介紹了JavaWeb文件上傳與下載功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2016-06-06
  • SWT(JFace) 圖片瀏覽器 實現(xiàn)代碼

    SWT(JFace) 圖片瀏覽器 實現(xiàn)代碼

    SWT(JFace)小制作:圖片瀏覽器
    2009-06-06

最新評論

新津县| 万山特区| 高淳县| 高邮市| 远安县| 黑河市| 汶上县| 伽师县| 厦门市| 合江县| 黄龙县| 苍山县| 德阳市| 诏安县| 老河口市| 留坝县| 裕民县| 随州市| 元阳县| 花莲市| 石景山区| 阿拉尔市| 福海县| 三门县| 固镇县| 绿春县| 岑溪市| 巴塘县| 开远市| 辽中县| 天峻县| 堆龙德庆县| 石景山区| 武定县| 西平县| 茂名市| 镇康县| 华宁县| 嘉善县| 左云县| 昌平区|