2

在我的应用程序中,我正在尝试使用 POST 请求获取响应。响应服务器以 Json 格式发送给我。但在添加属性后,它返回我的响应代码为 411(即内容长度问题)。我已经添加了内容长度。那么我没有得到的问题在哪里。这是我的代码:

String url = "https://xxx:8243/people/v3";
STRURL = url + HttpComm.getConnectionString().trim();

StringBuffer postData = new StringBuffer();
HttpConnection httpConnection = null;
try {
    httpConnection = (HttpConnection) Connector.open(STRURL);
} catch (IOException e1) {
    e1.printStackTrace();
};

try {
    httpConnection.setRequestMethod("POST");
    postData.append("?username="+user);
    postData.append("&password="+password);
    String encodedData = postData.toString();
    byte[] postDataByte = postData.toString().getBytes("UTF-8");
    httpConnection.setRequestProperty("Authorization", "bearer"+"ZWOu3HL4vwaOLrFAuEFqsxNQf6ka");
    httpConnection.setRequestProperty("Content-Type","application/json");
    httpConnection.setRequestProperty("Content-Length", String.valueOf(postDataByte.length));
    OutputStream out = httpConnection.openOutputStream();
    DataOutputStream dos = new DataOutputStream(out);
    out.write(postData.toString().getBytes());
    out.flush();
    int statusCode = httpConnection.getResponseCode();
    Logger.out("HttpComm", "status code:::::::   "+statusCode);
    if (statusCode != HttpConnection.HTTP_OK)
    {

    }

更新代码:

HttpConnection httpConnection = null;
    try {
        httpConnection = (HttpConnection) Connector.open(STRURL);
    } catch (IOException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    };
            try {
                httpConnection.setRequestMethod("POST");
                URLEncodedPostData postData = new URLEncodedPostData("UTF-8", false);
                 postData.append("username", user);
                 postData.append("password", password);
                 byte[] postDataByte = postData.getBytes();
                  httpConnection.setRequestProperty("Authorization", "bearer"+"ZWOu3HL4vwaOLrFAuEFqsxNQf6ka");
                   httpConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
                   httpConnection.setRequestProperty("Content-Length", String.valueOf(postDataByte.length));
                   OutputStream out = httpConnection.openOutputStream(); 
                   out.write(postDataByte);
                   out.flush();

                   int statusCode = httpConnection.getResponseCode();
                   Logger.out("HttpComm", "status code:::::::   "+statusCode);
4

1 回答 1

1

这里有一些看起来不太对劲的东西。我建议试试这个:

 httpConnection.setRequestMethod("POST");

 URLEncodedPostData postData = new URLEncodedPostData("UTF-8", false);
 postData.append("username", user);
 postData.append("password", password);
 byte[] postDataByte = postData.getBytes();

 httpConnection.setRequestProperty("Authorization", "bearer"+"ZWOu3HL4vwaOLrFAuEFqsxNQf6ka");
 httpConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
 httpConnection.setRequestProperty("Content-Length", String.valueOf(postDataByte.length));

 OutputStream out = httpConnection.openOutputStream(); 
 DataOutputStream dos = new DataOutputStream(out);
 out.write(postDataByte);
 out.flush();

 int statusCode = httpConnection.getResponseCode();
 Logger.out("HttpComm", "status code:::::::   "+statusCode);
 if (statusCode != HttpConnection.HTTP_OK)

我改变了什么:

  1. 正如@samlewis 所说,代码正在创建一个变量来保存发布数据字节,但在调用out.write().

  2. 代码将内容类型设置为 JSON,但它没有发送JSON。请求只是两个参数。响应可能是 JSON,但您没有在请求的Content-Type参数中指定它。

  3. 用户名/密码参数仅使用字符串进行编码。通常,最好使用URLEncodedPostData来保存您的 POST 参数

  4. 如果您要使用字符串,我认为?在用户名参数的前面添加 a 仍然是不正确的。如果要对GETURL 中的参数进行编码,则使用https://xxx:8243/people/v3?username=user&password=password. 但是,这段代码使用的是 POST,而不是 GET。

  5. 还有一个未使用的encodedData变量。

于 2013-06-18T22:23:00.363 回答