gpt4 book ai didi

http - 如何将 Python urllib.request 代码翻译成 Java 代码

转载 作者:可可西里 更新时间:2023-11-01 17:05:55 25 4
gpt4 key购买 nike

这是python代码

import urllib.request as urllib2
import json

data = {
"Inputs": {
"input1": {
"ColumnNames": ["id", "regex"],
"Values": [ [ "0", "the regex value" ],]
},
},
"GlobalParameters": {
"Database query": "select * from expone",
}
}

body = str.encode(json.dumps(data))

url = 'https://ussouthcentral.services.azureml.net/workspaces/4729545551a741e1a2e606d37' \
'ae61ce0/services/ac7c34ad134d43ca9fdc65e292ce35d3/execute?api-version=2.0&details=true'
api_key = '8ku5P6fR3F8ykgMHK5Y8+PL8dn+Zi2Ajmwyjk9ENsomzzkDfuT8CtgKS7dF4yjaJfYxARe+1iLjh' \
'Tv1R0qOTvw=='
headers = {
'Content-Type': 'application/json',
'Authorization': ('Bearer '+ api_key)
}

req = urllib2.Request(url, body, headers)

try:
response = urllib2.urlopen(req)
result = response.read()
print(result)
except Exception as e:
print("The request failed with status code: ", e)

这是我在 Java 中的尝试

public static void main(String[] args) {   
System.out.println("MachineLearning main");
try{
//connections settings
URL url = new URL("https://ussouthcentral.services.azureml.net/workspaces/4729545551a741e1a2e606d37ae61ce0/services/ac7c34ad134d43ca9fdc65e292ce35d3/execute?api-version=2.0&details=true");
HttpURLConnection con = (HttpURLConnection)url.openConnection();
con.setDoInput(true);
con.setDoOutput(true);
String requestMethod = "GET";
con.setRequestMethod(requestMethod);
con.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");

String data=URLEncoder.encode("input1", "UTF-8") + "="
+ URLEncoder.encode("\"ColumnNames\": [\"id\", \"regex\"]", "UTF-8") + "&"
+ URLEncoder.encode("GlobalParameters", "UTF-8")
+ URLEncoder.encode("Database query\": \"select * from expone\"", "UTF-8");

//make the request
OutputStreamWriter writer = new OutputStreamWriter(con.getOutputStream());
writer.write(data);
writer.flush();

//read the request
BufferedReader reader=new BufferedReader(new InputStreamReader(con.getInputStream()));
String response;
while ((response=reader.readLine())!=null)
System.out.println(response);
}
catch(Exception e) {
System.out.println("Exception in MachineLearning.main " + e);
}
}

java中请求的代码不成功,返回异常:服务器返回 HTTP 响应代码:URL 为 401

问题是我不知道如何将 python 中的数据变量转换为 java 中的数据变量,以及如何传递 apiKey 以及如何将其放入 header 中?

最佳答案

您的 Java 代码中有几个小“错误”:

  1. setRequestMethod更改为“POST”
  2. 将“Content-Type”更改为“application/json”
  3. 为授权添加一个新的setRequestProperty
  4. 不要对 数据 进行 urlencode

代码:

public static void main(String[] args) {  
System.out.println("MachineLearning main");
try{
//connections settings
String api_key = "8ku5P6fR3F8ykgMHK5Y8+PL8dn+Zi2Ajmwyjk9ENsomzzkDfuT8CtgKS7dF4yjaJfYxARe+1iLjhTv1R0qOTvw==";
String data = "{\"Inputs\": {\"input1\": {\"ColumnNames\": [\"id\", \"regex\"], \"Values\": [[\"0\", \"the regex value\"]]}}, \"GlobalParameters\": {\"Database query\": \"select * from expone\"}}";
URL url = new URL("https://ussouthcentral.services.azureml.net/workspaces/4729545551a741e1a2e606d37ae61ce0/services/ac7c34ad134d43ca9fdc65e292ce35d3/execute?api-version=2.0&details=true");
HttpURLConnection con = (HttpURLConnection) url.openConnection();

con.setDoInput(true);
con.setDoOutput (true);
con.setRequestMethod("POST");
con.setRequestProperty("Content-Type", "application/json");
con.setRequestProperty("Authorization", "Bearer " + api_key);

//make the request
OutputStreamWriter writer = new OutputStreamWriter(con.getOutputStream());
writer.write(data);
writer.flush();

//read the request
BufferedReader reader=new BufferedReader(new InputStreamReader(con.getInputStream()));
String response;
while ((response=reader.readLine())!=null)
System.out.println(response);
} catch(Exception e) {
System.out.println("Exception in MachineLearning.main " + e);
}
}

关于http - 如何将 Python urllib.request 代码翻译成 Java 代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43611790/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com