- 浏览: 222268 次
- 性别:
- 来自: 广州
文章分类
- 全部博客 (109)
- IOS (15)
- 设计模式 (7)
- XML (1)
- Android (31)
- 面试经 (1)
- J2EE (3)
- android md5 加密 (2)
- android imagebutton文字实现 (1)
- 反射机制 (2)
- 基础知识 (1)
- linux (3)
- java (4)
- java基础 (2)
- 文章 (1)
- myeclipse tomcat (1)
- Hadoop (1)
- ubuntu (2)
- redmine (1)
- python (4)
- jmeter (10)
- xamarin (1)
- selenium (9)
- nexus (1)
- appium (3)
- BDD (1)
- apache2 (1)
- zabbix (2)
- python,webdriver (1)
- ajax (1)
- jmeter,正则表达式,关联 (2)
- 性能测试工具 (1)
- Django (0)
- Intelij (1)
- RAP (0)
- 性能测试 (0)
最新评论
客户端代码示例:
/**
* HTTP请求
* @author kesenhoo
*
*/
public class HttpRequest
{
public static boolean sendXML(String path, String xml)throws Exception
{
byte[] data = xml.getBytes();
URL url = new URL(path);
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
conn.setRequestMethod("POST");
conn.setConnectTimeout(5 * 1000);
//如果通过post提交数据,必须设置允许对外输出数据
conn.setDoOutput(true);
conn.setRequestProperty("Content-Type", "text/xml; charset=UTF-8");
conn.setRequestProperty("Content-Length", String.valueOf(data.length));
OutputStream outStream = conn.getOutputStream();
outStream.write(data);
outStream.flush();
outStream.close();
if(conn.getResponseCode()==200)
{
return true;
}
return false;
}
/**
* 通过get方式提交参数给服务器
* @param path
* @param params
* @param enc
* @return
* @throws Exception
*/
public static boolean sendGetRequest(String path, Map<String, String> params, String enc) throws Exception
{
//构造如下形式的字符串,这里的字符串依情况不同
// ?method=save&title=435435435&timelength=89&
//使用StringBuilder对象
StringBuilder sb = new StringBuilder(path);
sb.append('?');
//迭代Map
for(Map.Entry<String, String> entry : params.entrySet())
{
sb.append(entry.getKey()).append('=')
.append(URLEncoder.encode(entry.getValue(), enc)).append('&');
}
sb.deleteCharAt(sb.length()-1);
//打开链接
URL url = new URL(sb.toString());
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
conn.setRequestMethod("GET");
conn.setConnectTimeout(5 * 1000);
//如果请求响应码是200,则表示成功
if(conn.getResponseCode()==200)
{
return true;
}
return false;
}
/**
* 通过Post方式提交参数给服务器
* @param path
* @param params
* @param enc
* @return
* @throws Exception
*/
public static boolean sendPostRequest(String path, Map<String, String> params, String enc) throws Exception
{
//需要构造的字符串形式如下:
// title=dsfdsf&timelength=23&method=save
StringBuilder sb = new StringBuilder();
//如果参数不为空
if(params!=null && !params.isEmpty())
{
for(Map.Entry<String, String> entry : params.entrySet())
{
//Post方式提交参数的话,不能省略内容类型与长度
sb.append(entry.getKey()).append('=')
.append(URLEncoder.encode(entry.getValue(), enc)).append('&');
}
sb.deleteCharAt(sb.length()-1);
}
//得到实体的二进制数据
byte[] entitydata = sb.toString().getBytes();
URL url = new URL(path);
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
conn.setRequestMethod("POST");
conn.setConnectTimeout(5 * 1000);
//如果通过post提交数据,必须设置允许对外输出数据
conn.setDoOutput(true);
//这里只设置内容类型与内容长度的头字段
//内容类型Content-Type: application/x-www-form-urlencoded
//内容长度Content-Length: 38
conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
conn.setRequestProperty("Content-Length", String.valueOf(entitydata.length));
OutputStream outStream = conn.getOutputStream();
//把实体数据写入是输出流
outStream.write(entitydata);
//内存中的数据刷入
outStream.flush();
outStream.close();
//如果请求响应码是200,则表示成功
if(conn.getResponseCode()==200)
{
return true;
}
return false;
}
/**
* 在遇上HTTPS安全模式或者操作cookie的时候使用HTTPclient会方便很多
* 使用HTTPClient(开源项目)向服务器提交参数
* @param path
* @param params
* @param enc
* @return
* @throws Exception
*/
public static boolean sendRequestFromHttpClient(String path, Map<String, String> params, String enc) throws Exception
{
//需要把参数放到NameValuePair
List<NameValuePair> paramPairs = new ArrayList<NameValuePair>();
if(params!=null && !params.isEmpty())
{
for(Map.Entry<String, String> entry : params.entrySet())
{
paramPairs.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
}
}
//对请求参数进行编码,得到实体数据
UrlEncodedFormEntity entitydata = new UrlEncodedFormEntity(paramPairs, enc);
//构造一个请求路径
HttpPost post = new HttpPost(path);
//设置请求实体
post.setEntity(entitydata);
//浏览器对象
DefaultHttpClient client = new DefaultHttpClient();
//执行post请求
HttpResponse response = client.execute(post);
//从状态行中获取状态码,判断响应码是否符合要求
if(response.getStatusLine().getStatusCode()==200)
{
return true;
}
return false;
}
}
/**
* HTTP请求
* @author kesenhoo
*
*/
public class HttpRequest
{
public static boolean sendXML(String path, String xml)throws Exception
{
byte[] data = xml.getBytes();
URL url = new URL(path);
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
conn.setRequestMethod("POST");
conn.setConnectTimeout(5 * 1000);
//如果通过post提交数据,必须设置允许对外输出数据
conn.setDoOutput(true);
conn.setRequestProperty("Content-Type", "text/xml; charset=UTF-8");
conn.setRequestProperty("Content-Length", String.valueOf(data.length));
OutputStream outStream = conn.getOutputStream();
outStream.write(data);
outStream.flush();
outStream.close();
if(conn.getResponseCode()==200)
{
return true;
}
return false;
}
/**
* 通过get方式提交参数给服务器
* @param path
* @param params
* @param enc
* @return
* @throws Exception
*/
public static boolean sendGetRequest(String path, Map<String, String> params, String enc) throws Exception
{
//构造如下形式的字符串,这里的字符串依情况不同
// ?method=save&title=435435435&timelength=89&
//使用StringBuilder对象
StringBuilder sb = new StringBuilder(path);
sb.append('?');
//迭代Map
for(Map.Entry<String, String> entry : params.entrySet())
{
sb.append(entry.getKey()).append('=')
.append(URLEncoder.encode(entry.getValue(), enc)).append('&');
}
sb.deleteCharAt(sb.length()-1);
//打开链接
URL url = new URL(sb.toString());
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
conn.setRequestMethod("GET");
conn.setConnectTimeout(5 * 1000);
//如果请求响应码是200,则表示成功
if(conn.getResponseCode()==200)
{
return true;
}
return false;
}
/**
* 通过Post方式提交参数给服务器
* @param path
* @param params
* @param enc
* @return
* @throws Exception
*/
public static boolean sendPostRequest(String path, Map<String, String> params, String enc) throws Exception
{
//需要构造的字符串形式如下:
// title=dsfdsf&timelength=23&method=save
StringBuilder sb = new StringBuilder();
//如果参数不为空
if(params!=null && !params.isEmpty())
{
for(Map.Entry<String, String> entry : params.entrySet())
{
//Post方式提交参数的话,不能省略内容类型与长度
sb.append(entry.getKey()).append('=')
.append(URLEncoder.encode(entry.getValue(), enc)).append('&');
}
sb.deleteCharAt(sb.length()-1);
}
//得到实体的二进制数据
byte[] entitydata = sb.toString().getBytes();
URL url = new URL(path);
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
conn.setRequestMethod("POST");
conn.setConnectTimeout(5 * 1000);
//如果通过post提交数据,必须设置允许对外输出数据
conn.setDoOutput(true);
//这里只设置内容类型与内容长度的头字段
//内容类型Content-Type: application/x-www-form-urlencoded
//内容长度Content-Length: 38
conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
conn.setRequestProperty("Content-Length", String.valueOf(entitydata.length));
OutputStream outStream = conn.getOutputStream();
//把实体数据写入是输出流
outStream.write(entitydata);
//内存中的数据刷入
outStream.flush();
outStream.close();
//如果请求响应码是200,则表示成功
if(conn.getResponseCode()==200)
{
return true;
}
return false;
}
/**
* 在遇上HTTPS安全模式或者操作cookie的时候使用HTTPclient会方便很多
* 使用HTTPClient(开源项目)向服务器提交参数
* @param path
* @param params
* @param enc
* @return
* @throws Exception
*/
public static boolean sendRequestFromHttpClient(String path, Map<String, String> params, String enc) throws Exception
{
//需要把参数放到NameValuePair
List<NameValuePair> paramPairs = new ArrayList<NameValuePair>();
if(params!=null && !params.isEmpty())
{
for(Map.Entry<String, String> entry : params.entrySet())
{
paramPairs.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
}
}
//对请求参数进行编码,得到实体数据
UrlEncodedFormEntity entitydata = new UrlEncodedFormEntity(paramPairs, enc);
//构造一个请求路径
HttpPost post = new HttpPost(path);
//设置请求实体
post.setEntity(entitydata);
//浏览器对象
DefaultHttpClient client = new DefaultHttpClient();
//执行post请求
HttpResponse response = client.execute(post);
//从状态行中获取状态码,判断响应码是否符合要求
if(response.getStatusLine().getStatusCode()==200)
{
return true;
}
return false;
}
}
发表评论
-
Starting emulator for AVD 'android' PANIC: Could not open: android
2013-05-21 13:29 1880我的电脑-->属性-->高级-->环境变量。 ... -
eclipse4.2版本下面安装ADT,安装已经完成了,但没有ADT的那个图标显示
2013-05-21 13:26 904如果安装过程没错,直接在Eclipse ->window ... -
Android 打包签名 从生成keystore到完成签名 -
2012-10-30 00:49 914首先,我们需要一个keystore,当然已经有了的话就不用这一 ... -
解决更新并使用最新ADT20不能创建android项目问题
2012-10-18 22:20 1007不知道谷歌又怎么了,每次更新ADT插件就会出现各种各样的问题, ... -
ORACLE分页查询SQL语法
2012-10-18 22:20 1174oracle数据库 --1:无ORDER BY ... -
Activity生命周期
2012-10-18 22:20 1110博客分类: Android 新的activit ... -
布局
2012-10-18 22:21 1053padding:描述控件里面的内容与控件的关机,内边距;有四个 ... -
常用控件:TextView EditView
2012-10-13 13:32 1137TextView 布局: Xml代 ... -
按钮控件
2012-10-13 13:32 1153监听器: 监听器 方法 内容 OnClickList ... -
菜单
2012-10-13 13:31 1075menu键触发 三种形式:普通的option menu;上下 ... -
HttpClient
2012-10-13 13:31 1091在Android开发中我们经常会用到网络连接功能与服务器进行数 ... -
Android 的一些提示框
2012-10-08 00:57 7651.在测试时,如何实现一个提示 可以使用 Toast.ma ... -
Intent的几种用法
2012-10-08 00:57 908下面列出几种Intent的用法 1. 启动一个新的Activ ... -
Android改变窗口标题栏的布局
2012-10-10 23:26 888一、 重点 一般应用的Title都是建立应用时在Androi ... -
android中如何自定义attributes
2012-10-10 23:26 954写程序中可能需要用到一些自定义的view控件,这样就需要增加一 ... -
android manifest.xml中元素含义
2012-10-08 00:56 797android:allowTaskReparenting 是 ... -
十二个android编程技巧
2012-10-10 23:26 9631.让一个图片透明: Java代码 1. Bitm ... -
Android Phone类分析
2012-10-10 23:26 1199AccelerometerListener:感应 ... -
android控件设置居中方式
2012-10-07 00:16 8423垂直居中 android:layout_centerVert ... -
android TextView属性大全
2012-10-10 23:28 944android:autoLink设置是否当 ...
相关推荐
Android Studio使用HttpClient请求数据(get请求)不包括post请求
小小测试项目包含了4个知识点,其中包含了http协议向服务器传输数据的post和get方法,包含了httpclient传输方法,httpclient上传文件到服务器的方法,
采用 HttpClient GET、POST 方式 提交数据到服务器
android通过POST和GET向服务器发送数据.包括使用普通HTTP协议发送和使用android内置的HttpClient进行发送。
Android客户端向服务器端请求数据的两种主要方式:HTTPURLConnection、HTTPClient;内含客户端与服务器端Demo(带有详细注释),原生方式解析json数据,希望大家多多批评指教!!
Android中通过GET和POST方式以及使用HttpClient框架通过网络通信提交参数给web应用案例!
利用httpclient-4.3.1.jar、httpcore-4.3.jar包,很简单的用java实现httpget和httppost请求。
分别演示了android中Http和HttpClient的Get和Post方法的使用,还有加载图片的演示
采用httpclient发送xml报文,httpServer接收报文。两个程序一看就明白可以直接调用执行不在多说。
Android-Httpclient_Get_Post
使用HttpURLConnection类提交数据到服务器端,使用HttpClient类到服务器端
MFC使用H偷偷跑Client的GET和POST方法 MFC使用H偷偷跑Client的GET和POST方法
HttpClient实现POST GET和文件下载 HttpClient实现POST GET和文件下载
一个简单的易学的 基于HttpClient 4.3发送psot及get请求,返回数据,适合初学者,适合初学者
利用c# httpclient ,post json数据到web api
HttpPost httpPost = new HttpPost(url); StringEntity stringEntity = new StringEntity(params, Charset.forName("UTF-8")); httpPost.setHeader("Content-type", "application/json"); httpPost.setEntity...
httpClient httpPost httpGet 所需jar包 httpClient httpPost httpGet 所需jar包
httpclient的用法,发送get请求和post请求,设置header
C++ 实现 HTTP HTTPS POST GET(包含curl版本和winhttp两种实现)。 玩过抓包,网络协议分析的朋友肯定都知道http https post get,web端和用户的交互主要是通过post get完成的。 我这里有两种实现: 1:libcurl实现的...
以httpclient方式提交数据(POST和GET),实现返回服务器数据。