如题:代码如下,为神马返回状态码为405呢?有哪位大神用过google elevation api,通过浏览器直接访问接口正常,用httpclient访问在不设置代理前直接连接超时,设置代理后后返回405的状态码。
public static void test2(){
try {
SSLContext sslcontext = SSLContexts.custom()
.loadTrustMaterial(new File("C:\\Program Files\\Java\\jdk1.7.0_13\\jre\\lib\\security\\cacerts"), "changeit".toCharArray(),
new TrustSelfSignedStrategy())
.build();
SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(
sslcontext,
new String[] { "TLSv1" },
null,
SSLConnectionSocketFactory.getDefaultHostnameVerifier());
CloseableHttpClient httpclient = HttpClients.custom()
.setSSLSocketFactory(sslsf)
.build();
HttpHost proxy = new HttpHost("220.255.2.153"); //设置google代理服务器
RequestConfig config = RequestConfig.custom().setProxy(proxy).build();
HttpGet httpget = new HttpGet("https://maps.googleapis.com/maps/api/elevation/json?locations=39.938576,116.42188&key=YOUR_KEY");
httpget.setConfig(config);
System.out.println("Executing request " + httpget.getRequestLine());
HttpResponse response = httpclient.execute(httpget);
HttpEntity entity = response.getEntity();
System.out.println("----------------------------------------");
System.out.println(response.getStatusLine().getStatusCode());
EntityUtils.consume(entity);
} catch (Exception e) {
e.printStackTrace();
}
}