I am having difficulty POSTing and reading from a web server from GAE.
I can authenticate but the webserver does not accept the XML file I am
POSTing (which includes a query). I am not running the webserver in
question, so I have no insight into what might be wrong at that end
(or set it up differently).

First, a sanitized snippet of the curl statement that returns the
desired result (on OSX):
$ curl http://targetwebserver.org/servlet -u username:password -d '<?
xml version="1.0" encoding="UTF-8" standalone="yes"?><Tag> .... </
Tag>'
That to me confirm the webserver works alright.

Below now a snippet of the code (sanitized and rough) that I cannot
get to work. Again, authentication seems to work (I am not getting
responseCode 401), but whatever I have tried, I cannot get over
responseCode 400 and a corresponding boilerplate error message from
the webserver.

<--------- snip ------->
String requestString = "<?xml version="1.0" encoding="UTF-8"
standalone="yes"?><Tag> .... </Tag>"; // Also tried all sorts of
encoding, no luck
URL url = new URL("targetwebserver.org/servlet ");
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
conn.setRequestMethod("POST");
conn.setDoOutput(true);
conn.setDoInput(true);
conn.addRequestProperty("Content-type", "text/xml"); // tried
"application/xml" as well, no luck

// Authenticate (seems to work OK)
String authData = "username:password";
byte[] encodedAuthData = new
org.apache.commons.codec.binary.Base64().encode (authData.getBytes());
String authString = "Basic ";
for (int i = 0; i < encodedAuthData.length; i++)
        authString += (char)encodedAuthData[i];
conn.addRequestProperty ("Authorization", authString);

// Post request
PrintWriter pw = new PrintWriter(conn.getOutputStream()); // tried
OutputStreamWriter as well, no luck
pw.println(requestString);
pw.close();

responseCode = conn.getResponseCode();
if (responseCode == 200) {
        InputStream in = conn.getInputStream();
        BufferedReader rd = new BufferedReader(new
InputStreamReader(conn.getInputStream()));
        String line; while ((line = rd.readLine()) != null)
{ log.info(line); }
}
<--------- snip ------->

Please have a look at this - help's appreciated!

-- 
You received this message because you are subscribed to the Google Groups 
"Google App Engine for Java" group.
To post to this group, send email to google-appengine-j...@googlegroups.com.
To unsubscribe from this group, send email to 
google-appengine-java+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/google-appengine-java?hl=en.

Reply via email to