httpclient 访问https应用

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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值