使用httpclient 4.4实现需要basic auth认证的http请求
2015-06-25 00:00
621 查看
摘要: 使用httpclient 4.4实现需要basic auth认证的http请求
测试代码
public class HttpHelper { private final static HttpHelper instance = new HttpHelper(); private HttpHelper() { } public final static HttpHelper getInstnace() { return instance; } /** * * @param host 服务主机地址如 apiv2.sewcloud.com * @param port 服务端口 * @param uri 请求的uri如/api/sendmail * @param postData 提交的请求数据 * @param username 用户名 * @param password 密码 * @param timeout 超时时间(单位为毫秒) * @return * @throws ClientProtocolException * @throws IOException * @throws KeyManagementException * @throws NoSuchAlgorithmException */ public String postBasicAuth(String host, int port, String uri, String postData, String username, String password, int timeout) throws ClientProtocolException, IOException, KeyManagementException, NoSuchAlgorithmException { String deliverResponse = null; CloseableHttpClient httpclient = HttpClients.createDefault(); HttpHost targetHost = new HttpHost(host, port, "http"); CredentialsProvider credsProvider = new BasicCredentialsProvider(); credsProvider.setCredentials(new AuthScope(targetHost.getHostName(), targetHost.getPort()), new UsernamePasswordCredentials( username, password)); // Create AuthCache instance AuthCache authCache = new BasicAuthCache(); // Generate BASIC scheme object and add it to the local auth cache BasicScheme basicAuth = new BasicScheme(); authCache.put(targetHost, basicAuth); // Add AuthCache to the execution context HttpClientContext context = HttpClientContext.create(); context.setCredentialsProvider(credsProvider); context.setAuthCache(authCache); HttpPost httppost = new HttpPost(uri); //设置超时时间 RequestConfig requestConfig = RequestConfig.custom() .setConnectionRequestTimeout(timeout) .setConnectTimeout(timeout).setSocketTimeout(timeout).build(); httppost.setConfig(requestConfig); StringEntity postentity = new StringEntity(postData,"utf-8"); postentity.setContentType("application/json"); postentity.setContentEncoding("utf-8"); //httppost.setEntity(new UrlEncodedFormEntity(params, "utf-8")); httppost.setEntity(postentity); CloseableHttpResponse response = null; try { response = httpclient.execute(targetHost, httppost, context); HttpEntity entity = response.getEntity(); deliverResponse = EntityUtils.toString(entity); httpclient.close(); } finally { if (response != null) { try { response.close(); } catch (IOException e) { e.printStackTrace(); } } } return deliverResponse; } }
测试代码
HttpHelper httpHelper = HttpHelper.getInstnace(); String resp = this.httpHelper.postBasicAuth("apiv2.sewcloud.com", 80, "/api/sendmail", builder.toString(), "sewcloud", "sewcloud6688" ,10000);
相关文章推荐
- 穿越到 1983 年学习 BASIC
- FREEBASIC 编译可被python调用的dll函数示例
- ColdFusionMX 编程指南 ColdFusionMX Basic Tag编程
- ASP.NET MVC Web API HttpClient简介
- 使用httpclient实现免费的google翻译api
- PHP封装的HttpClient类用法实例
- Rsync ERROR: auth failed on module解决方法
- Android 中HttpURLConnection与HttpClient使用的简单实例
- Android通过HttpURLConnection和HttpClient接口实现网络编程
- volley框架下发送和读取cookie
- HttpClient不保存、发送Cookie
- HttpClient获取服务器JSON数据问题处理
- 学习 HTTP协议挺不错的一个类
- 通过Http协议下载文件、访问接口等等
- Apache HttpClient
- HttpClient 使用
- 了解HttpParser
- httpclient完全支持ssl连接方式