2014年6月5日 星期四

[ Java 代碼範本 ] HttpClient4.x: HttpGet & Http Post

來源自 這裡 
Preface: 
The following examples do not necessary work out of the box as we do not provide the required backend for receiving the data. Think of the following as examples how to setup HttpClients. Download the HttpClient libraries from the Apache Website, you can download the "bin" package it includes all dependencies. 

Http Get: 
The following is an example an HTTP Get request via DefaultHttpClient
  1. HttpClient client = new DefaultHttpClient();  
  2. HttpGet request = new HttpGet("http://www.vogella.com");  
  3. HttpResponse response = client.execute(request);  
  4.   
  5. // Get the response  
  6. BufferedReader rd = new BufferedReader  
  7.   (new InputStreamReader(response.getEntity().getContent()));  
  8.       
  9. String line = "";  
  10. while ((line = rd.readLine()) != null) {  
  11.   textView.append(line);  
  12. }   
Http Post: 
The following will send a post request to the URL "http://vogellac2dm.appspot.com/register" and include a parameter "registrationid" which an random value. 
  1. package de.vogella.web.httpclient;  
  2.   
  3. import java.io.BufferedReader;  
  4. import java.io.IOException;  
  5. import java.io.InputStreamReader;  
  6. import java.util.ArrayList;  
  7. import java.util.List;  
  8.   
  9. import org.apache.http.HttpResponse;  
  10. import org.apache.http.NameValuePair;  
  11. import org.apache.http.client.HttpClient;  
  12. import org.apache.http.client.entity.UrlEncodedFormEntity;  
  13. import org.apache.http.client.methods.HttpPost;  
  14. import org.apache.http.impl.client.DefaultHttpClient;  
  15. import org.apache.http.message.BasicNameValuePair;  
  16.   
  17. public class SimpleHttpPut {   
  18.   public static void main(String[] args) {  
  19.     HttpClient client = new DefaultHttpClient();  
  20.     HttpPost post = new HttpPost("http://vogellac2dm.appspot.com/register");  
  21.     try {  
  22.       List nameValuePairs = new ArrayList(1);  
  23.       nameValuePairs.add(new BasicNameValuePair("registrationid",  
  24.           "123456789"));  
  25.       post.setEntity(new UrlEncodedFormEntity(nameValuePairs));  
  26.   
  27.       HttpResponse response = client.execute(post);  
  28.       BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));  
  29.       String line = "";  
  30.       while ((line = rd.readLine()) != null) {  
  31.         System.out.println(line);  
  32.       }  
  33.   
  34.     } catch (IOException e) {  
  35.       e.printStackTrace();  
  36.     }  
  37.   }  
  38. }   
The following request adds several parameters to the post request. 
  1. package de.vogella.web.httpclient;  
  2.   
  3. import java.io.BufferedReader;  
  4. import java.io.IOException;  
  5. import java.io.InputStreamReader;  
  6. import java.util.ArrayList;  
  7. import java.util.List;  
  8.   
  9. import org.apache.http.HttpResponse;  
  10. import org.apache.http.NameValuePair;  
  11. import org.apache.http.client.HttpClient;  
  12. import org.apache.http.client.entity.UrlEncodedFormEntity;  
  13. import org.apache.http.client.methods.HttpPost;  
  14. import org.apache.http.impl.client.DefaultHttpClient;  
  15. import org.apache.http.message.BasicNameValuePair;  
  16.   
  17. public class TestHttpClient {  
  18.   public static void main(String[] args) {  
  19.     HttpClient client = new DefaultHttpClient();  
  20.     HttpPost post = new HttpPost("https://www.google.com/accounts/ClientLogin");  
  21.   
  22.     try {  
  23.   
  24.       List nameValuePairs = new ArrayList(1);  
  25.       nameValuePairs.add(new BasicNameValuePair("Email""youremail"));  
  26.       nameValuePairs  
  27.           .add(new BasicNameValuePair("Passwd""yourpassword"));  
  28.       nameValuePairs.add(new BasicNameValuePair("accountType""GOOGLE"));  
  29.       nameValuePairs.add(new BasicNameValuePair("source",  
  30.           "Google-cURL-Example"));  
  31.       nameValuePairs.add(new BasicNameValuePair("service""ac2dm"));  
  32.   
  33.       post.setEntity(new UrlEncodedFormEntity(nameValuePairs));  
  34.       HttpResponse response = client.execute(post);  
  35.       BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));  
  36.   
  37.       String line = "";  
  38.       while ((line = rd.readLine()) != null) {  
  39.         System.out.println(line);  
  40.         if (line.startsWith("Auth=")) {  
  41.           String key = line.substring(5);  
  42.           // do something with the key  
  43.         }  
  44.   
  45.       }  
  46.     } catch (IOException e) {  
  47.       e.printStackTrace();  
  48.     }  
  49.   }  
  50. }   
Supplement: 
符碼記憶 - Apache HttpClient 4.x 使用 GET, POST 範例

沒有留言:

張貼留言

[Git 常見問題] error: The following untracked working tree files would be overwritten by merge

  Source From  Here 方案1: // x -----删除忽略文件已经对 git 来说不识别的文件 // d -----删除未被添加到 git 的路径中的文件 // f -----强制运行 #   git clean -d -fx 方案2: 今天在服务器上  gi...