您的位置:首页 > 理论基础 > 计算机网络

HttpClient——Post请求

2016-04-30 10:45 561 查看
//为以下拼接请求地址
public String path="";
public String key="";
public String pno="";
public String ps="";
try {
// 创建客户端

HttpClient client = new DefaultHttpClient();

// 创建post对象

HttpPost post = new HttpPost(path);

ArrayList<BasicNameValuePair> list = new ArrayList<BasicNameValuePair>();
list.add(new BasicNameValuePair("key", key));
list.add(new BasicNameValuePair("pno", pno));
list.add(new BasicNameValuePair("ps", ps));

HttpEntity entity = new UrlEncodedFormEntity(list);

// 发送内容
post.setEntity(entity);
// 发送请求
HttpResponse response = client.execute(post);
//得到请求码
int statusCode = response.getStatusLine().getStatusCode();

if (statusCode == 200)
{

// 得到实体内容
InputStream inputStream = response.getEntity()
.getContent();

int len;
byte[] b = new byte[1024];
ByteArrayOutputStream arrayOutputStream = new ByteArrayOutputStream();
while ((len = inputStream.read(b)) != -1)
{
arrayOutputStream.write(b, 0, len);
}

String strJson = arrayOutputStream.toString();
//handler向主线程发送信息,更新数据
handler.obtainMessage(SUCCESS, strJson).sendToTarget();

}
} catch (Exception e) {
e.printStackTrace();
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: