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

HttpClient入门

2013-09-24 19:31 218 查看
这是get方式的情况:
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.params.HttpMethodParams;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;

public class TestHttpClient {
public static void main(String[] args) {
HttpClient httpclient = new DefaultHttpClient();
httpclient.getParams().setParameter(
HttpMethodParams.HTTP_CONTENT_CHARSET, "utf-8");
String http = "";
HttpGet httppost = new HttpGet(http);
HttpResponse response;
try {
response = httpclient.execute(httppost);
if (HttpStatus.SC_OK == response.getStatusLine().getStatusCode()) {
HttpEntity enties = response.getEntity();
if (enties != null) {
String str = EntityUtils.toString(enties);
}

if (enties != null) {
enties.consumeContent();
}
}

} catch (Exception e) {

}finally{
httpclient.getConnectionManager().shutdown();
}
}

}

POST的情况:
List<NameValuePair> formparams = new ArrayList<NameValuePair>();
formparams.add(new BasicNameValuePair("user_password", ""));
UrlEncodedFormEntity entity2 = new UrlEncodedFormEntity(formparams,
"UTF-8");
httppost.setEntity(entity2);

文件类型使用FileBody,字符串类型使用StringBody,其它的还有ByteArrayBody,InputStreamBody.




import java.util.ArrayList;
import java.util.List;

import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.params.HttpMethodParams;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
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;

public class TestHttpClient {
public static void main(String[] args) {
HttpClient httpclient = new DefaultHttpClient();
httpclient.getParams().setParameter(
HttpMethodParams.HTTP_CONTENT_CHARSET, "utf-8");
String http = "http://。。。。.json";
HttpPost httppost = new HttpPost(http);

HttpResponse response;
try {
List<NameValuePair> formparams = new ArrayList<NameValuePair>();
formparams.add(new BasicNameValuePair("email", "410463718@qq.com"));
formparams.add(new BasicNameValuePair("password", "123456"));
formparams.add(new BasicNameValuePair("appName", "ferjek"));
formparams.add(new BasicNameValuePair("url", "www.dkskeefl.com"));
UrlEncodedFormEntity entity2 = new UrlEncodedFormEntity(formparams,
"UTF-8");
httppost.setEntity(entity2);
response = httpclient.execute(httppost);
if (HttpStatus.SC_OK == response.getStatusLine().getStatusCode()) {
HttpEntity enties = response.getEntity();
if (enties != null) {
String str = EntityUtils.toString(enties);
System.out.println("返回信息:"+str);
}

if (enties != null) {
enties.consumeContent();
}
}

} catch (Exception e) {

} finally {
httpclient.getConnectionManager().shutdown();
}
}

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