欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

java使用httpclient模拟post请求和get请求示例

程序员文章站 2024-02-25 16:48:21
复制代码 代码如下:import java.io.bufferedreader;import java.io.ioexception;import java.io.inpu...

复制代码 代码如下:

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";  
  }
 }

}