httpclient的get和post

來源:互聯網
上載者:User

標籤:

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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.