所以我正致力于将我的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();
}我正在尝试在python3.x中找到一种简洁的方法来做到这一点。或者我猜斯坦德人在python中做了这样的事情。
我试过这样的方法:
conn = requests.get(env, headers={"content-type":"application/json"}, auth=(userName,password))但一直没有太多的运气。
发布于 2018-08-02 02:11:26
对于python中的请求,需要传递url。
conn = requests.get(url = 'https://myurl', headers = {'Content-Type':'application/json'})
https://stackoverflow.com/questions/51638850
复制相似问题