【问题标题】:HTTP POST method returning status code 404HTTP POST 方法返回状态码 404
【发布时间】:2014-01-24 11:53:59
【问题描述】:

当我通过以下方法执行 API 时,总是得到 404 作为响应码。

private void execute() throws IllegalStateException, IOException, NoSuchAlgorithmException {

    Map<String, String> comment = new HashMap<String, String>();
    comment.put("accounts-groups", "customers/enterprise");
    comment.put("companyType", "customer");
    comment.put("companyName", "Test");
    String json = new GsonBuilder().create().toJson(comment, Map.class);
    Log.i(TAG, "json : "+json);

    HttpResponse response = makeRequest(URL, json);

    /*Checking response */
    if(response != null) {
        InputStream inputStream = response.getEntity().getContent(); //Get the data in the entity
        int statusCode = response.getStatusLine().getStatusCode();
        Log.i(TAG, "statusCode : "+statusCode);
        String result;
        // convert inputstream to string
        if(inputStream != null)
            result = convertStreamToString(inputStream);
        else
            result = "Did not work!";

        Log.i(TAG, "result : "+result);
    }
}

private HttpResponse makeRequest(String uri, String json) throws NoSuchAlgorithmException {
    Log.i(TAG, "uri : "+uri);
    try {
        HttpPost httpPost = new HttpPost(uri);
        httpPost.setEntity(new StringEntity(json, HTTP.UTF_8));

        long timestamp = System.currentTimeMillis();

        String signatureKey = PRIVATE_KEY + timestamp;

        byte[] bytesOfMessage = signatureKey.getBytes(HTTP.UTF_8);

        MessageDigest md = MessageDigest.getInstance("MD5");
        byte[] thedigest = md.digest(bytesOfMessage);
        char[] signature = Hex.encodeHex(thedigest);

        String finalSignature = String.valueOf(signature);

        Log.i(TAG, "finalSignature : "+finalSignature);

        httpPost.setHeader("Timestamp", ""+timestamp);
        httpPost.setHeader("Api_token", API_TOKEN);
        httpPost.setHeader("Signature" , finalSignature);

        httpPost.setHeader("Accept", "application/json");
        httpPost.setHeader(HTTP.CONTENT_TYPE, "application/json");          

        return new DefaultHttpClient().execute(httpPost);
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return null;
}

我不知道我哪里出错了。有人可以帮帮我吗?

【问题讨论】:

    标签: java android http-post


    【解决方案1】:

    来自维基:

    404 或 Not Found 错误消息是 HTTP 标准响应代码 表明客户端能够与服务器通信, 但服务器找不到请求的内容。

    所以,您的代码没问题,但服务器找不到您正在寻找的资源。仔细检查您的网址是否正确。


    如何通过 fiddler 代理传递请求以进行调试:

      HttpParams params = new BasicHttpParams();
    
      // ....
    
      HttpHost proxy = new HttpHost("192.168.1.12", 8888); // IP to your PC with fiddler proxy
      params.setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);
    
      // use params as a second parameter to: following constructor:
      // public DefaultHttpClient (ClientConnectionManager conman, HttpParams params) 
    

    【讨论】:

    • 网址正确。他们在 apex 代码中使用的相同 url 并且工作正常。不知道java代码有什么问题。
    • 我会在 windows 上使用 fiddler 调试这个请求,它会准确地显示服务器的内容以及返回的内容 - 这也允许保存此类请求并在桌面上进行测试。
    • HTTPHost 的第一个参数是我的系统 IP 地址,请告诉我什么是“参数”n 如何获取它?我在 ubuntu 上
    • 关于如何设置参数的更新答案,不幸的是我不知道 linux 上有任何 Fiddler2 替代品
    • 服务器出现问题,所以它响应的所有内容都是 404。
    【解决方案2】:

    我收到 404 的 POST 请求,因为 Apache 2 服务器的 mod_headers 模块未启用。如果发生这种情况,您可以通过以下方式启用它:

    sudo a2enmod headers
    

    然后重启apache:

    sudo service apache2 restart
    

    【讨论】:

      猜你喜欢
      • 2017-07-29
      • 2021-09-19
      • 2019-02-24
      • 2015-01-12
      • 2021-01-13
      • 2016-09-16
      • 2015-02-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多