Java HTTP POST 方法返回状态码 404

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21332003/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 08:06:32  来源:igfitidea点击:

HTTP POST method returning status code 404

javaandroidhttp-post

提问by Braj

When I execute an API through following method, I always get 404 as response code.

当我通过以下方法执行 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;
}

I am not getting where am I going wrong. Can anybody please help me out?

我不明白我哪里出错了。有人可以帮我吗?

采纳答案by marcinj

from wiki:

来自维基:

The 404 or Not Found error message is a HTTP standard response code indicating that the client was able to communicate with the server, but the server could not find what was requested.

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

so, your code is OK, but server cannot find resource you are looking for. Double check if your url is correct.

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



how to pass request through fiddler proxy for debugging purposes:

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

  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) 

回答by MilanG

I was getting 404 for POST requests because mod_headers module of Apache 2 server was not enabled. If that happens you can enable it with:

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

sudo a2enmod headers

and then restart apache:

然后重启apache:

sudo service apache2 restart