51

Do you know how to set Content-Type on HttpURLConnection?

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");

Is it right for android?

Please advise.

Jeremy Logan
  • 47,151
  • 38
  • 123
  • 143
AndroiDBeginner
  • 3,571
  • 11
  • 40
  • 43
  • Was looking for the right header to specify for a GET request, so got my question answered by looking at your question. – Grubsnik Nov 12 '10 at 09:20
  • Hi, I have a question related to your topic... can you tell me some general idea on how "connection.setRequestProperty("Expect", "100-continue");" affects your procedure? Do you need like... wait for a 100 response, then do some other operation, and then wait for a 200 response? – Josh Dec 12 '15 at 19:05

2 Answers2

75

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");

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
}
Jeremy Logan
  • 47,151
  • 38
  • 123
  • 143
  • 10
    Really as recommended you should use the UrlConnection as suggested by Jesse Wilson - http://android-developers.blogspot.com/2011/09/androids-http-clients.html – Chris.Jenkins Oct 17 '11 at 09:31
  • 1
    Note future people, @Chris.Jenkins is right. As of the time of this answer the apache libs were superior, but that changed in subsequent releases. – Jeremy Logan Apr 03 '18 at 20:42
  • How can I send scope in http post method in java. I need to generate token by sending scope, id password and credentials. I am not able to get any code –  Jun 27 '20 at 21:42
16
connection.setRequestProperty("Content-Type", "VALUE");
Rites
  • 2,242
  • 5
  • 28
  • 44
  • Thanks for quick reply. I have a question ? I am trying to create http GET connection : connection.setRequestProperty("content-type", "text/plain; charset=utf-8"); connection.setRequestProperty("Host", "192.168.1.36"); connection.setRequestProperty("Expect", "100-continue"); Is it right for android ? – AndroiDBeginner Dec 22 '09 at 10:17
  • Don't have much knowledge of android. Also, See the comment on your ques. – Rites Dec 22 '09 at 10:21