怎么调用httputil工具类
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了怎么调用httputil工具类相关的知识,希望对你有一定的参考价值。
参考技术A 以java为例,调用httputil示例如下:import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.params.HttpMethodParams;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.StringUtils;
/**
* <p>Http工具类
*
* <p>Http工具类,为系统提供通用Http访问操作方法:
*
* <p>1、发送GET请求;
* <p>2、发送POST请求。
*
*/
public class HttpUtil
/**
* <p>发送GET请求
*
* @param url GET请求地址
*
* @return 与当前请求对应的响应内容字节数组
*
*/
public static byte[] doGet(String url)
return HttpUtil.doGet(url , null , null , 0);
/**
* <p>发送GET请求
*
* @param url GET请求地址
* @param headerMap GET请求头参数容器
*
* @return 与当前请求对应的响应内容字节数组
*
*/
public static byte[] doGet(String url , Map headerMap)
return HttpUtil.doGet(url , headerMap , null , 0);
/**
* <p>发送GET请求
*
* @param url GET请求地址
* @param proxyUrl 代理服务器地址
* @param proxyPort 代理服务器端口号
*
* @return 与当前请求对应的响应内容字节数组
*
* @modify 窦海宁, 2012-03-19
*/
public static byte[] doGet(String url , String proxyUrl , int proxyPort)
return HttpUtil.doGet(url , null , proxyUrl , proxyPort);
/**
* <p>发送GET请求
*
* @param url GET请求地址
* @param headerMap GET请求头参数容器
* @param proxyUrl 代理服务器地址
* @param proxyPort 代理服务器端口号
*
* @return 与当前请求对应的响应内容字节数组
*
* @modify 窦海宁, 2012-03-19
*/
public static byte[] doGet(String url , Map headerMap , String proxyUrl , int proxyPort)
byte[] content = null;
HttpClient httpClient = new HttpClient();
GetMethod getMethod = new GetMethod(url);
if (headerMap != null)
//头部请求信息
if (headerMap != null)
Iterator iterator = headerMap.entrySet().iterator();
while (iterator.hasNext())
Entry entry = (Entry) iterator.next();
getMethod.addRequestHeader(entry.getKey().toString() , entry.getValue().toString());
if (StringUtils.isNotBlank(proxyUrl))
httpClient.getHostConfiguration().setProxy(proxyUrl , proxyPort);
//设置成了默认的恢复策略,在发生异常时候将自动重试3次,在这里你也可以设置成自定义的恢复策略
getMethod.getParams().setParameter(HttpMethodParams.SO_TIMEOUT , 10000);
//postMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER , new DefaultHttpMethodRetryHandler());
InputStream inputStream = null;
try
if (httpClient.executeMethod(getMethod) == HttpStatus.SC_OK)
//读取内容
inputStream = getMethod.getResponseBodyAsStream();
content = IOUtils.toByteArray(inputStream);
else
System.err.println("Method failed: " + getMethod.getStatusLine());
catch (IOException ex)
ex.printStackTrace();
finally
IOUtils.closeQuietly(inputStream);
getMethod.releaseConnection();
return content;
/**
* <p>发送POST请求
*
* @param url POST请求地址
* @param parameterMap POST请求参数容器
*
* @return 与当前请求对应的响应内容字节数组
*
*/
public static byte[] doPost(String url , Map parameterMap)
return HttpUtil.doPost(url , null , parameterMap , null , null , 0);
/**
* <p>发送POST请求
*
* @param url POST请求地址
* @param parameterMap POST请求参数容器
* @param paramCharset 参数字符集名称
*
* @return 与当前请求对应的响应内容字节数组
*
* @modify 窦海宁, 2012-05-21
*/
public static byte[] doPost(String url , Map parameterMap , String paramCharset)
return HttpUtil.doPost(url , null , parameterMap , paramCharset , null , 0);
/**
* <p>发送POST请求
*
* @param url POST请求地址
* @param headerMap POST请求头参数容器
* @param parameterMap POST请求参数容器
* @param paramCharset 参数字符集名称
*
* @return 与当前请求对应的响应内容字节数组
*
* @modify 窦海宁, 2012-05-21
*/
public static byte[] doPost(String url , Map headerMap , Map parameterMap , String paramCharset)
return HttpUtil.doPost(url , headerMap , parameterMap , paramCharset , null , 0);
/**
* <p>发送POST请求
*
* @param url POST请求地址
* @param parameterMap POST请求参数容器
* @param paramCharset 参数字符集名称
* @param proxyUrl 代理服务器地址
* @param proxyPort 代理服务器端口号
*
* @return 与当前请求对应的响应内容字节数组
*
*/
public static byte[] doPost(String url , Map parameterMap , String paramCharset , String proxyUrl , int proxyPort)
return HttpUtil.doPost(url , null , parameterMap , paramCharset , proxyUrl , proxyPort);
/**
* <p>发送POST请求
*
* @param url POST请求地址
* @param headerMap POST请求头参数容器
* @param parameterMap POST请求参数容器
* @param paramCharset 参数字符集名称
* @param proxyUrl 代理服务器地址
* @param proxyPort 代理服务器端口号
*
* @return 与当前请求对应的响应内容字节数组
*
* @modify 窦海宁, 2012-05-21
*/
public static byte[] doPost(String url , Map headerMap , Map parameterMap , String paramCharset , String proxyUrl , int proxyPort)
byte[] content = null;
HttpClient httpClient = new HttpClient();
PostMethod postMethod = new PostMethod(url);
if (StringUtils.isNotBlank(paramCharset))
postMethod.getParams().setContentCharset(paramCharset);
postMethod.getParams().setHttpElementCharset(paramCharset);
if (headerMap != null)
//头部请求信息
if (headerMap != null)
Iterator iterator = headerMap.entrySet().iterator();
while (iterator.hasNext())
Entry entry = (Entry) iterator.next();
postMethod.addRequestHeader(entry.getKey().toString() , entry.getValue().toString());
Iterator iterator = parameterMap.keySet().iterator();
while (iterator.hasNext())
String key = (String) iterator.next();
postMethod.addParameter(key , (String) parameterMap.get(key));
if (StringUtils.isNotBlank(proxyUrl))
httpClient.getHostConfiguration().setProxy(proxyUrl , proxyPort);
//设置成了默认的恢复策略,在发生异常时候将自动重试3次,在这里你也可以设置成自定义的恢复策略
postMethod.getParams().setParameter(HttpMethodParams.SO_TIMEOUT , 10000);
//postMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER , new DefaultHttpMethodRetryHandler());
InputStream inputStream = null;
try
if (httpClient.executeMethod(postMethod) == HttpStatus.SC_OK)
//读取内容
inputStream = postMethod.getResponseBodyAsStream();
content = IOUtils.toByteArray(inputStream);
else
System.err.println("Method failed: " + postMethod.getStatusLine());
catch (IOException ex)
ex.printStackTrace();
finally
IOUtils.closeQuietly(inputStream);
postMethod.releaseConnection();
return content;
public static void main(String[] args)
Map<String, String> map = new HashMap<String, String>();
map.put("wd", "nima");
byte[] b = doGet("http://www.baidu.com", map);
System.out.println("-------------------"+new String(b));
参考技术B 怎么调用httputil工具类
主要是设置两个地方:
1,info.plist中配置的URL Schemes
2.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
// TestApp2是TestApp2这个app在info.plist中配置的URL Schemes
if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"TestApp2://"]])
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"TestApp2://success=1&count=100"]];
return YES;
- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url
NSString *receText = [[url host] stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSLog(@"%@ %@",receText, url.absoluteString);
return YES;
HttpUtil工具类
HttpUtil工具类
mavne配置
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpcore</artifactId>
<version>4.4.10</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.6</version>
</dependency>
HttpUtil.java
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.Charset;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import lombok.extern.slf4j.Slf4j;
@Slf4j
public class HttpUtil
private static CloseableHttpClient httpClient;
static
PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager();
cm.setMaxTotal(100);
cm.setDefaultMaxPerRoute(20);
cm.setDefaultMaxPerRoute(50);
httpClient = HttpClients.custom().setConnectionManager(cm).build();
public static String get(String url)
log.info("HttpClient.get:", url);
CloseableHttpResponse response = null;
BufferedReader in = null;
String result = "";
try
HttpGet httpGet = new HttpGet(url);
RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(30000)
.setConnectionRequestTimeout(30000).setSocketTimeout(30000).build();
httpGet.setConfig(requestConfig);
httpGet.setConfig(requestConfig);
httpGet.addHeader("Content-type", "application/json; charset=utf-8");
httpGet.setHeader("Accept", "application/json");
response = httpClient.execute(httpGet);
in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
StringBuffer sb = new StringBuffer("");
String line = "";
String NL = System.getProperty("line.separator");
while ((line = in.readLine()) != null)
sb.append(line + NL);
in.close();
result = sb.toString();
catch (IOException e)
e.printStackTrace();
finally
try
if (null != response)
response.close();
catch (IOException e)
e.printStackTrace();
return result;
public static String post(String url, String jsonString)
log.info("HttpClient.post:,", url, jsonString);
CloseableHttpResponse response = null;
BufferedReader in = null;
String result = "";
try
HttpPost httpPost = new HttpPost(url);
RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(30000)
.setConnectionRequestTimeout(30000).setSocketTimeout(30000).build();
httpPost.setConfig(requestConfig);
httpPost.setConfig(requestConfig);
httpPost.addHeader("Content-type", "application/json; charset=utf-8");
httpPost.setHeader("Accept", "application/json");
httpPost.setEntity(new StringEntity(jsonString, Charset.forName("UTF-8")));
response = httpClient.execute(httpPost);
in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
StringBuffer sb = new StringBuffer("");
String line = "";
String NL = System.getProperty("line.separator");
while ((line = in.readLine()) != null)
sb.append(line + NL);
in.close();
result = sb.toString();
catch (IOException e)
e.printStackTrace();
finally
try
if (null != response)
response.close();
catch (IOException e)
e.printStackTrace();
return result;
以上是关于怎么调用httputil工具类的主要内容,如果未能解决你的问题,请参考以下文章