如何使用httpclient访问网络
1个回答
2015-05-17 · 知道合伙人数码行家
huanglenzhi
知道合伙人数码行家
向TA提问 私信TA
知道合伙人数码行家
采纳数:117538
获赞数:517194
长期从事计算机组装,维护,网络组建及管理。对计算机硬件、操作系统安装、典型网络设备具有详细认知。
向TA提问 私信TA
关注
展开全部
使用Apache HttpClient访问网络
package com.szy.news.service;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;
import org.json.JSONObject;
import com.szy.news.model.Parameter;
/**
*以同步方式发送Http请求
*/
public class SyncHttp
{
/**
* 通过GET方式发送请求
* @param url URL地址
* @param params 参数
* @return
* @throws Exception
*/
public String httpGet(String url, String params) throws Exception
{
String response = null; //返回信息
//拼接请求URL
if (null!=params&&!params.equals(""))
{
url += "?" + params;
}
int timeoutConnection = 3000;
int timeoutSocket = 5000;
HttpParams httpParameters = new BasicHttpParams();
// Set the timeout in milliseconds until a connection is established.
HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutConnection);
// in milliseconds which is the timeout for waiting for data.
HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket);
// 构造HttpClient的实例
// HttpClient httpClient = new DefaultHttpClient();
HttpClient httpClient = new DefaultHttpClient(httpParameters);
// 创建GET方法的实例
HttpGet httpGet = new HttpGet(url);
try
{
HttpResponse httpResponse = httpClient.execute(httpGet);
int statusCode = httpResponse.getStatusLine().getStatusCode();
if (statusCode == HttpStatus.SC_OK) //SC_OK = 200
{
// 获得返回结果
response = EntityUtils.toString(httpResponse.getEntity());
JSONObject jsonObject = new JSONObject(response);
}
else
{
response = "返回码:"+statusCode;
}
} catch (Exception e)
{
throw new Exception(e);
}
return response;
}
/**
* 通过POST方式发送请求
* @param url URL地址
* @param params 参数
* @return
* @throws Exception
*/
public String httpPost(String url, List<Parameter> params) throws Exception
{
String response = null;
int timeoutConnection = 3000;
int timeoutSocket = 5000;
// Set the timeout in milliseconds until a connection is established.
HttpParams httpParameters = new BasicHttpParams();
// Set the default socket timeout (SO_TIMEOUT).in milliseconds which is the timeout for waiting for data.
HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutConnection);
HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket);
// 构造HttpClient的实例
//HttpClient httpClient = new DefaultHttpClient();
HttpClient httpClient = new DefaultHttpClient(httpParameters);
HttpPost httpPost = new HttpPost(url);
if (params.size()>=0)
{
//设置httpPost请求参数
httpPost.setEntity(new UrlEncodedFormEntity(buildNameValuePair(params),HTTP.UTF_8));
}
//使用execute方法发送HTTP Post请求,并返回HttpResponse对象
HttpResponse httpResponse = httpClient.execute(httpPost);
int statusCode = httpResponse.getStatusLine().getStatusCode();
if(statusCode==HttpStatus.SC_OK)
{
//获得返回结果
response = EntityUtils.toString(httpResponse.getEntity());
}
else
{
response = "返回码:"+statusCode;
}
return response;
}
/**
* 把Parameter类型集合转换成NameValuePair类型集合
* @param params 参数集合
* @return
*/
private List<BasicNameValuePair> buildNameValuePair(List<Parameter> params)
{
List<BasicNameValuePair> result = new ArrayList<BasicNameValuePair>();
for (Parameter param : params)
{
BasicNameValuePair pair = new BasicNameValuePair(param.getName(), param.getValue());
result.add(pair);
}
return result;
}
}
Storm代理
2023-05-30 广告
2023-05-30 广告
StormProxies是全球大数据IP资源服务商,其住宅代理网络由真实的家庭住宅IP组成,可为企业或个人提供满足各种场景的代理产品。点击免费测试(注册即送1G流量)StormProxies有哪些优势?1、IP+端口提取形式,不限带宽,IP...
点击进入详情页
本回答由Storm代理提供
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询