httpClient使用,1 发送post请求 2 发送get请求取得接口中的数据

来源:互联网 发布:lol动态桌面软件 编辑:程序博客网 时间:2024/06/16 06:07

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONObject;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;

import com.alibaba.fastjson.JSON;

/**
 * @Title:TestHttpClient
 * @Author Tony
 * @Date: 2014年6月21日 下午3:29:37
 * @Description: httpClient使用,1 发送post请求 2 发送get请求
 *
 */
public class TestHttpClient {

    /**
     * @Title: methodPost
     * @Description: httpclient方法中post提交数据的使用
     * @param @return
     * @param @throws Exception
     * @return String
     * @throws
     */
    public static String methodPost() throws Exception {
        DefaultHttpClient httpclient = new DefaultHttpClient();
        // // 代理的设置
        // HttpHost proxy = new HttpHost("10.60.8.20", 8080);
        // httpclient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY,
        // proxy);

        // 目标地址
        HttpPost httppost = new HttpPost(
                "http://192.168.6.27:8010/mobileservices/cart/payment_list.htm?");
        System.out.println("请求: " + httppost.getRequestLine());

        // post 参数 传递
        List<BasicNameValuePair> nvps = new ArrayList<BasicNameValuePair>();
        nvps.add(new BasicNameValuePair("pid", "7")); // 参数
        nvps.add(new BasicNameValuePair("v", "1")); // 参数
        nvps.add(new BasicNameValuePair("time", "1406968391")); // 参数
        nvps.add(new BasicNameValuePair("device_type", "android_tuan")); // 参数
        nvps.add(new BasicNameValuePair("cps_code", "jx")); // 参数
        nvps.add(new BasicNameValuePair("cps_id", "1")); // 参数
        nvps.add(new BasicNameValuePair("app_key",
                "f35486edbc52fa48981a28b49cc7dceb")); // 参数
        nvps.add(new BasicNameValuePair("user_id", "2069860")); // 参数
        nvps.add(new BasicNameValuePair("area_id", "2")); // 参数
        nvps.add(new BasicNameValuePair("is_cod", "1")); // 参数
        nvps.add(new BasicNameValuePair("sign",
                "45951e4ae7f0c1afb82ff03e3598b270")); // 参数

        httppost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8)); // 设置参数给Post

        // 执行
        HttpResponse response = httpclient.execute(httppost);
        HttpEntity entity = response.getEntity();
        System.out.println(response.getStatusLine());
        if (entity != null) {
            System.out.println("Response content length: "
                    + entity.getContentLength());
        }
        // 显示结果
        BufferedReader reader = new BufferedReader(new InputStreamReader(
                entity.getContent(), "UTF-8"));

        String line = null;
        while ((line = reader.readLine()) != null) {
            System.out.println(line);
        }
        if (entity != null) {
            entity.consumeContent();
        }
        return null;

    }

    /**
     * @Title: methodGet
     * @Description: 以get方法提交数的使用
     * @param @return
     * @param @throws Exception
     * @return String
     * @throws
     */
    public static String methodGet() throws Exception {
        DefaultHttpClient httpclient = new DefaultHttpClient();
        // // 代理的设置
        // HttpHost proxy = new HttpHost("10.60.8.20", 8080);
        // httpclient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY,
        // proxy);

        // 目标地址
        HttpPost httpGet = new HttpPost("http://localhost:8011/testServlet");

        // 构造最简单的字符串数据
        StringEntity reqEntity = new StringEntity("name=test&password=test");
        // 设置类型
        reqEntity.setContentType("application/x-www-form-urlencoded");
        // 设置请求的数据
        httpGet.setEntity(reqEntity);

        // 执行
        HttpResponse response = httpclient.execute(httpGet);
        HttpEntity entity = response.getEntity();
        System.out.println(response.getStatusLine());

        if (entity != null) {
            System.out.println("Response content length: "
                    + entity.getContentLength()); // 得到返回数据的长度
        }
        // 显示结果
        BufferedReader reader = new BufferedReader(new InputStreamReader(
                entity.getContent(), "UTF-8"));

        String line = null;
        while ((line = reader.readLine()) != null) {
            System.out.println(line);
        }
        if (entity != null) {
            entity.consumeContent();
        }
        return null;

    }

    /**
     * 模拟url访问 从特定的url中获取json
     *
     * @param urlStr
     * @param params
     * @return json object ,or null if failed 参数经过封装后传过来 ,提交为 post请求
     */
    private static JSONObject getJsonFromUrl(String urlStr,
            Map<String, String> params) {
        HttpClient httpClient = new DefaultHttpClient();
        HttpPost httpPost = new HttpPost(urlStr);
        JSONObject json = null;
        try {
            if (params != null) {
                Iterator<String> keys = params.keySet().iterator();
                List<NameValuePair> nvps = new ArrayList<NameValuePair>();
                while (keys.hasNext()) {
                    String key = keys.next();
                    nvps.add(new BasicNameValuePair(key, params.get(key)));
                }
                httpPost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));
            }
            HttpResponse response = httpClient.execute(httpPost);
            HttpEntity entity = response.getEntity();
            InputStream is = entity.getContent();
            byte[] bytes = new byte[256];
            StringBuffer sb = new StringBuffer();
            while (is.read(bytes) > 0) {
                sb.append(new String(bytes));
                bytes = new byte[256];
            }
            json = JSONObject.fromObject(sb.toString());
        } catch (Exception e) {
            e.printStackTrace();
        }

        return json;
    }

    /**
     * @Title: main
     * @Description: 测试类
     * @param @param args
     * @return void
     * @throws
     */
    public static void main(String[] args) {
        Map<String, String> params = new HashMap<String, String>();
        String strUrl = "http://192.168.6.27:8010/mobileservices/cart/payment_list.htm?pid=7&v=1&time=1406968391&device_type=android_tuan&cps_code=jx&cps_id=1&app_key=f35486edbc52fa48981a28b49cc7dceb&user_id=2069860&area_id=2&is_cod=1&sign=45951e4ae7f0c1afb82ff03e3598b270";
        try {
            //post获取json数据
            TestHttpClient.methodPost();
            //从url获取json数据
            JSONObject obj = getJsonFromUrl(strUrl, params);
            System.out.println(obj);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
0 0