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
原创粉丝点击