欢迎来到代码驿站!

JAVA代码

当前位置:首页 > 软件编程 > JAVA代码

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

时间:2021-06-03 09:11:19|栏目:JAVA代码|点击:

复制代码 代码如下:

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

}

上一篇:SpringMVC开发restful API之用户查询代码详解

栏    目:JAVA代码

下一篇:IDEA下创建SpringBoot+MyBatis+MySql项目实现动态登录与注册功能

本文标题:java使用httpclient模拟post请求和get请求示例

本文地址:http://www.codeinn.net/misctech/134503.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有