dcsimg
Is HttpURLConnection can Solve my problem if SO how ?
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Anoop_KumarA
Posted On:   Wednesday, March 28, 2001 09:06 AM

How can make an HTTP Post request where my request is an XML page and the response is also XML

Re: Is HttpURLConnection can Solve my problem if SO how ?

Posted By:   Zachary_Hunter  
Posted On:   Wednesday, March 28, 2001 12:19 PM


Here's how to do it using just a URLConnection. It might not handle all the protocol specific errors, but should work fine fundimentally. Here's what you do:




//Create URL to post to
URL url = new URL("http://your.url.here");

//get connection to URL
URLConnection urlConn = url_.openConnection();

//prepare for writing request data
urlConn.setDoOutput(true);

//get output stream from URLCon
PrintWriter out = new PrintWriter(urlConn.getOutputStream());

//write the request data to stream
out.print(yourXMLRequestString);

//close the output connection
out.close();

//get response from post request
BufferedReader xmlIn = new BufferedReader(new InputStreamReader(urlConn.getInputStream()));

//prepare to read in content
StringBuffer response = new StringBuffer();
Strint line;

//read in all the xml
while((line = inXML.readLine()) != null)
{
response.append(sLine);
response.append("
");
}



Rather than reading the response into a StringBuffer, you could just use the input stream to create an InputSource to pass to a SAX parser's parse() method.

About | Sitemap | Contact