如何在Android中的HTTP POST请求中设置选项标头和内容 [英] How to set the option Header and Content in HTTP POST Resquest in Android
问题描述
我在Android中使用HttpPost和HttpURLConnection发送带有以下标头和内容的请求:
I used HttpPost and HttpURLConnection in Android to send the request with following Headers and Contents:
标题:api_key:9a8akx8badkaxxxx,速度:0,声音:男,韵律:1,缓存控制:无缓存
Headers: api_key: 9a8akx8badkaxxxx, speed: 0, voice: male, prosody: 1, Cache-Control: no-cache
内容(内容类型:文本/纯文本):大家好
Contents (with Content-Type: text/plain): Hello everyone
这些是我的代码:*使用HttpPost:
These are my code: *Using the HttpPost:
HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost(requestURL);
post.addHeader("api_key", "9a8akx8badkaxxxx");
post.addHeader("speed", "0");
post.addHeader("voice", "male");
post.addHeader("prosody", "1");
post.addHeader("Cache-Control", "no-cache");
List <NameValuePair> nvps = new ArrayList <NameValuePair>();
nvps.add(new BasicNameValuePair("data[body]", "Hello everyone"));
AbstractHttpEntity ent= null;
ent = new UrlEncodedFormEntity(nvps, HTTP.UTF_8);
ent.setContentType("application/x-www-form-urlencoded; charset=UTF-8");
ent.setContentEncoding("UTF-8");
post.setEntity(ent);
post.setURI(new URI("http://api.openfpt.vn/text2speech/v4"));
HttpResponse response =client.execute(post);
*使用HttpURLConnection:
*Using the HttpURLConnection:
URL url = new URL(requestURL);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setDoOutput(true);
conn.setInstanceFollowRedirects(false);
conn.setRequestMethod("POST");
conn.setRequestProperty("api_key", "9a8akx8badkaxxxx");
conn.setRequestProperty("Content-Type", "text/plain");
conn.setRequestProperty("speed", "0");
conn.setRequestProperty("voice", "male");
conn.setRequestProperty("prosody", "1");
conn.setRequestProperty("Cache-Control", "no-cache");
然后我不知道将内容"Hello Everyone"放入HttpURLConnection的方法
and then I don't know the method to put the content "Hello everyone" into HttpURLConnection
结果总是错误.但是,当我在Firefox中使用附加的HttpRequester时,响应就可以了.请帮助我在Android中创建和设置Http请求
The result always error. But when I use the add-on HttpRequester in Firefox, the response is OK. please help me to create and set Http Request in Android
推荐答案
尝试使用此方法设置内容类型:
Try this to set the content-type:
HttpPost httppost = new HttpPost(builder.getUrl());
httppost.setHeader(HTTP.CONTENT_TYPE,
"text/plain;charset=UTF-8");
要设置任意标头,我认为应该这样做:
To set arbitrary headers, I believe it should be done like this:
httppost.setHeader("api_key", "9a8akx8badkaxxxx");
httppost.setHeader("speed", "0");
httppost.setHeader("voice", "male");
httppost.setHeader("voice", "male");
httppost.setHeader("prosody", "1");
httppost.setHeader("Cache-Control", "no-cache");
要发送数据,请为您的帖子字段选择一个名称(此处为Fn和IdDevice):
To send the data, choose a name for your post field (here Fn and IdDevice):
List<NameValuePair> nameValuePairs = new ArrayList<>(1);
// nameValuePairs.add(new BasicNameValuePair("Fn",function));
// nameValuePairs.add(new BasicNameValuePair("IdDevice",IdDevice));
编辑开始
nameValuePairs.add(new BasicNameValuePair("Content","Hello everyone"));
编辑结束
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
最后,执行发布请求:
// Execute HTTP Post Request
HttpResponse response = httpclient.execute(httppost);
这篇关于如何在Android中的HTTP POST请求中设置选项标头和内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!