码迷,mamicode.com
首页 > Web开发 > 详细

httpclient的get和post

时间:2016-08-03 10:33:48      阅读:194      评论:0      收藏:0      [点我收藏+]

标签:

pom.xml

        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpclient</artifactId>
            <version>4.4.1</version>
        </dependency>
        <dependency>
            <groupId>org.testng</groupId>
            <artifactId>testng</artifactId>
            <version>6.8.5</version>
        </dependency>
        <dependency>
            <groupId>org.json</groupId>
            <artifactId>json</artifactId>
            <version>20090211</version>
        </dependency>

 

class代码
 
public class HttpClient {
    @Test
    public void test_temp() {
//        String url="http://med.ihbedu.com/gateway/web/home/door/findLessonPackage?_q_=1469948710642&skuId=2c91b6905493e1810154947b705a0063";
//        this.HttpGet(url);
        //测试平台西医注册用户
        String postUrl = "http://test.ihbedu.com:1457/gateway/web/home/registered/createUserAccount?_q_=1458452488928";
        try {
            String userNameOrPhone = "15988880018";
            JSONObject parsers = new JSONObject();
            parsers.put("phone", userNameOrPhone);
            parsers.put("password", "000000");
            parsers.put("picValidateCode", "c4kx");
            parsers.put("remoteValidateCode", "success");
            parsers.put("phoneValidateCode", "003023");
            parsers.put("yesIDo", true);
            JSONArray provinceArray = new JSONArray();
            JSONObject provinceInfoObject = new JSONObject();
            provinceInfoObject.put("id", "110000");
            provinceInfoObject.put("municipalities", true);
            provinceInfoObject.put("name", "北京市");
            provinceInfoObject.put("regionPath", "/110000");
            provinceArray.put(provinceInfoObject);
            parsers.put("province", provinceArray);
            parsers.put("schoolId", "122");
            JSONArray schoolArray = new JSONArray();
            JSONObject schoolInfoObject = new JSONObject();
            schoolInfoObject.put("areaPath", "/110000");
            schoolInfoObject.put("available", true);
            schoolInfoObject.put("id", "122");
            schoolInfoObject.put("name", "北京(其他)");
            schoolInfoObject.put("platformId", "2c9180e5520a5e70015214fb2849000a");
            schoolInfoObject.put("platformVersionId", "2c9180e5520a6063015214fc062d0006");
            schoolInfoObject.put("projectId", "-1");
            schoolInfoObject.put("subProjectId", "-1");
            schoolArray.put(schoolInfoObject);
            parsers.put("school", schoolArray);
            parsers.put("name", userNameOrPhone);
            parsers.put("sex", "1");
            parsers.put("registerSource", 1);
            parsers.put("registerType", 11);
            parsers.put("accountType", 1);
            this.httpPost(postUrl, parsers);
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    public void HttpGet(String url) {
        CloseableHttpClient httpClient = HttpClients.createDefault();//建立httpclient
        HttpGet httpGet = new HttpGet(url);//建立httpget
        System.out.println("get请求的地址:" + httpGet.getURI());
        try {
            CloseableHttpResponse response = httpClient.execute(httpGet);//执行get请求,并结果保存
            System.out.println("get请求返回的状态码:" + response.getStatusLine().getStatusCode());
            HttpEntity httpEntity = response.getEntity();//将保存的response转为实体
            try {

                if (httpEntity != null) {
                    {
                        System.out.println("get请求返回的response值:" + EntityUtils.toString(httpEntity));
                    }
                }
            } finally {
                EntityUtils.consume(httpEntity);//关闭实体
                response.close();//关闭response
            }

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                httpClient.close();//关闭httpclient
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public void httpPost(String url, JSONObject parser) {
        CloseableHttpClient httpClient = HttpClients.createDefault();
        HttpPost httpPost = new HttpPost(url);
        if (parser != null) {
            StringEntity stringEntity = new StringEntity(parser.toString(), "utf-8");//构造字符串数据
            stringEntity.setContentEncoding("utf-8");//设置内容编码格式
            stringEntity.setContentType("application/json");//设置内容类型
            httpPost.setEntity(stringEntity);//设置请求数据
        }
        try {
            CloseableHttpResponse response = httpClient.execute(httpPost);
            System.out.println("post请求返回的状态码:" + response.getStatusLine().getStatusCode());
            System.out.println("get请求返回的response值:" + EntityUtils.toString(response.getEntity()));

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                httpClient.close();//关闭httpclient
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

 

httpclient的get和post

标签:

原文地址:http://www.cnblogs.com/xxyBlogs/p/5731671.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!