[英]How to send authentication key using HttpURLConnection for java android? I am getting error code 500
我正在嘗試通過 RapidAPI.com 訪問 Skyscanner API。 我正在使用提供的代碼片段並為我的程序重新格式化它。 當我運行我的程序時,我收到錯誤代碼 500 和此鏈接https://skyscanner-skyscanner-flight-search-v1.p.rapidapi.com/apiservices/pricing/v1.0
我在快速 api 上測試了端點,它在那里工作正常。
我正在提供rapidapi提供給我的密鑰。
我試過只在 intellij 上運行 java 代碼,在 android studio 上運行 java 代碼。 在搜索“如何使用 HttpURLConnection”時,我嘗試在 google 上使用 HttpURLConnection 和相關示例。 我試過 HttpResponse。 結果一樣。 我很確定我正在發送密鑰。
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());
... // 我的 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");
}
});
...
編輯
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());
我期待一個 JSON 字符串,但我收到錯誤 500。
我相信X-RapidAPI-Host
標頭丟失。 嘗試添加它
x-rapidapi-host: "skyscanner-skyscanner-flight-search-v1.p.rapidapi.com"
PS Skyscanner RapidAPI 集成不再可用。 所以,我認為現在無論如何都行不通。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.