Java POST HTTP请求的内容长度应该使用什么值?
我正在尝试发送HTTP请求。目前我一直在添加内容长度属性:我有Java POST HTTP请求的内容长度应该使用什么值?,java,android,http,http-headers,Java,Android,Http,Http Headers,我正在尝试发送HTTP请求。目前我一直在添加内容长度属性:我有 httpPost.addHeader("Accept", "application/json"); httpPost.addHeader("Content-Type", "application/json;charset=utf-8"); httpPost.addHeader("Content-Length", ""+json.length() ); 若我注释最后一行(内容长度)服务器返回我错过smth的错误,我的最后一行将保持未
httpPost.addHeader("Accept", "application/json");
httpPost.addHeader("Content-Type", "application/json;charset=utf-8");
httpPost.addHeader("Content-Length", ""+json.length() );
若我注释最后一行(内容长度)服务器返回我错过smth的错误,我的最后一行将保持未注释状态—然后在尝试执行此操作时捕获异常
HttpResponse httpResponse = httpclient.execute(httpPost);
请告诉我设置标题有什么问题
PS我正在发送easy JSON对象
JSONObject jsonObject = new JSONObject();
jsonObject.accumulate("phone", phone);
我的post metod的完整代码片段:
public static JSONObject POST(String url, String phone){
InputStream inputStream = null;
String result = "";
try {
// 1. create HttpClient
HttpClient httpclient = new DefaultHttpClient();
// 2. make POST request to the given URL
HttpPost httpPost = new HttpPost(url);
String json = "";
// 3. build jsonObject
Log.d("ANT", "JSONObject jsonObject = new JSONObject(); PHONE:"+phone);
JSONObject jsonObject = new JSONObject();
jsonObject.accumulate("phone", phone);
// 4. convert JSONObject to JSON to String
json = jsonObject.toString();
// 5. set json to StringEntity
StringEntity se = new StringEntity(json);
// 6. set httpPost Entity
httpPost.setEntity(se);
Log.d("ANT", "json"+json.length());
// 7. Set some headers to inform server about the type of the content
httpPost.addHeader("Accept", "application/json");
httpPost.addHeader("Content-Type", "application/json;");
// httpPost.addHeader("Content-Length", ""+json.length() );
// 8. Execute POST request to the given URL
Log.d("ANT", "httpPost:"+getStringFromInputStream(httpPost.getEntity().getContent()));
for (Header s : httpPost.getAllHeaders())
Log.i("ANT", "header::"+s.getName() + ":"+s.getValue());
HttpResponse httpResponse = httpclient.execute(httpPost);
// 9. receive response as inputStream
inputStream = httpResponse.getEntity().getContent();
Log.d("ANT", "httpResponse.getEntity().getContent();");
// 10. convert inputstream to string
if (inputStream != null) {
result = getStringFromInputStream(inputStream);
Log.d("ANT", "getStringFromInputStream : "+result);
JSONObject obj = new JSONObject(result);
Log.d("ANT", "JSONObject");
return obj;
}
else
result = "Did not work!";
} catch (ClientProtocolException e) {
Log.d("ANT", "ClientProtocolException : "+ e.getLocalizedMessage());
} catch (IOException e) {
Log.d("ANT", "IOException:"+ e.getLocalizedMessage());
} catch (Exception e) {
Log.d("ANT", "Exception:"+ e.getLocalizedMessage());
}
// 11. return result
return null;
}
您能否详细说明服务器错误代码是什么以及异常发生时的堆栈跟踪 编辑: 我试过你的部分代码。我没有设置内容长度(因为这应该自动完成): 我使用了wireshark,可以验证以下请求是否会发送到百度: POST/HTTP/1.1
接受:应用程序/json
内容类型:application/json
内容长度:20
主持人:www.baidu.com
连接:保持活动状态
用户代理:Apache HttpClient/4.3.6(java 1.5) {“电话”:“+8912345”}
这表明请求已正确发送,这意味着客户端似乎没有问题。这就是为什么我建议检查服务器端的错误。正如我已经提到的,我认为您应该使用tcpdump或wireshark来检查客户端和服务器之间的通信量。这可能会让您更好地了解出了什么问题。您能否详细说明发生异常时的服务器错误代码和堆栈跟踪 编辑: 我试过你的部分代码。我没有设置内容长度(因为这应该自动完成): 我使用了wireshark,可以验证以下请求是否会发送到百度: POST/HTTP/1.1
接受:应用程序/json
内容类型:application/json
内容长度:20
主持人:www.baidu.com
连接:保持活动状态
用户代理:Apache HttpClient/4.3.6(java 1.5) {“电话”:“+8912345”}
这表明请求已正确发送,这意味着客户端似乎没有问题。这就是为什么我建议检查服务器端的错误。正如我已经提到的,我认为您应该使用tcpdump或wireshark来检查客户端和服务器之间的通信量。这可能会让您更好地了解出了什么问题。我有一个带有e.getLocalizedMessage()的ClientProtocolException:null;如果是httpPost.addHeader(“Content-Length”,即“+json.Length());没有注释。若为逗号,服务器将给出“:-31”,消息“:”操作被拒绝。请与我们联系“您能展示您如何形成post请求的代码片段吗?另外,json.length()是否返回大于0的值?此外,您还可以尝试使用tcpdump或wireshark验证您发送的请求是否确实包含正确的头和内容数据。json.length()返回24。将在一分钟内添加代码Great Answer,谢谢!我当然会搜索这些工具!我有ClientProtocolException和e.getLocalizedMessage():null;如果httpPost.addHeader(“Content Length”,即“+json.Length()”;未注释。如果是注释,服务器会给出“:-31”,“消息”:“操作被拒绝。请与我们联系”您能否向代码片段展示如何形成post请求?另外,json.length()是否返回大于0的字符串?此外,您还可以尝试使用tcpdump或wireshark验证您发送的请求是否确实包含正确的头和内容数据。json.length()实际返回24。将添加一分钟代码Great Answer,谢谢!我肯定会谷歌这些工具!
public class App
{
public static void main( String[] args )
{
post("http://www.baidu.com","+8912345");
}
public static JSONObject post(String url, String phone){
InputStream inputStream = null;
String result = "";
try {
// 1. create HttpClient
HttpClient httpclient = new DefaultHttpClient();
// 2. make POST request to the given URL
HttpPost httpPost = new HttpPost(url);
String json = "";
// 3. build jsonObject
JSONObject jsonObject = new JSONObject();
jsonObject.accumulate("phone", phone);
// 4. convert JSONObject to JSON to String
json = jsonObject.toString();
// 5. set json to StringEntity
StringEntity se = new StringEntity(json);
// 6. set httpPost Entity
httpPost.setEntity(se);
// 7. Set some headers to inform server about the type of the content
httpPost.addHeader("Accept", "application/json");
httpPost.addHeader("Content-Type", "application/json;");
// 8. Execute
HttpResponse httpResponse = httpclient.execute(httpPost);
// 9. receive response as inputStream
inputStream = httpResponse.getEntity().getContent();
} catch (ClientProtocolException e) {
System.out.println("ClientProtocolException : "+e.getLocalizedMessage());
} catch (IOException e) {
System.out.println("IOException:"+ e.getLocalizedMessage());
} catch (Exception e) {
System.out.println("Exception:"+ e.getLocalizedMessage());
}
return null;
}
}