Java Apache HttpClient post 获取提交参数并获取网页

基于 Apache HttpClient 获取提交参数并获取网页

/*
 * 提供 url,param,charset
 * 返回字符串内容
 * 
 *  // 设置访问的Web站点
    // String path = "http://192.168.89.1/login";
    // 设置Http请求参数
    Map<String, String> params = new HashMap<String, String>();
    params.put("username", strUsername);
    params.put("password", strPassword);

    HttpClientPost httpClientPost = new HttpClientPost(strURL, params, "utf-8");
    String result = httpClientPost.getHttpResultString();

    // 把返回的接口输出
    System.out.println(result);

 * 
 */

package test;

import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;

public class HttpClientPost
{

  public HttpClientPost()
  {

  }

  // 返回結果
  String strHttpResult = null;

  /**
   * 发送Http请求到Web站点
   * 
   * @param path
   *          Web站点请求地址
   * @param map
   *          Http请求参数
   * @param encode
   *          编码格式
   * @return string, Web站点响应的字符串
   * 
   */
  public HttpClientPost(final String strURL, Map<String, String> map, final String encode)
  {
    strHttpResult = null;

    HttpClient httpclient = HttpClients.createDefault();
    HttpPost httppost = new HttpPost(strURL);

    List<NameValuePair> params = new ArrayList<NameValuePair>();

    if (map != null && !map.isEmpty())
    {
      for (Map.Entry<String, String> entry : map.entrySet())
      {
        params.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
      }
    }

    try
    {
      httppost.setEntity(new UrlEncodedFormEntity(params, encode));
      HttpResponse response = httpclient.execute(httppost);
      HttpEntity entity = response.getEntity();

      if (entity != null)
      {
        InputStream instream = entity.getContent();
        try
        {
          // stream to string
          String strResult = InputStream2String(instream);
          strHttpResult = strResult;
        }
        finally
        {
          instream.close();
        }
      }
    }
    catch (UnsupportedEncodingException e)
    {
      e.printStackTrace();
    }
    catch (ClientProtocolException e)
    {
      e.printStackTrace();
    }
    catch (IOException e)
    {
      e.printStackTrace();
    }
  }

  /**
   * get string from stream
   * 
   * @param is
   * @return
   * @throws IOException
   */
  private String InputStream2String(InputStream inputStream)
  {
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    int i = -1;
    String result = null;

    try
    {
      while ((i = inputStream.read()) != -1)
      {
        outputStream.write(i);
      }

      result = outputStream.toString();
      // result = new String(outputStream.toByteArray(), "gbk");

    }
    catch (IOException e)
    {
      e.printStackTrace();
    }

    return result;
  }

  /**
   * 返回 http 结果
   * 
   * @return
   */
  public String getHttpResultString()
  {
    return strHttpResult;
  }
}


测试代码:

  public void test1()
  {

    // 设置访问的Web站点
    String url = "http://192.168.89.1/login";
    // 设置Http请求参数
    Map<String, String> params = new HashMap<String, String>();
    params.put("username", "myname");
    params.put("password", "mypassword");

    HttpClientPost httpClientPost = new HttpClientPost(url, params, "utf-8");
    String result = httpClientPost.getHttpResultString();

    // 把返回的接口输出
    System.out.println(result);
  }


Q群讨论:236201801

更多信息

http://hc.apache.org/httpcomponents-client-ga/tutorial/html/fundamentals.html
http://hc.apache.org/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值