package net.blogjava.mobile;

 import java.net.HttpURLConnection;
import java.util.ArrayList;
import java.util.List; import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils; import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView; public class Main extends Activity implements OnClickListener
{ @Override
public void onClick(View view)
{
String url = "http://169.254.103.188/querybooks/QueryServlet";
TextView tvQueryResult = (TextView) findViewById(R.id.tvQueryResult);
EditText etBookName = (EditText) findViewById(R.id.etBookName);
HttpResponse httpResponse = null;
try
{ switch (view.getId())
{
case R.id.btnGetQuery:
url += "?bookname=" + etBookName.getText().toString();
HttpGet httpGet = new HttpGet(url);
httpResponse = new DefaultHttpClient().execute(httpGet);
if (httpResponse.getStatusLine().getStatusCode() == 200)
{ String result = EntityUtils.toString(httpResponse.getEntity(), HTTP.UTF_8);
tvQueryResult.setText(result);
System.out.println(result);
}
break; case R.id.btnPostQuery:
HttpPost httpPost = new HttpPost(url);
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("bookname", etBookName
.getText().toString()));
httpPost.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8)); httpResponse = new DefaultHttpClient().execute(httpPost);
if (httpResponse.getStatusLine().getStatusCode() == 200)
{
String result = EntityUtils.toString(httpResponse
.getEntity());
tvQueryResult.setText(result.replaceAll("\r", ""));
}
break;
}
}
catch (Exception e)
{
tvQueryResult.setText(e.getMessage());
} } @Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button btnGetQuery = (Button) findViewById(R.id.btnGetQuery);
Button btnPostQuery = (Button) findViewById(R.id.btnPostQuery);
btnGetQuery.setOnClickListener(this);
btnPostQuery.setOnClickListener(this); }
}

上传文件时,可以使用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 鏂囦欢涓婁紶鎴愬姛!

最新文章

  1. Entity Framework Code First数据库连接
  2. 模仿mybatis,用jdk proxy实现接口
  3. OCJP(1Z0-851) 模拟题分析(七)--&gt;214
  4. Jenkins问题汇总
  5. bzoj 3293 数学整理
  6. MySQL 大表优化方案探讨
  7. php.ini的中文解释
  8. java zip工具类
  9. 四轴飞行器1.4 姿态解算和Matlab实时姿态显示
  10. iOS:由URL成员UIImage
  11. IOS 跳转到系统的url
  12. OOC,泛型,糟糕的设计。
  13. PAT (Advanced Level) 1010. Radix (25)
  14. Android为TV端助力 post带数据请求方式,传递的数据格式包括json和map
  15. ganache与metamask
  16. 一 Struts框架(下)
  17. Centos7 64位 -- glibc-2.29 编译升级方法(已成功)
  18. javascript this详解 面向对象
  19. maven一键部署linux的tomcat(wagon-maven-plugin)
  20. sharepoint 版本信息查看

热门文章

  1. 老李分享:android app自动化测试工具合集
  2. 性能测试分享:MYSQL死锁
  3. JSP带有属性的自定义标签
  4. Spring基础学习(二)&mdash;详解Bean(上)
  5. Hibernate基础学习(二)&mdash;Hibernate相关API介绍
  6. AFNetworking 内部详解
  7. less的基本操作
  8. CF IndiaHacks 2016 F Paper task 后缀数组
  9. vue 调用高德地图
  10. JS设计模式---缓存代理