Java:将原始数据添加到有效负载 Httpost 请求
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20094042/
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
Java: Adding raw data to payload Httpost request
提问by James Mclaren
I intend to send a simple http post request with a large string in the Payload.
我打算在 Payload 中发送一个带有大字符串的简单 http post 请求。
So far I have the following.
到目前为止,我有以下内容。
DefaultHttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("address location");
String cred = "un:pw";
byte[] authEncBytes = Base64.encodeBase64(cred.getBytes());
String authStringEnc = new String(authEncBytes);
httppost.setHeader("Authorization","Basic " + authStringEnc);
However, I do not know how to attach a simple RAW string into the payload. The only examples I can find are name value pairs into the Entity but this is not what I want.
但是,我不知道如何将简单的 RAW 字符串附加到有效负载中。我能找到的唯一示例是实体中的名称值对,但这不是我想要的。
Any assistance?
任何帮助?
回答by isnot2bad
It depends on the concrete HTTP-API you're using:
这取决于您使用的具体 HTTP-API:
Commons HttpClient (old - end of life)
Commons HttpClient(旧 - 生命周期结束)
Since HttpClient 3.0 you can specify a RequestEntity
for your PostMethod
:
从 HttpClient 3.0 开始,您可以RequestEntity
为您的指定一个PostMethod
:
httpPost.setRequestEntity(new StringRequestEntity(stringData));
Implementations of RequestEntity
for binary data are ByteArrayRequestEntity
for byte[]
, FileRequestEntity
which reads the data from a file (since 3.1) and InputStreamRequestEntity
, which can read from any input stream.
RequestEntity
for 二进制数据的实现是ByteArrayRequestEntity
for byte[]
,FileRequestEntity
它从文件中读取数据(自 3.1 起)和InputStreamRequestEntity
,它可以从任何输入流中读取。
Before 3.0 you can directly set a String
or an InputStream
, e.g. a ByteArrayInputStream
, as request body:
在 3.0 之前,您可以直接设置 aString
或 an InputStream
,例如 a ByteArrayInputStream
,作为请求正文:
httpPost.setRequestBody(stringData);
or
或者
httpPost.setRequestBody(new ByteArrayInputStream(byteArray));
This methods are deprecated now.
现在不推荐使用此方法。
HTTP components (new)
HTTP 组件(新)
If you use the newer HTTP componentsAPI, the method, class and interface names changed a little bit, but the concept is the same:
如果使用较新的HTTP 组件API,方法、类和接口名称会发生一些变化,但概念是相同的:
httpPost.setEntity(new StringEntity(stringData));
Other Entity
implementations: ByteArrayEntity
, InputStreamEntity
, FileEntity
, ...
其他Entity
实现:ByteArrayEntity
, InputStreamEntity
, FileEntity
, ...
回答by Adnan Abdollah Zaki
i was making a common mistake sequence of json object was wrong. for example i was sending it like first_name,email..etc..where as correct sequence was email,first_name
我犯了一个常见的错误 json 对象序列是错误的。例如我发送它像 first_name,email..etc.. 正确的顺序是 email,first_name
my code
我的代码
boolean result = false;
HttpClient hc = new DefaultHttpClient();
String message;
HttpPost p = new HttpPost(url);
JSONObject object = new JSONObject();
try {
object.put("updates", updates);
object.put("mobile", mobile);
object.put("last_name", lastname);
object.put("first_name", firstname);
object.put("email", email);
} catch (Exception ex) {
}
try {
message = object.toString();
p.setEntity(new StringEntity(message, "UTF8"));
p.setHeader("Content-type", "application/json");
HttpResponse resp = hc.execute(p);
if (resp != null) {
if (resp.getStatusLine().getStatusCode() == 204)
result = true;
}
Log.d("Status line", "" + resp.getStatusLine().getStatusCode());
} catch (Exception e) {
e.printStackTrace();
}
return result;