apache HttpClient学习(一)——简单的post请求
来源:互联网 发布:2017人工智能最新进展 编辑:程序博客网 时间:2024/06/16 13:33
apache HttpClient学习(一)——简单的post请求
因为在请求数据的时候,POST请求要比GET请求多很多。所以这里用一个简单的demo演示POST请求的使用。
1.首先有一个能够访问的地址和内容:
2.新建一个简单的java项目,在其中引入apache HttpClient的包
3.写主要代码:
import org.apache.http.*;import org.apache.http.client.HttpClient;import org.apache.http.client.entity.UrlEncodedFormEntity;import org.apache.http.client.methods.HttpPost;import org.apache.http.impl.client.DefaultHttpClient;import org.apache.http.message.BasicNameValuePair;import org.apache.http.util.EntityUtils;import java.io.IOException;import java.util.ArrayList;import java.util.List;public class Main { public static void main(String[] args) { System.out.println("Hello World!"); List<NameValuePair> formparams = new ArrayList<NameValuePair>(); formparams.add(new BasicNameValuePair("username", "xueyou")); formparams.add(new BasicNameValuePair("password", "123")); UrlEncodedFormEntity entity = new UrlEncodedFormEntity(formparams, Consts.UTF_8); HttpPost httppost = new HttpPost("http://192.168.0.102:8989/androidBackground/login/gologin"); httppost.setEntity(entity); System.out.println(httppost.getURI()); HttpClient httpClient = new DefaultHttpClient(); HttpResponse response = null; try { response = httpClient.execute(httppost); } catch (IOException e) { e.printStackTrace(); } System.out.println(response.getStatusLine()); System.out.println("===================="); System.out.println(response.getProtocolVersion()); System.out.println(response.getStatusLine().getStatusCode()); System.out.println(response.getStatusLine().getReasonPhrase()); System.out.println(response.getStatusLine().toString()); System.out.println("===================="); Header[] headers = response.getAllHeaders(); for (int i = 0; i < headers.length; i++) { Header header = headers[i]; System.out.println(header); } System.out.println("===================="); HttpEntity httpEntity = response.getEntity(); if(httpEntity != null){ try { System.out.println(EntityUtils.toString(httpEntity)); } catch (IOException e) { e.printStackTrace(); } } }}
4.运行结果:
0 0
- apache HttpClient学习(一)——简单的post请求
- apache httpclient新版本的get/post请求
- HttpClient——Post请求
- HttpClient发送Post请求(一)
- HttpClient--HttpClient的Post请求
- java apache commons HttpClient发送get和post请求的学习整理
- java apache commons HttpClient发送get和post请求的学习整理
- java apache commons HttpClient发送get和post请求的学习整理
- java apache commons HttpClient发送get和post请求的学习整理
- java apache commons HttpClient发送get和post请求的学习整理
- java apache commons HttpClient发送get和post请求的学习整理
- java apache commons HttpClient发送get和post请求的学习整理
- java apache commons HttpClient发送get和post请求的学习整理
- java apache commons HttpClient发送get和post请求的学习整理
- java apache commons HttpClient发送get和post请求的学习整理
- java apache commons HttpClient发送get和post请求的学习整理
- java apache commons HttpClient发送get和post请求的学习整理
- HttpClient的post请求
- Python “SyntaxError: Non-ASCII character '\xe2' in file”
- ROS Learning-016 Arduino-For-ROS-001 搭建 Arduino 和 ROS 之间相连接的开发环境
- 自顶向下的语法分析(修改)
- hook Activity的mInstrumentation变量
- 18. Yii 2.0 依赖注入容器
- apache HttpClient学习(一)——简单的post请求
- 彻底理解js中this的指向
- linux下sftp
- HLS协议深入分析——时间线介绍(二)
- servlet路径访问问题
- Android 6.0 新功能及主要 API 变更
- class和ID的区别
- 122. Best Time to Buy and Sell Stock II**
- 使用Vitamio打造自己的Android万能播放器(1)——准备