HttpGet和HttpPost
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 鏂囦欢涓婁紶鎴愬姛!
HttpGet和HttpPost的更多相关文章
- 转-浅谈HTTP-GET 、 HTTP-POST 和SOAP
HTTP-GET 和 HTTP-POST HTTP-GET和HTTP-POST是标准协议,他们使用HTTP(超文本传输协议)谓词(谓词是指条件表达式的求值返回真或假的过程.)对参数金星编码并将参数作为 ...
- java最简单的方式实现httpget和httppost请求
java实现httpget和httppost请求的方式多种多样,个人总结了一种最简单的方式,仅仅需几行代码,就能够完美的实现. 此处须要用到两个jar包,httpclient-4.3.1.jar.ht ...
- HTTPClient模块的HttpGet和HttpPost
HttpClient常用HttpGet和HttpPost这两个类,分别对应Get方式和Post方式. 无论是使用HttpGet,还是使用HttpPost,都必须通过如下3步来访问HTTP资源. 1.创 ...
- Android中使用HttpGet和HttpPost访问HTTP资源
需求:用户登录(name:用户名,pwd:密码) (一)HttpGet :doGet()方法//doGet():将参数的键值对附加在url后面来传递 public String getResultFo ...
- HttpGet和HttpPost的区别
HttpGet和HttpPost的区别总结就是下面这样: Get一般用于从服务器取数据,而且不改变原来的内容: Post一般用于向服务器传递数据,这需要改变服务器的内容. 从安全性上考虑,Get的安全 ...
- Java实现HttpGet和HttpPost请求
maven引入JSON处理jar <dependency> <groupId>com.alibaba</groupId> <artifactId>fas ...
- Taurus.MVC WebAPI 入门开发教程5:控制器安全校验属性【HttpGet、HttpPost】【Ack】【Token】【MicroService】。
系列目录 1.Taurus.MVC WebAPI 入门开发教程1:框架下载环境配置与运行. 2.Taurus.MVC WebAPI 入门开发教程2:添加控制器输出Hello World. 3.Tau ...
- .net在Controller里的方法添加[HttpGet]和[HttpPost]
前端用post过来,Controller就要用[HttpPost],前端用get,Controller就要用[HttpGet],或者不管前端用什么,Controller都不加这些. 前端用post $ ...
- .net mvc 一个Action的 HttpGet 和 HttpPost
http://www.cnblogs.com/freeliver54/p/3747836.html 本文转自:http://stackoverflow.com/questions/11767911/m ...
随机推荐
- TensorFlow 安装详解
摘要: 原创出处:www.bysocket.com 泥瓦匠BYSocket 希望转载,保留摘要,谢谢! 『不要把手段当成目标 — <一个瑜伽行者的自传>』 本文提纲 1. 机器学习 2 ...
- 老李推荐:第14章7节《MonkeyRunner源码剖析》 HierarchyViewer实现原理-装备ViewServer-获取版本号 2
代码先是发送”LIST”命令到ViewServer列出所有的打开的窗口,然后把每个窗口都保存起来.342行起按照源码的注释解析就是说:从协议版本3以后开始加入了窗口自动更新的功能,但是在此之前,如果用 ...
- 接口测试培训:HTTP协议基础
接口测试培训:HTTP协议基础 引言 HTTP是一个属于应用层的面向对象的协议,由于其简捷.快速的方式,适用于分布式超媒体信息系统.它于1990年提出,经过几年的使用与发展,得到不断地完善和扩展. ...
- word-wrap: break-word;与word-break: break-all;文本自动换行
word-break:break-all和word-wrap:break-word都是能使其容器如DIV的内容自动换行它们的区别就在于:1,word-break:break-all 例如div宽200 ...
- echarts雷达图点击事件
最近看见别人问的问题,点击雷达图的拐点,获取点击数据的问题,直接上代码. echarts配置问题:https://www.douban.com/note/509404582/ <!doctype ...
- Android Studio 安装后首次启动的 Config path ...... is invalid 问题(转)
原文链接:http://m.blog.csdn.net/blog/hnust_xiehonghao/46127775 1. 问题描述: 安装好Android Studio后,启动时弹出如下信息: Co ...
- 初次接触 Android Studio FAQ
1. 2. 3. 4. 6. 7. Tools -> Adnroid -> enable ADB integration 8.官方模拟器太慢,还是自己下个第三方模拟器吧. http:/ ...
- CSS元素垂直居中方法总结
坚持,坚持,坚持... 以上为自我鼓励,哈哈... ------------------------------------------------- 相信没有真正是尝试过的人应该都和我一样,觉得居中 ...
- LinkBar选中项字体颜色
通过控制disabledColor样式来控制,选中项字体的颜色.
- DFB系列 之 SetCooperativeLevel协作级别
1. 函数原型解析 函数声明 function SetCooperativeLevel(hWnd: HWND; dwFlags: DWORD): HResult; stdcall; 设置指定的IDir ...