http post
2016-06-08 14:50
465 查看
使用HTTP POST 与HTTP GET类似,只是增加了创建包含NameValuePair对象实例的列表,使用UrlEncodedFormEntity包装列表.
Java代码 收藏代码
public String executeHttpPost() throws Exception {
BufferedReader in = null;
try {
// 定义HttpClient
HttpClient client = new DefaultHttpClient();
// 实例化HTTP方法
HttpPost request = new HttpPost("****");
// 创建名/值组列表
List<NameValuePair> parameters = new ArrayList<NameValuePair>();
parameters.add(new BasicNameValuePair("***", "***"));
parameters.add(new BasicNameValuePair("***", "***"));
// 创建UrlEncodedFormEntity对象
UrlEncodedFormEntity formEntiry = new UrlEncodedFormEntity(
parameters);
request.setEntity(formEntiry);
// 执行请求
HttpResponse response = client.execute(request);
in = new BufferedReader(new InputStreamReader(response.getEntity()
.getContent()));
StringBuffer sb = new StringBuffer("");
String line = "";
String NL = System.getProperty("line.separator");
while ((line = in.readLine()) != null) {
sb.append(line + NL);
}
in.close();
String result = sb.toString();
return result;
} finally {
if (in != null) {
try {
in.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
//////////////////////////////////////////////////////////////////////////////////////////////
public void register(View v){
final String name = etname.getText().toString();
final String pwd = etpwd.getText().toString();
final String real = etrealname.getText().toString();
final String email = etmail.getText().toString();
new Thread(){
public void run() {
try{
HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost(HOST+"regist.do");
post.setHeader("Content-Type","application/x-www-form-urlencoded");
List<NameValuePair> parameters = new ArrayList<NameValuePair>();
parameters.add(new BasicNameValuePair("loginname", name));
parameters.add(new BasicNameValuePair("password", pwd));
parameters.add(new BasicNameValuePair("realname", real));
parameters.add(new BasicNameValuePair("email", email));
HttpEntity entity = new UrlEncodedFormEntity(parameters );
post.setEntity(entity );
HttpResponse resp = client.execute(post);
String result = EntityUtils.toString(resp.getEntity());
Log.d("TAG", "注册结果---->"+result);
Message.obtain(handler,100,result).sendToTarget();
}catch(Exception e){
e.printStackTrace();
}
};
}.start();
}
相关文章推荐
- HTTP协议详解
- HTML——HTTP
- HTTP状态码大全
- 网络LED矩阵显示器
- 理解HTTP幂等性
- linux网络编程常用头文件总结
- java后台调用HttpURLConnection类模拟浏览器请求实例
- apache服务,或者说httpd服务,如何启动,如何开机启动。
- Apache服务器主配置文件httpd.conf详解2
- Apache主配置文件httpd.conf 详解
- ubuntu下,django结合网络爬虫环境的搭建
- apache/httpd安装
- Apache服务以及httpd.conf配置详解
- linux上安装apache以及httpd.conf基本配置
- 解决WebClient或HttpWebRequest首次连接缓慢问题
- jboss配置https
- iOS网络-06-监听Iphone的网络状态
- iOS网络-05-AFNetwoking原理及常用操作
- iOS网络-04-大文件下载
- iOS网络-03-NSURLSession与NSURLSessionTask