如何设置内容类型上的HttpURLConnection? [英] How to set Content Type on HttpURLConnection?
问题描述
你知道如何设置内容类型
的<一个href="http://developer.android.com/reference/java/net/HttpURLConnection.html">HttpURLConnection?
随着code是黑莓,我想在Android等效的:
Following code is on Blackberry and I want the Android equivalent:
connection.setRequestProperty("content-type", "text/plain; charset=utf-8");
connection.setRequestProperty("Host", "192.168.1.36");
connection.setRequestProperty("Expect", "100-continue");
是不是为Android?
Is it right for android?
请指教。
推荐答案
如果你真的想使用 HttpURLConnection的可以使用<一个href="http://developer.android.com/reference/java/net/URLConnection.html#setRequestProperty%28java.lang.String,%20java.lang.String%29">setRequestProperty类似的方法:
If you really want to use the HttpURLConnection you can use the setRequestProperty method like:
myHttpURLConnection.setRequestProperty("Content-Type", "text/plain; charset=utf-8");
myHttpURLConnection.setRequestProperty("Expect", "100-continue");
但是,如果我是你,我会考虑使用的Apache HTTP库。他们是高一点的层次和更容易使用。有了他们,你会做的东西,如:
However, if I were you I'd look into using the Apache HTTP libraries. They're a little higher-level and easier to use. With them you would do it with something like:
HttpGet get = new HttpGet("http://192.168.1.36/");
get.setHeader("Content-Type", "text/plain; charset=utf-8");
get.setHeader("Expect", "100-continue");
HttpResponse resp = null;
try {
HttpClient httpClient = new DefaultHttpClient();
resp = httpClient.execute(get);
} catch (ClientProtocolException e) {
Log.e(getClass().getSimpleName(), "HTTP protocol error", e);
} catch (IOException e) {
Log.e(getClass().getSimpleName(), "Communication error", e);
}
if (resp != null) {
// got a response, do something with it
} else {
// there was a problem
}
这篇关于如何设置内容类型上的HttpURLConnection?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!