HttpGet和HttpPost
1 package net.blogjava.mobile; 2 3 import java.net.HttpURLConnection; 4 import java.util.ArrayList; 5 import java.util.List; 6 7 import org.apache.http.HttpResponse; 8 import org.apache.http.NameValuePair; 9 import org.apache.http.client.entity.UrlEncodedFormEntity; 10 import org.apache.http.client.methods.HttpGet; 11 import org.apache.http.client.methods.HttpPost; 12 import org.apache.http.impl.client.DefaultHttpClient; 13 import org.apache.http.message.BasicNameValuePair; 14 import org.apache.http.protocol.HTTP; 15 import org.apache.http.util.EntityUtils; 16 17 import android.app.Activity; 18 import android.os.Bundle; 19 import android.view.View; 20 import android.view.View.OnClickListener; 21 import android.widget.Button; 22 import android.widget.EditText; 23 import android.widget.TextView; 24 25 public class Main extends Activity implements OnClickListener 26 { 27 28 @Override 29 public void onClick(View view) 30 { 31 String url = "http://169.254.103.188/querybooks/QueryServlet"; 32 TextView tvQueryResult = (TextView) findViewById(R.id.tvQueryResult); 33 EditText etBookName = (EditText) findViewById(R.id.etBookName); 34 HttpResponse httpResponse = null; 35 try 36 { 37 38 switch (view.getId()) 39 { 40 case R.id.btnGetQuery: 41 url += "?bookname=" + etBookName.getText().toString(); 42 HttpGet httpGet = new HttpGet(url); 43 httpResponse = new DefaultHttpClient().execute(httpGet); 44 if (httpResponse.getStatusLine().getStatusCode() == 200) 45 { 46 47 String result = EntityUtils.toString(httpResponse.getEntity(), HTTP.UTF_8); 48 tvQueryResult.setText(result); 49 System.out.println(result); 50 } 51 break; 52 53 case R.id.btnPostQuery: 54 HttpPost httpPost = new HttpPost(url); 55 List<NameValuePair> params = new ArrayList<NameValuePair>(); 56 params.add(new BasicNameValuePair("bookname", etBookName 57 .getText().toString())); 58 httpPost.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8)); 59 60 httpResponse = new DefaultHttpClient().execute(httpPost); 61 if (httpResponse.getStatusLine().getStatusCode() == 200) 62 { 63 String result = EntityUtils.toString(httpResponse 64 .getEntity()); 65 tvQueryResult.setText(result.replaceAll("\r", "")); 66 } 67 break; 68 } 69 } 70 catch (Exception e) 71 { 72 tvQueryResult.setText(e.getMessage()); 73 } 74 75 } 76 77 @Override 78 public void onCreate(Bundle savedInstanceState) 79 { 80 super.onCreate(savedInstanceState); 81 setContentView(R.layout.main); 82 Button btnGetQuery = (Button) findViewById(R.id.btnGetQuery); 83 Button btnPostQuery = (Button) findViewById(R.id.btnPostQuery); 84 btnGetQuery.setOnClickListener(this); 85 btnPostQuery.setOnClickListener(this); 86 87 } 88 }
上传文件时,可以使用HttpAnalyzer来捕获HTTP请求信息:上传文档新建文本文档 (3).txt过程:
Request Headers Value
(Request-Line) POST /upload/UploadServlet HTTP/1.1
Accept text/html, application/xhtml+xml, */*
Referer http://localhost/upload/upload.jsp
Accept-Language zh-CN
User-Agent Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; WOW64; Trident/6.0)
Content-Type multipart/form-data; boundary=---------------------------7dea05100618
Accept-Encoding gzip, deflate
Host localhost
Content-Length 239
DNT 1
Connection Keep-Alive
Cache-Control no-cache
Cookie JSESSIONID=C55149A1C7C5EEFE0A67D4DA2183E8BF
Response Headers Value
(Status-Line) HTTP/1.1 200 OK
Server Apache-Coyote/1.1
Content-Type text/html;charset=UTF-8
Content-Length 21
Date Sun, 14 Dec 2014 07:00:05 GMT
stream标签页信息:
744 bytes sent to :0
POST /upload/UploadServlet HTTP/1.1
Accept: text/html, application/xhtml+xml, */*
Referer: http://localhost/upload/upload.jsp
Accept-Language: zh-CN
User-Agent: Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; WOW64; Trident/6.0)
Content-Type: multipart/form-data; boundary=---------------------------7dea05100618
Accept-Encoding: gzip, deflate
Host: localhost
Content-Length: 239
DNT: 1
Connection: Keep-Alive
Cache-Control: no-cache
Cookie: JSESSIONID=C55149A1C7C5EEFE0A67D4DA2183E8BF
-----------------------------7dea05100618
Content-Disposition: form-data; name="file"; filename="C:\Users\Administrator\Desktop\鏂板缓鏂囨湰鏂囨。 (3).txt"
Content-Type: text/plain
123abc
-----------------------------7dea05100618--
163 bytes received by :0
HTTP/1.1 200 OK
Server: Apache-Coyote/1.1
Content-Type: text/html;charset=UTF-8
Content-Length: 21
Date: Sun, 14 Dec 2014 07:00:05 GMT
鏂囦欢涓婁紶鎴愬姛!