国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Java > 正文

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

2019-11-26 15:39:56
字體:
來源:轉載
供稿:網友

復制代碼 代碼如下:

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://VeVB.COm");
  //使用系統提供的默認恢復策略
//  getMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler());

  

  //創建post方法實例
  PostMethod postMethod = new UTF8PostMethod("http://VeVB.COm");
//  
//  //填入各個表單域的值
//  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://VeVB.COm");
  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://VeVB.COm/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";  
  }
 }

}

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 巴塘县| 图片| 巴中市| 江山市| 株洲市| 江陵县| 襄汾县| 彩票| 南丹县| 深州市| 抚顺县| 清徐县| 驻马店市| 府谷县| 陕西省| 宝兴县| 和平县| 栾城县| 南丹县| 玉田县| 镇远县| 南靖县| 兰坪| 宜昌市| 那曲县| 金乡县| 蒙山县| 库伦旗| 万州区| 苏尼特左旗| 石首市| 望江县| 孝感市| 南丹县| 东至县| 衡南县| 福建省| 谷城县| 龙门县| 额敏县| 北安市|