httpclient_1

package com.httpclient;

/**
* http://api.xxx.com/api/android/
* commandid=guestpacklist&coursetype=6&grade=1039&mac=78%3Af5%3Afd%3A7b%3Ac2%3A9d&pagenumber=1&phpsessid=&rowcount=8&signed=0aa38c77108b23f4e1b588ac02338445&subject=1001&subversion=8014&userid=
*/
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;

import org.apache.http.HttpEntity;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

public class CustomHttpClient {
    public void postForm() {
        // 创建默认的httpClient实例.
        CloseableHttpClient httpclient = HttpClients.createDefault();
        // 创建httppost
        HttpPost httppost = new HttpPost("http://api.xxx.com/api/android/");
        // 创建参数队列
        ArrayList<BasicNameValuePair> formparams = new ArrayList<BasicNameValuePair>();
        formparams.add(new BasicNameValuePair("commandid", "guestpacklist"));
        formparams.add(new BasicNameValuePair("coursetype", "6"));
        formparams.add(new BasicNameValuePair("grade", "1039"));
        formparams.add(new BasicNameValuePair("mac",
                "78%3Af5%3Afd%3A7b%3Ac2%3A9d"));
        formparams.add(new BasicNameValuePair("pagenumber", "1"));
        formparams.add(new BasicNameValuePair("phpsessid", "6"));
        formparams.add(new BasicNameValuePair("rowcount", "8"));
        formparams.add(new BasicNameValuePair("signed",
                "0aa38c77108b23f4e1b588ac02338445"));
        formparams.add(new BasicNameValuePair("subject", "1001"));
        formparams.add(new BasicNameValuePair("subversion", "8014"));
        formparams.add(new BasicNameValuePair("userid", ""));
        UrlEncodedFormEntity uefEntity;
        try {
            uefEntity = new UrlEncodedFormEntity(formparams, "UTF-8");
            httppost.setEntity(uefEntity);
            System.out.println("executing request " + httppost.getURI());
            CloseableHttpResponse response = httpclient.execute(httppost);
            try {
                HttpEntity entity = response.getEntity();
                if (entity != null) {
                    System.out
                            .println("--------------------------------------");
                    System.out.println("Response content: "
                            + EntityUtils.toString(entity, "UTF-8"));
                    System.out
                            .println("--------------------------------------");
                }
            } finally {
                response.close();
            }
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e1) {
            e1.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            // 关闭连接,释放资源
            try {
                httpclient.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

public static void main(String[] args) {
        CustomHttpClient chttpclient=new CustomHttpClient();
        chttpclient.postForm();
    }
}

  • public void get() {
  • CloseableHttpClient httpclient = HttpClients.createDefault();
  • try {
  • // 创建httpget.
  • HttpGet httpget = new HttpGet("http://www.baidu.com/");
  • System.out.println("executing request " + httpget.getURI());
  • // 执行get请求.
  • CloseableHttpResponse response = httpclient.execute(httpget);
  • try {
  • // 获取响应实体
  • HttpEntity entity = response.getEntity();
  • System.out.println("--------------------------------------");
  • // 打印响应状态
  • System.out.println(response.getStatusLine());
  • if (entity != null) {
  • // 打印响应内容长度
  • System.out.println("Response content length: " + entity.getContentLength());
  • // 打印响应内容
  • System.out.println("Response content: " + EntityUtils.toString(entity));
  • }
  • System.out.println("------------------------------------");
  • } finally {
  • response.close();
  • }
  • } catch (ClientProtocolException e) {
  • e.printStackTrace();
  • } catch (ParseException e) {
  • e.printStackTrace();
  • } catch (IOException e) {
  • e.printStackTrace();
  • } finally {
  • // 关闭连接,释放资源
  • try {
  • httpclient.close();
  • } catch (IOException e) {
  • e.printStackTrace();
  • }
  • }
  • }
时间: 2024-11-05 15:56:49

httpclient_1的相关文章