httpclient 访问https应用
来源:互联网 发布:js仿淘宝上拉查看详情 编辑:程序博客网 时间:2024/06/05 03:57
HTTPS
public class Test { public static HttpClient wrapClient(HttpClient base) throws Exception { SSLContext ctx = SSLContext.getInstance("TLSv1"); X509TrustManager tm = new X509TrustManager(){ public X509Certificate[] getAcceptedIssuers() { return null; } public void checkClientTrusted(X509Certificate[] arg0, String arg1) throws CertificateException {} public void checkServerTrusted(X509Certificate[] arg0, String arg1)throws CertificateException{} }; ctx.init(null, new TrustManager[] { tm }, null); SSLSocketFactory ssf = new SSLSocketFactory(ctx, SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER); ClientConnectionManager ccm = base.getConnectionManager(); SchemeRegistry registry = ccm.getSchemeRegistry(); registry.register(new Scheme("https", 443, ssf)); return new DefaultHttpClient(ccm, base.getParams()); } public static void main(String args[]) throws Exception { HttpClient client=wrapClient(new DefaultHttpClient()); // HttpGet get=new HttpGet("https://beta.openimslive.com/omp/oauth/authorize?app_key=cqMIkoEd9AUrktTP7EMWNykTwlka&response_type=code&redirect_uri=http://www.engyne.net");// get.setHeader("Host", "1.1.1.1:8543");// get.setHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");// get.setHeader("Accept", "text/html, application/xhtml+xml, */*"); String fastloginUsername = "+8675566552003"; String fastloginPassword = "mdx3600@HW"; String server = "205.177.226.80:8543"; String appKey = "wfbVlIqjuT4miYMFxmqhMSn2Bsoa"; OmpRestClient Restclient = new OmpRestClient(server, appKey); String accessToken=Restclient.getFastloginToken(fastloginUsername, fastloginPassword); System.out.println(accessToken); String url="https://193.3.2.16:8543/sandbox/rest/httpsessions/click2Call/v1.0?app_key=nGe8HpnhunNsh5yYGT8xfcAE9dsa&access_token="+"+accessToken+"+"&format=json"; HttpPost post = new HttpPost(url);//post.setHeader("Content-Type","application/x-www-form-urlencoded;charset=UTF-8"); List<BasicNameValuePair> formparams = new ArrayList<BasicNameValuePair>(); formparams.add(new BasicNameValuePair("calleeNbr","+8613163359533")); formparams.add(new BasicNameValuePair("callerNbr","+867556666663036")); formparams.add( new BasicNameValuePair("displayNbr","+867556666663036")); formparams.add(new BasicNameValuePair("languageType","1")); UrlEncodedFormEntity uefEntity= new UrlEncodedFormEntity(formparams, "UTF-8"); post.setEntity(uefEntity); HttpResponse resp=client.execute(post); System.out.println(resp.toString()); System.out.println(resp.getStatusLine().getStatusCode()); System.out.println(resp.getEntity().getContent()); } }HTTP
普通的httpclient应用 应对http情况
package net.engyne.common;import java.io.IOException;import java.security.cert.CertificateException;import java.security.cert.X509Certificate;import javax.net.ssl.SSLContext;import javax.net.ssl.SSLSocketFactory;import javax.net.ssl.TrustManager;import javax.net.ssl.X509TrustManager;import org.apache.commons.httpclient.HttpClient;import org.apache.commons.httpclient.HttpException;import org.apache.commons.httpclient.HttpStatus;import org.apache.commons.httpclient.NameValuePair;import org.apache.commons.httpclient.methods.GetMethod;import org.apache.commons.httpclient.methods.PostMethod;import org.apache.commons.io.IOUtils;import org.apache.log4j.Logger;import net.sf.json.JSONObject;public class RemoteOperation {private static Logger logger = Logger.getLogger(RemoteOperation.class);public static JSONObject remotePostCall(String url) throws HttpException, IOException {HttpClient httpClient = new HttpClient();PostMethod post = new PostMethod(url);post.setRequestHeader("Content-Type","application/x-www-form-urlencoded;charset=UTF-8"); NameValuePair[] param = { new NameValuePair("calleeNbr","+8613163359533"), new NameValuePair("callerNbr","+867556666663036"), new NameValuePair("displayNbr","+867556666663036"), new NameValuePair("languageType","1") } ; post.setRequestBody(param);int retcode = httpClient.executeMethod(post);if (retcode != HttpStatus.SC_OK) {// 发送不成功logger.info("远程调用出错");return null;} else {String body = post.getResponseBodyAsString();logger.info(body + "远程调用php成功");JSONObject jsonObject = new JSONObject();try {jsonObject = JSONObject.fromObject(body);} catch (Exception e) {e.printStackTrace();}if (post != null) {post.releaseConnection();}return jsonObject;}}public static JSONObject remoteGetCall(String url) throws HttpException, IOException {HttpClient httpClient = new HttpClient();GetMethod method = new GetMethod(url);method.setRequestHeader("Accept", "text/html, application/xhtml+xml, */*");;int retcode = httpClient.executeMethod(method);if (retcode != HttpStatus.SC_OK) {// 发送不成功logger.info("远程调用出错");return null;} else {String body = method.getResponseBodyAsString();logger.info(body + "远程调用php成功");JSONObject jsonObject = new JSONObject();try {jsonObject = JSONObject.fromObject(body);} catch (Exception e) {e.printStackTrace();}if (method != null) {method.releaseConnection();}return jsonObject;}}public static void main(String args[]){}}
0 0
- httpclient 访问https应用
- HttpClient 实现https访问
- android HttpClient 访问https
- httpclient 访问https
- httpclient访问https
- httpclient-访问https
- httpclient来进行https访问
- 使用httpclient来进行https访问
- [Android实例] Android 使用HttpClient访问https
- 通过HttpClient实现访问HTTPS网站
- java httpclient 无证书访问 https
- HttpClient 使用代理访问Https【转】
- HttpClient忽略证书访问HTTPS接口
- android httpClient 支持HTTPS的访问方式
- HttpClient忽略证书访问HTTPS接口
- httpclient 面https认证,带cookie访问
- android httpClient 支持HTTPS的访问方式
- Android:使用HttpClient访问HTTP/HTTPS服务器
- 实现Static Initialization从而不需要安装OpenCV Manager
- Android7(N)开发者应该知道的一切(最全)
- maven配置文件详解
- Windows svn服务端安装步骤visualSVN
- 改变 导航 控制器 tittle的字体颜色
- httpclient 访问https应用
- hdu 1254 推箱子
- 树莓派3下远程桌面到Windows系统
- centos 6.5安装GitLab全过程和问题记录
- 这是一遍测试博客
- linux网络编程之-----多播(组播)编程
- 【Android】【ViewGroup】【ScrollView】ScrollView的一些细节
- ES6中的一些新特性
- POJ1159 Palindrome(滚动数组DP)