Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

XML/http client applet

Status
Not open for further replies.

AndrewChandler

Programmer
Jul 2, 2003
6
GB
I'm trying to build an XML client which communicates over http with a remote server, using the HttpURLConnection class, but believe that there are problems in the way I am POSTing the data or trying to read the response. The current code is

Code:
    try {
		String xml = BuildXML();
		byte data [] = xml.getBytes();


		URL url = new URL( "[URL unfurl="true"]http://myhost/XMLserver/default.asp"[/URL] );
		HttpURLConnection urlcon = (HttpURLConnection)url.openConnection();
		urlcon.setRequestMethod("POST");
		urlcon.setRequestProperty( "Content-Type", "text/xml" );
		urlcon.setRequestProperty( "Content-Lanuguage", "en-US" );
		urlcon.setDoInput( true );
		urlcon.setDoOutput( true );
		urlcon.connect();
		OutputStream os = urlcon.getOutputStream();
			
		os.write(data);
		os.close();


		/* line is used by the paint method */
		/* line = "Response = " + urlcon.getResponseMessage(); */
		line = "Response = " + urlcon.getContent(); 
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
    }

Can someone point me in the right direction?
 
>> there are problems

What problems?

Please read the FAQ: "5 Steps to asking a question": faq333-3811

Now just to guess at a few.

1) Java is case sensitive, therefore httpURLConnection is NOT equal to HttpURLConnection

2) urlcon.setRequestProperty( "[red]Content-Lanuguage[/red]", "en-US" );

3) If your sending a body in a POST request you need a "content-length" HTTP header.

I suspect there are others but that should give you a start.


-pete
 
Pete: Thx for the response - all comments taken on board. The POST now works fine - code is:

try
{
String xml = BuildXML();
byte data [] = xml.getBytes();

URL url = new URL( " );
HttpURLConnection urlcon = (HttpURLConnection)url.openConnection();
urlcon.setRequestMethod("POST");
urlcon.setRequestProperty( "Content-Type", "text/xml" );
urlcon.setRequestProperty( "Content-Language", "en-US" );
urlcon.setDoInput( true );
urlcon.setDoOutput( true );
urlcon.connect();
OutputStream os = urlcon.getOutputStream();

os.write(data);
os.close();

line = "Response Code = " + urlcon.getResponseCode()
+ " --- Response Message = " + urlcon.getResponseMessage()
+ " --- ContentType = " + urlcon.getContentType()
+ " --- ContentLength = " + urlcon.getContentLength();
}

Which delivers:

Response Code = 200 -- Response Message = OK -- ContentType = text/xml; charset="UTF-8" -- ContentLength = 5670

The question now is how do extract the content of the response as a string - I suspect its contained in the object getContent()...
 
BufferedReader in = new BufferedReader(new InputStreamReader(urlcon.getInputStream()));
String inputline = "";
FileOutputStream fos = new FileOutputStream("somefile.txt");
int b = 0;
while ((b = in.read()) != -1) {
fos.write(b);
}
fos.flush();
fos.close();
in.close();
urlcon.disconnect();

Obviously the FileOutputStream could be any kind of output stream ...
 
Thanks - the following got what I needed:

String inputline;
BufferedReader in = new BufferedReader(new InputStreamReader(urlcon.getInputStream()));

inputline = in.readLine();
in.close();
urlcon.disconnect();
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top