标签:android style blog http io ar os sp for
package com.httpclient;
/**
* http://api.xxx.com/api/android/
* commandid=guestpacklist&coursetype=6&grade=1039&mac=78%3Af5%3Afd%3A7b%3Ac2%3A9d&pagenumber=1&phpsessid=&rowcount=8&signed=0aa38c77108b23f4e1b588ac02338445&subject=1001&subversion=8014&userid=
*/
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import org.apache.http.HttpEntity;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
public class CustomHttpClient {
public void postForm() {
// 创建默认的httpClient实例.
CloseableHttpClient httpclient = HttpClients.createDefault();
// 创建httppost
HttpPost httppost = new HttpPost("http://api.xxx.com/api/android/");
// 创建参数队列
ArrayList<BasicNameValuePair> formparams = new ArrayList<BasicNameValuePair>();
formparams.add(new BasicNameValuePair("commandid", "guestpacklist"));
formparams.add(new BasicNameValuePair("coursetype", "6"));
formparams.add(new BasicNameValuePair("grade", "1039"));
formparams.add(new BasicNameValuePair("mac",
"78%3Af5%3Afd%3A7b%3Ac2%3A9d"));
formparams.add(new BasicNameValuePair("pagenumber", "1"));
formparams.add(new BasicNameValuePair("phpsessid", "6"));
formparams.add(new BasicNameValuePair("rowcount", "8"));
formparams.add(new BasicNameValuePair("signed",
"0aa38c77108b23f4e1b588ac02338445"));
formparams.add(new BasicNameValuePair("subject", "1001"));
formparams.add(new BasicNameValuePair("subversion", "8014"));
formparams.add(new BasicNameValuePair("userid", ""));
UrlEncodedFormEntity uefEntity;
try {
uefEntity = new UrlEncodedFormEntity(formparams, "UTF-8");
httppost.setEntity(uefEntity);
System.out.println("executing request " + httppost.getURI());
CloseableHttpResponse response = httpclient.execute(httppost);
try {
HttpEntity entity = response.getEntity();
if (entity != null) {
System.out
.println("--------------------------------------");
System.out.println("Response content: "
+ EntityUtils.toString(entity, "UTF-8"));
System.out
.println("--------------------------------------");
}
} finally {
response.close();
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e1) {
e1.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
// 关闭连接,释放资源
try {
httpclient.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
CustomHttpClient chttpclient=new CustomHttpClient();
chttpclient.postForm();
}
}
标签:android style blog http io ar os sp for
原文地址:http://www.cnblogs.com/stay-sober/p/4166115.html