简体   繁体   English

如何使用 HttpURLConnection for java android 发送身份验证密钥? 我收到错误代码 500

[英]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.

相关问题 错误500 HttpUrlConnection java - Error 500 HttpUrlConnection java 将HttpURLConnection GET请求与标头一起使用时,如何修复错误代码500? 标头未发送 - How to fix error code 500 when using HttpURLConnection GET request with header? Header is not being sent 嘿,我才刚开始在android中进行编码并收到错误Permission Denial:不允许在android中发送广播 - Hey , I am just started to code in android and getting error Permission Denial: not allowed to send broadcast in android 运行代码时,我在 Java Derby 数据库中收到此外键错误 - I am getting this foreign key error in Java Derby database when I run the code 如何将 controller 中的 RequestBody 定义为列表,我收到 500 错误 - How to define RequestBody in controller as list, I am getting 500 error 我想使用 multipart 将图像上传到后端,在 android 中使用改造。 我在邮递员中工作时收到 500 错误 - i want to upload images using multipart to backend using retrofit in android. i am getting 500 error whie it works in postman Java HTTPUrlConnection 返回 500 状态码 - Java HTTPUrlConnection returns 500 status code 使用HttpURLConnection在Android中进行摘要式身份验证 - Digest authentication in Android using HttpURLConnection 检查HTTP 500错误HttpUrlConnection Java的缺陷 - Flaw in checking HTTP 500 error HttpUrlConnection Java 当我尝试使用 Tomcat 服务器在 java servlet 中发送 GET 请求时,我收到错误实例化 servlet class - when I am trying to send a GET request in java servlet using Tomcat server I am getting error instantiating servlet class
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM