首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >https get请求python (HTTPS / SSL)

https get请求python (HTTPS / SSL)
EN

Stack Overflow用户
提问于 2018-08-02 01:12:43
回答 1查看 160关注 0票数 1

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

代码语言:javascript
复制
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();
        }

我正在尝试在python3.x中找到一种简洁的方法来做到这一点。或者我猜斯坦德人在python中做了这样的事情。

我试过这样的方法:

代码语言:javascript
复制
conn = requests.get(env, headers={"content-type":"application/json"}, auth=(userName,password))

但一直没有太多的运气。

EN

回答 1

Stack Overflow用户

发布于 2018-08-02 02:11:26

对于python中的请求,需要传递url。

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51638850

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档