在使用阿里云短信服务的时候,由于要将服务设置在内网供其他服务调用,这就需要用代理去处理短信接口请求,但是阿里官方提供的API就两个jar包,将http请求的具体细节封装起来了,楼主为此很是无奈,然后想到有个可以设置全局代理System.setPropert的方式,这种方式固然可以,但是整合springcloud的eureka之后,发生了非常奇怪的事情,使用代理的时候直接把eureka的心跳给弄死了,疯狂的报错。然后无奈之下,去官网找客服,打电话咨询后竟然说没有这个东西,然后又到官网问答那儿,有位大神告诉我说可以用弹性网卡,但是考虑到具体业务场景,最终没考虑没用它。然后又想到一个比较笨的点子,之前看它封装的Jar里使用也是HttpURLConnection就想如果直接继承它里面的类,在此基础上修改就好了,可是那里面很多受保护的类,并不能直接继承,这时候就想到用最简单粗暴的方法,将用到的类把源码全部拷出来直接修改,大概也就7,8类的样子,加上代理,然后调用的时候就用自己修改的类美滋滋,搞定了,虽然这个方法比较笨,但是最终解决的了问题,也不知道和我遇到同样问题的大侠是怎么处理的。。。
这个是直接把源码拷贝出来的类
就是下面这个方法。。
private HttpURLConnection buildHttpConnection(HttpRequest request) throws IOException {
Map<String, String> mappedHeaders = request.getHeaders();
String strUrl = request.getUrl();
if (null == strUrl) {
throw new IllegalArgumentException("URL is null for HttpRequest.");
}
if (null == request.getMethod()) {
throw new IllegalArgumentException("Method is not set for HttpRequest.");
}
URL url = null;
String[] urlArray = null;
if (MethodType.POST.equals(request.getMethod()) && null == request.getHttpContent()) {
urlArray = strUrl.split("\\?");
url = new URL(urlArray[0]);
} else {
url = new URL(strUrl);
}
System.setProperty("sun.net.http.allowRestrictedHeaders", "true");
HttpURLConnection httpConn = null;
//加载通用属性配置
BasicProperties basic = ApplicationContextUtil.getBean(BasicProperties.class);
if (url.getProtocol().equalsIgnoreCase("https")) {
if (sslSocketFactory != null) {
HttpsURLConnection httpsConn = null;
if(basic.isEnable()) {
logger.info("==============Https Proxy Mode!");
Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(basic.getProxyHost(), Integer.parseInt(basic.getProxyPort())));
httpsConn = (HttpsURLConnection)url.openConnection(proxy);
}else {
logger.info("==============Https Direct Mode!");
httpsConn = (HttpsURLConnection)url.openConnection();
}
httpsConn.setSSLSocketFactory(sslSocketFactory);
httpConn = httpsConn;
}
}
if (httpConn == null) {
if(basic.isEnable()) {
logger.info("==============HTTP Proxy Mode!");
Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(basic.getProxyHost(), Integer.parseInt(basic.getProxyPort())));
httpConn = (HttpURLConnection)url.openConnection(proxy);
}else{
logger.info("==============HTTP Direct Mode!");
httpConn = (HttpURLConnection)url.openConnection();
}
}
httpConn.setRequestMethod(request.getMethod().toString());
httpConn.setDoOutput(true);
httpConn.setDoInput(true);
httpConn.setUseCaches(false);
//自定义超时设置
httpConn.setConnectTimeout(new Integer(basic.getConnectTimeOut()));
httpConn.setReadTimeout(new Integer(basic.getReadTimeOut()));
if (request.getConnectTimeout() != null) {
httpConn.setConnectTimeout(request.getConnectTimeout());
}
if (request.getReadTimeout() != null) {
httpConn.setReadTimeout(request.getReadTimeout());
}
httpConn.setRequestProperty(ACCEPT_ENCODING, "identity");
for (Entry<String, String> entry : mappedHeaders.entrySet()) {
httpConn.setRequestProperty(entry.getKey(), entry.getValue());
}
if (null != request.getHeaderValue(CONTENT_TYPE)) {
httpConn.setRequestProperty(CONTENT_TYPE, request.getHeaderValue(CONTENT_TYPE));
} else {
String contentTypeValue = request.getContentTypeValue(request.getHttpContentType(), request.getEncoding());
if (null != contentTypeValue) {
httpConn.setRequestProperty(CONTENT_TYPE, contentTypeValue);
}
}
if (MethodType.POST.equals(request.getMethod()) && null != urlArray && urlArray.length == 2) {
httpConn.getOutputStream().write(urlArray[1].getBytes());
}
return httpConn;
}