robinandable 2016-04-27 02:30 采纳率: 0%
浏览 2017
已结题

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

如题:代码如下,为神马返回状态码为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 2016-04-27 03:22
    关注

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

    评论

报告相同问题?

悬赏问题

  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容