【问题标题】:https get request python (HTTPS / SSL)https 获取请求 python (HTTPS / SSL)
【发布时间】:2018-08-01 17:12:42
【问题描述】:

所以我正在努力将我拥有的 java 程序转换为 python。在我的 java 代码中,我有一个如下所示的 http get 调用。

sslContext.init(null, new TrustManager[] { new X509TrustManager() {
            public X509Certificate[] getAcceptedIssuers() {
                return null;
            }

            public void checkClientTrusted(X509Certificate[] certs, String authType) {
            }

            public void checkServerTrusted(X509Certificate[] certs, String authType) {
            }
        } }, new SecureRandom());
        try {
            CloseableHttpClient httpclient = HttpClients.custom()
                    .setSSLSocketFactory(new org.apache.http.conn.ssl.SSLSocketFactory(sslContext)).build();

            String authString = username + ":" + password;
            byte[] authEncBytes = Base64.encodeBase64(authString.getBytes());
            String authStringEnc = new String(authEncBytes);

            HttpGet httpGet = new HttpGet(envURL);
            httpGet.setHeader("Content-Type", "application/json");

            httpGet.setHeader("Authorization", "Basic " + authStringEnc);

            CloseableHttpResponse httpGetResponse = httpclient.execute(httpGet);

            HttpEntity entityResponse = httpGetResponse.getEntity();
            String result = EntityUtils.toString(entityResponse);
            EntityUtils.consume(entityResponse);
            JSONParser parser = new JSONParser();
            thresholdContent = (JSONArray) parser.parse(result);

        } catch (Exception e) {
            e.printStackTrace();
        }

我正在尝试在 python 3.x 中找到更简洁的方法来执行此操作。或者我猜想在 python 中做这样的事情的标准。

我试过类似的东西:

conn = requests.get(env, headers={"content-type":"application/json"}, auth=(userName,password))

但运气不佳。

【问题讨论】:

    标签: python python-3.x https python-requests


    【解决方案1】:

    使用 python 中的请求,您需要传递 url

    conn = requests.get(url = 'https://myurl', headers = {'Content-Type':'application/json'})

    【讨论】:

      猜你喜欢
      • 2016-06-29
      • 1970-01-01
      • 1970-01-01
      • 2018-02-04
      • 1970-01-01
      • 2012-07-07
      • 1970-01-01
      • 2017-10-22
      • 2016-09-08
      相关资源
      最近更新 更多