Note:
這裡使用了以下 Maven dependency,其中官方說明
commons-httpclient 官方已停止更新,
改由 org.apache.httpcomponents 繼續提供更新
(官方原文 :
The Commons HttpClient project is now end of life, and is no longer being
developed. It has been replaced by the Apache HttpComponents project in its
HttpClient and HttpCore modules, which offer better performance and more
flexibility.
)
<!--
https://mvnrepository.com/artifact/commons-httpclient/commons-httpclient
-->
<dependency>
<groupId>commons-httpclient</groupId>
<artifactId>commons-httpclient</artifactId>
<version>3.0.1</version>
</dependency>
<!--
https://mvnrepository.com/artifact/org.apache.httpcomponents/httpclient
-->
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.13</version>
</dependency>
HttpGet:
HttpGet的參數都是放在Url後面的,例如"http://XXX.XXX.XXX.XXX?param1=1¶m2=2",所以我們的目標就是創造出帶有參數的Url即可。
參考資料:How to add parameters to a HTTP GET request in Android?
- 最簡單的方式就是手動在HttpGet的Url後面加上要的參數資料(限文字),例如直接自行組合字串 (param 的值可能需要做 url encode):
- 使用 org.apache.http.client.utils.URIBuilder 來幫助我們產生Uri:
String url = "http://XXX.XXX.XXX.XXX?" + "param1=" + param1 + "¶m2=" + param2;
URI uri = new URIBuilder("http://XXX.XXX.XXX.XXX:8080/path1/path2/xxx.jsp") .addParameter("param1", param1) .addParameter("param2", param2) .addParameters(listOfParameters) //如果參數以NamerValuePair放在 //List<NameValuePair>中的話可以 //使用addParameters() .build();以上也可也拆成多個方法變成如下的樣子:
URI uri = new URIBuilder() .setScheme("http") .setHost("XXX.XXX.XXX.XXX") .setPort(8080) .setPath("/path1/path2/xxx.jsp") .addParameter("param1", param1) .addParameter("param2", param2) .addParameters(listOfParameters) .build();因為 addParameter() 已經會對輸入值做 url encode ,所以不用再對資料做一次 url encode
HttpPost的參數都是放在Entity中的,所以我們只要用List<NameValuePair>的型式把資料放到HttpPost的Entity中就行了。
參考資料:How to add parameters to a HTTP GET request in Android?
String url = "http://XXX.XXX.XXX.XXX:8080/path1/path2/xxx.jsp"; //建立HttpClient //DefaultHttpClient httpClient = new DefaultHttpClient(); //舊方法,現已 Deprecated CloseableHttpClient httpClient = HttpClients.createDefault(); //建立HttpPost HttpPost httpPost = new HttpPost( url ) ; //建立要送的資料參數 ArrayList<BasicNameValuePair> params = new ArrayList<BasicNameValuePair>(); params.add(new BasicNameValuePair("param1" , param1)) ; params.add(new BasicNameValuePair("param2" , param2)) ; //將資料參數放到HttpPost的Entity裡面,並指定編碼 httpPost.setEntity(new UrlEncodedFormEntity(params, StandardCharsets.UTF_8)); //Execute連線發送訊息並取得回應 HttpResponse response = httpClient.execute(httpPost); System.out.println(EntityUtils.toString(response.getEntity(), "UTF-8"));或是使用PostMethod
final HttpClient client = new HttpClient(); final PostMethod postMethod = new PostMethod("http://XXX.XXX.XXX.XXX:8080/path1/path2/xxx.jsp"); postMethod.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET,"utf-8"); postMethod.addParameter("param1", param1); postMethod.addParameter("param2", param2); //如果想將文字資料直接放到HttpPost的Entity裡面,可以這樣 //postMethod.setRequestEntity(new StringRequestEntity("", ContentType.APPLICATION_JSON.toString(), StandardCharsets.UTF_8.name())); int statusCode = client.executeMethod(postMethod); System.out.println(postMethod.getStatusLine()); System.out.println(postMethod.getResponseBodyAsString());
沒有留言 :
張貼留言