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 ...
随机推荐
- c++内存优化:二级间接索引模式内存池
/********************************************************* 在一些不确定内存总占用量的情形下,频繁的使用new申请内存,再通过链表 进行索引似 ...
- laravel redis Error while reading line from the server.
代码运行一段时间后,会报下面的错误. [Predis\Connection\ConnectionException] Error while reading line from the server. ...
- JS滚动加载
var one = true;//设置一个全局变量 $(window).scroll(function () { var hight = document.body.scrollHeight - do ...
- Java与面向对象之随感(1)
大一下学期上完了c++课程,当时自我感觉很良好,认为对面向对象编程已经是身经百战了,但是上了院里HuangYu老师的Java课之后,才发现自己对于面向对象的编程风格的理解只在皮毛,着实惭愧不已. 假设 ...
- ssh相关命令
ssh命令 ssh命令是openssh套件中的客户端连接工具,可以给予ssh加密协议实现安全的远程登录服务器. 语法ssh(选项)(参数)选项 -1:强制使用ssh协议版本1: -2:强制使用ssh协 ...
- 事件驱动的简明讲解(python实现)
关键词:编程范式,事件驱动,回调函数,观察者模式 作者:码匠信龙 举个简单的例子: 有些人喜欢的某个公众号,然后去关注这个公众号,哪天这个公众号发布了篇新的文章,没多久订阅者就会在微信里收到这个公众号 ...
- HDU 5617 DP
沿着一条条斜线DP即可,dp[k][i][j]表示第k步,一端在第j列,另一端在第i列,构成回文的个数,沿着四个方向推下去即可. #include <iostream> #include ...
- HashMap 学习笔记
先摆上JDK1.8中HashMap的类注释:我翻译了一下 /** * Hash table based implementation of the <tt>Map</tt> i ...
- List<String> 和 ArrayList<String>的区别
最近对这两个问题比较懵逼,关于List和ArrayList.List<String> list = new ArrayList<String>(); 好了,先搞明白List 和 ...
- 用vuejs仿网易云音乐(实现听歌以及搜索功能)
前言 前端时间学了vue,一开始看了vue1.0,后来实在觉得技术总得实践,就直接上手vue2.0.然后花了将近一周时间做了一个网易云音乐的小项目.一开始觉得项目比较小,没必要用vuex所以就没有使用 ...