java - HTTP POST 方法返回状态代码 404

标签 java android http-post

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

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

最佳答案

来自维基:

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.

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


如何通过 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) 

关于java - HTTP POST 方法返回状态代码 404,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21332003/

相关文章:

java - "if"语句的问题

java - 为什么Java中有些方法会修改原始值而有些则不会?

java - 快速简便地测试 OSGi 包的方法

java - 安卓。为什么 E/JavaBinder : FAILED BINDER TRANSACTION?

python - 无法使用请求从下一页中抓取姓名

asp.net - HTML 表单数据在 POST 时丢失

java - 在一个命令中提取多个 jar

android - 房间选择表达式

android - 如何检索Android日历中添加的所有提醒?

php - 在 javascript http post 请求后处理不同的 php 响应