簡體   English   中英

如何為 Android java 編寫/轉換 CURL

[英]How to write / convert CURL for Android java

I am trying to implement the MOT history API https://dvsa.github.io/mot-history-api-documentation/ and they give an example using CURL which works with the supplied api key successfully when using an online CURL tool.

我試圖在 Android 中實現這一點,並意識到我必須使用類似 HttpPost 而不是 CURL,這是我的代碼:

    //Tried with full URL and by adding the registration as a header.
    //HttpPost httpPost = new HttpPost("https://beta.check-mot.service.gov.uk/trade/vehicles/mot-tests?registration=" + reg_selected);
     HttpPost httpPost = new HttpPost("https://beta.check-mot.service.gov.uk/trade/vehicles/mot-tests");

     httpPost.addHeader("Content-Type", "application/json");
     httpPost.addHeader("Accept", "application/json+v6");
     httpPost.addHeader("x-api-key", "abcdefgh123456");
     httpPost.addHeader("registration", reg_selected);

     StringEntity entity = new StringEntity(jsonObj.toString(), HTTP.UTF_8);
     httpPost.setEntity(entity);
     HttpClient client = new DefaultHttpClient();

     try {
          HttpResponse response = client.execute(httpPost);

          if (response.getStatusLine().getStatusCode() == 200) {

               InputStream inputStream = response.getEntity().getContent();
               bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
               String readLine = bufferedReader.readLine();

               String jsonStr = readLine;
               JSONObject myJsonObj = new JSONObject(jsonStr);

               
          }else if (response.getStatusLine().getStatusCode() == 400){
                 //Bad Request  Invalid data in the request. Check your URL and parameters
                 error_text = "Bad Request";
          }else if (response.getStatusLine().getStatusCode() == 403){
                //Unauthorised – The x-api-key is missing or invalid in the header
                error_text = "Authentication error"; //<<<< FAILS HERE 403
          }

response.getStatusLine().getStatusCode() 返回 • “403 – 未經授權 – x-api-key 在標頭中丟失或無效”。 但是,我使用的 x-api-key 與在線 CURL 測試一起正常工作,因此實際密鑰是正確的,但是我如何將其添加到我的 android 代碼請求中的方式必須無效或類似。

任何人都可以了解將 CURL 轉換為 Android java 以使服務器不返回 403 的正確方法嗎?

謝謝

使用Jsoup很容易做到:

    // CREATE CONNECTION
    Connection conn=Jsoup.connect("URL_GOES_HERE");
    
    // ADD POST/FORM DATA
    conn.data("KEY", "VALUE");
    
    // ADD HEADERS HERE
    conn.header("KEY", "VALUE");
    
    // SET METHOD AS POST 
    conn.method(Connection.Method.POST);
    
    // ACCEPT RESPONDING CONTENT TYPE
    conn.ignoreContentType(true);
    
    try
    {
        // GET RESPONSE
        String response = conn.execute().body();
        
        // USE RESPONSE HERE
        // CREATE JSON OBJECT OR ANYTHING...
    } catch(HttpStatusException e)
    {
        int status = e.getStatusCode();
        // HANDLE HTTP ERROR HERE
    } catch (IOException e)
    {
        // HANDLE IO ERRORS HERE
    }

Ps:我猜你對HeaderPost Data感到困惑。 密鑰等(憑據)必須用作Post Data和內容類型等,例如Header

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM