一、HttpsURLConnection调用接口GET方法

做接口测试有时候,由于受限制于签名及网络无法使用postman、jmeter接口工具。需要自己编写脚本发送请求,这里介绍java使用HttpsURLConnection工具类,发送请求的方法,请求接口的GET方法。话不多说,直接上代码:

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.InetSocketAddress;
import java.net.MalformedURLException;
import java.net.Proxy;
import java.net.URL;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.util.ArrayList;
import java.util.List;

import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSession;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;

import AiLab.base64_no_empty;
import net.sf.json.JSONObject;


public class action {

	public static void main(String[] args) {
		
        GetResult("djlso441148");
}
public static String GetResult(String token){
		BufferedReader in = null;        
		StringBuilder result = new StringBuilder(); 
		try {
			//设置代理,如有填入代理地址和端口号
			Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("xxxxxxxxxxx", xxxx));
			//接口地址
			String u = "xxxxxxx"+token;
			URL url = new URL(u);
			HttpURLConnection conn = (HttpURLConnection)url.openConnection(proxy);
			conn.setRequestMethod("GET");
			conn.setDoOutput(false);
			conn.setDoInput(true);
			conn.setConnectTimeout(30000);
			conn.setReadTimeout(30000);
			conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
			conn.connect();  
			in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8"));
			String line;
			while ((line = in.readLine()) != null) {
				result.append(line);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		finally{
			try{
				if(in!=null){
					in.close();
				}
			}
			catch(IOException ex){
				ex.printStackTrace();
			}
		}


		String Toresult = ascii2native(result.toString());
		return Toresult;
	}
	
	//将返回的ASCII码转为汉字(若有)
	public static String ascii2native ( String asciicode ){
		String[] asciis = asciicode.split ("\\\\u");
		String nativeValue = asciis[0];
		try
		{
			for ( int i = 1; i < asciis.length; i++ )
			{
				String code = asciis[i];
				nativeValue += (char) Integer.parseInt (code.substring (0, 4), 16);
				if (code.length () > 4)
				{
					nativeValue += code.substring (4, code.length ());
				}
			}
		}
		catch (NumberFormatException e)
		{
			return asciicode;
		}
		return nativeValue;
	}

Https信任和忽略证书:

//Https的认证(全部信任)
	static TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() {
		public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {
		}

		public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {
		}

		public X509Certificate[] getAcceptedIssuers() {
			return null;
		}
	} };
	//Https忽略证书
	class NullHostNameVerifier implements HostnameVerifier {

		public boolean verify(String arg0, SSLSession arg1) {

			return true;
		}
	}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值