2 robinandable robinandable 于 2016.04.27 10:30 提问

用httpclient调用google elevation api时返回状态码为405 5C

如题:代码如下,为神马返回状态码为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();
        }
    }

2个回答

robinandable
robinandable   2016.04.27 11:22

有没有老司机啊,是因为google在国内被墙了吗?

robinandable
robinandable   2016.04.27 11:22

有没有老司机啊,是因为google在国内被墙了吗?

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!