繁体   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