3

How to attach XML file in message body of HTTP post request?

can any one give example for that kind

Paresh Mayani
  • 127,700
  • 71
  • 241
  • 295
Sanket Kachhela
  • 10,861
  • 8
  • 50
  • 75
  • plz refer it....http://code.google.com/apis/calendar/data/2.0/developers_guide_protocol.html#CreatingCalendars – Sanket Kachhela Oct 12 '11 at 09:36
  • i am making google calendar ...and i use Oauth for authorization and i m getting list of event ...and i want to add event in google calendar ...so i have to send HTTP post...and URL is https://www.google.com/calendar/feeds/default/owncalendars/full – Sanket Kachhela Oct 12 '11 at 09:53
  • sorry .. but i don't know how to send XML file in message body of HTTP post ..so i not start for HTTP post..thnx – Sanket Kachhela Oct 12 '11 at 10:04
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/4192/discussion-between-sanket-kachhela-and-pm-paresh-mayani) – Sanket Kachhela Oct 12 '11 at 10:07

1 Answers1

8

Here is a sample code snippet for making HTTP POST request on the desired URL:

try { 

String myXML = "<? xml version=1.0> <Request> <Elemtnt> <data id=\"1\">E1203</data> <data id=\"2\">E1204</data> </Element> </Request>";

HttpClient httpClient = new DefaultHttpClient(); 
HttpContext localContext = new BasicHttpContext(); 
HttpPost httpPost = new HttpPost("https://www.google.com/calendar/feeds/default/owncalendars/full"); 

List<NameValuePair> dataToPost = new ArrayList<NameValuePair>(); 

dataToPost.add(new BasicNameValuePair("yourxml", myXML)); 

httpPost.setEntity(new UrlEncodedFormEntity(dataToPost)); 

HttpResponse response = httpClient.execute(httpPost, localContext); 

} catch (Exception e) { 
e.printStackTrace(); 
}
Paresh Mayani
  • 127,700
  • 71
  • 241
  • 295