1. 程式人生 > >Http GET和Http Post傳送方式

Http GET和Http Post傳送方式

POST方式

傳入List<NameValuePair> params,CloseableHttpClienthttpClient

if(httpClient==null)httpClient=HttpClients.createDefault();

HttpPosthttpPost=new HttpPost(url);

httpPost.setEntity(new UrlEncodedFormEntity(params,"UTF-8"));

if(Headers!=null) httpPost.setHeaders(Headers);

CloseableHttpResponse response

=httpClient.execute(httpPost);

GET方式

if (params!= null&& !params.isEmpty()){

               url += "?" + EntityUtils.toString(new UrlEncodedFormEntity(params, "UTF-8"));

           }

         System.out.println("---------url---------"+url);

           HttpGet httpGet = new HttpGet(url);

           CloseableHttpResponse response

= httpClient.execute(httpGet);