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

java使用httpclient模擬post請(qǐng)求和get請(qǐng)求示例

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

復(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的實(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");
//  
//  //填入各個(gè)表單域的值
//  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)文章

最新評(píng)論

望都县| 温泉县| 剑河县| 龙山县| 克什克腾旗| 陵川县| 衡南县| 万盛区| 重庆市| 宁武县| 安达市| 敦化市| 塔河县| 瓦房店市| 孟津县| 罗平县| 城口县| 阳东县| 许昌市| 呼玛县| 西贡区| 曲靖市| 苍山县| 廊坊市| 泸州市| 大丰市| 苗栗县| 晋宁县| 秦皇岛市| 五家渠市| 洪江市| 天峻县| 都江堰市| 阿拉善盟| 宜州市| 社会| 商水县| 宽甸| 雷波县| 抚松县| 金川县|