[英]How to send authentication key using HttpURLConnection for java android? I am getting error code 500
I am trying to access Skyscanner API through RapidAPI.com.我正在尝试通过 RapidAPI.com 访问 Skyscanner API。 I am taking the provided code snippets and reformatting it for my program.
我正在使用提供的代码片段并为我的程序重新格式化它。 When I run my program I get error code 500 and this link https://skyscanner-skyscanner-flight-search-v1.p.rapidapi.com/apiservices/pricing/v1.0
当我运行我的程序时,我收到错误代码 500 和此链接https://skyscanner-skyscanner-flight-search-v1.p.rapidapi.com/apiservices/pricing/v1.0
I tested the endpoints on rapid api and it works fine there.我在快速 api 上测试了端点,它在那里工作正常。
I am providing the key provided to me by rapidapi.我正在提供rapidapi提供给我的密钥。
I have tried running just java code on intellij and java code on android studio.我试过只在 intellij 上运行 java 代码,在 android studio 上运行 java 代码。 I have tried to use HttpURLConnection and relevant examples on google when searching "How to use HttpURLConnection".
在搜索“如何使用 HttpURLConnection”时,我尝试在 google 上使用 HttpURLConnection 和相关示例。 I have tried HttpResponse.
我试过 HttpResponse。 Same results.
结果一样。 I am positive I am sending the key.
我很确定我正在发送密钥。
String url = "https://skyscanner-skyscanner-flight-search-v1.p.rapidapi.com/apiservices/pricing/v1.0";
URL obj = new URL(url);
HttpURLConnection con = (HttpsURLConnection) obj.openConnection();
// optional default is GET
con.setRequestMethod("POST");
//add request header
con.setRequestProperty("X-RapidAPI-Key", "dfgdgsgMYKEY");
con.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
con.setRequestProperty("inboundDate", "2019-01-10"); //omitted other requests properties
con.setUseCaches(false);
con.setDoInput(true);
con.setDoOutput(true);
int responseCode = con.getResponseCode();
System.out.println("\nSending 'GET' request to URL : " + url);
System.out.println("Response Code : " + responseCode);
BufferedReader in = new BufferedReader(
new InputStreamReader(con.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
//print result
System.out.println(response.toString());
... // my android code attempt ... // 我的 android 代码尝试
Future<HttpResponse<JsonNode>> future = Unirest.post("https://skyscanner-skyscanner-flight-search-v1.p.rapidapi.com/apiservices/pricing/v1.0")
.header("X-RapidAPI-Key", "jngoin4ooemgoerg") //not the actual key
.header("Content-Type", "application/x-www-form-urlencoded")
.field("inboundDate", "2019-07-10")
.field("cabinClass", "business")
.field("children", 0)
.field("infants", 0)
.field("country", "US")
.field("currency", "USD")
.field("locale", "en-US")
.field("originPlace", "SFO-sky")
.field("destinationPlace", "LHR-sky")
.field("outboundDate", "2019-06-01")
.field("adults", 1)
.asJsonAsync(new Callback<JsonNode>() {
public void failed(UnirestException e) {
System.out.println("The request has failed");
}
public void completed(HttpResponse<JsonNode> response) {
int code = response.getStatus();
Headers headers = response.getHeaders();
JsonNode body = response.getBody();
InputStream rawBody = response.getRawBody();
}
public void cancelled() {
System.out.println("The request has been cancelled");
}
});
... ...
EDIT编辑
String url = "https://skyscanner-skyscanner-flight-search-v1.p.rapidapi.com/apiservices/browsequotes/v1.0/US/USD/en-US/SFO-sky/JFK-sky/2019-09-01";
URL obj = new URL(url);
HttpsURLConnection con = (HttpsURLConnection) obj.openConnection();
//add request header
con.setRequestProperty("X-RapidAPI-Key", "werfwqefqwef");
//con.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
con.setUseCaches(false);
con.setDoInput(true);
con.setDoOutput(true);
con.setRequestMethod("GET");
int responseCode = con.getResponseCode();
System.out.println("\nSending 'GET' request to URL : " + url);
System.out.println("Response Code : " + responseCode);
BufferedReader in = new BufferedReader(
new InputStreamReader(con.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
//print result
System.out.println(response.toString());
I am expecting a JSON string but I am getting error 500.我期待一个 JSON 字符串,但我收到错误 500。
I believe X-RapidAPI-Host
header is missing.我相信
X-RapidAPI-Host
标头丢失。 Try adding it尝试添加它
x-rapidapi-host: "skyscanner-skyscanner-flight-search-v1.p.rapidapi.com"
PS Skyscanner RapidAPI integration in no longer available. PS Skyscanner RapidAPI 集成不再可用。 So, I think now it will not work anyway.
所以,我认为现在无论如何都行不通。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.