hc-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Lee Francis Wilhelmsen <le...@start.no>
Subject Posting XML over authenticated connection using SSL
Date Fri, 14 May 2004 12:24:43 GMT
Hi

This is my first posting to this list although I've been a subscriber 
for a while now. Anyway, I've been using HTTP client for some time and 
although it works it also fails too often and I don't know why.

I'm using HTTP Client version 2.0 final downloaded a few months back. 
I'm running on Sun JRE 1.4.2_03 on Win2000.

The task at hand is the following: I'm posting xml to a servlet on a IBM 
server. The server sends me xml back. The serlvet is protected by 
username/password and the connection is SSL. I have to parse the 
returned xml and read a special tag value to see if there is more data 
available. If there is then I have to establish a new connection and 
receive the next batch of xml data. This goes on until all data is 
returned at which point I send a receipt. The trouble I'm having is that 
the connection sometimes fails between batches and I have been getting a 
few different error messages.

Here's my code, does this look ok?

In the class constructor:
	client = new HttpClient(); // used for every call to connect()

In the class connect() method:

	logger.info("Making remote connection");
	client.setStrictMode(false);

         logger.debug("Setting username/password credentials");
         UsernamePasswordCredentials upc =
		new UsernamePasswordCredentials(username, password);
         HttpState httpState = client.getState();
         httpState.setCredentials(realm, host, upc);

         logger.debug("Making connection method object");
         HttpMethodBase method = requester.createHttpMethod(url);
         method.setDoAuthentication(true);

         logger.debug("Using DefaultMethodRetryHandler");
         DefaultMethodRetryHandler methodRetryHandler =
		new DefaultMethodRetryHandler();
	method.setMethodRetryHandler(methodRetryHandler);
         logger.debug("Method Retry count is " +
		methodRetryHandler.getRetryCount());

         logger.debug("Attempting to connect");

         final int LIMIT = 3;
         try {
         	for (int i=0; i<LIMIT; i++) {
                     try {
                         status = client.executeMethod(method);
                         logger.info("Executed!");
                         break;
                     } catch (HttpRecoverableException e) {
                         logger.error("Http exception", e);
                     } catch (IOException e) {
                         logger.fatal("IOException", e);
                     }
                 }
             } finally {
                 byte[] body = method.getResponseBody();
                 String charset = method.getRequestCharSet();
                 method.releaseConnection();

                 processContent(body, charset);
             }

             printRequestHeaders(method);
             printResponseHeaders(method);
             printCookieInfo(client.getState());
	} catch (...)

Does this look ok? It seems to work fine when executeMethod() succeeds, 
but when it fails there seems to be no way back.

Ok. From the wire trace it seems this is happening:

- I initiate a post to the server
- The server responds saying not authorized with a realm value
- HTTP Client then resends the post using the credentitals
   I have supplied (why doesn't it do this the first time?)
- The server responds with the result
- HTTP Client can't read the result because it can't find a
   line starting with HTTP.


The error I'm getting is this case is

org.apache.commons.httpclient.HttpRecoverableException: 
org.apache.commons.httpclient.HttpRecoverableException: Error in parsing 
the status  line from the response: unable to find line starting with "HTTP"
	at 
org.apache.commons.httpclient.HttpMethodBase.readResponse(HttpMethodBase.java:1965)
	at 
org.apache.commons.httpclient.HttpMethodBase.processRequest(HttpMethodBase.java:2659)
	at 
org.apache.commons.httpclient.HttpMethodBase.execute(HttpMethodBase.java:1093)
	at 
org.apache.commons.httpclient.HttpClient.executeMethod(HttpClient.java:675)
	at 
org.apache.commons.httpclient.HttpClient.executeMethod(HttpClient.java:529)



When I look at the wire trace it says:

DEBUG [main] httpclient.wire - >> "POST /sting/StingServlet 
HTTP/1.1[\r][\n]"
DEBUG [main] httpclient.wire - >> "User-Agent: Jakarta 
Commons-HttpClient/2.0final[\r][\n]"
DEBUG [main] httpclient.wire - >> "Host: e-torg.no.ihost.com[\r][\n]"
DEBUG [main] httpclient.wire - >> "Content-Length: 816[\r][\n]"
DEBUG [main] httpclient.wire - >> "Content-Type: 
application/x-www-form-urlencoded[\r][\n]"
DEBUG [main] httpclient.wire - >> "[\r][\n]"
DEBUG [main] httpclient.wire - >> 
"xmlInput=%3C%3Fxml+version%3D%22[stuffdeleted]3E%0A%3C%2FstingRequest%3E%0A"
DEBUG [main] httpclient.wire - << "HTTP/1.1 401 Unauthorized [\r][\n]"
DEBUG [main] httpclient.wire - << "Server: IBM HTTP Server/V5R3M0[\r][\n]"
DEBUG [main] httpclient.wire - << "Date: Fri, 14 May 2004 11:18:33 
GMT[\r][\n]"
DEBUG [main] httpclient.wire - << "Accept-Ranges: bytes[\r][\n]"
DEBUG [main] httpclient.wire - << "Content-Type: text/html; 
charset=IBM-1047[\r][\n]"
DEBUG [main] httpclient.wire - << "Content-Length: 282[\r][\n]"
DEBUG [main] httpclient.wire - << "Last-Modified: Fri, 14 May 2004 
11:18:33 GMT[\r][\n]"
DEBUG [main] httpclient.wire - << "Expires: Fri, 14 May 2004 11:18:33 
GMT[\r][\n]"
DEBUG [main] httpclient.wire - << "Pragma: no-cache[\r][\n]"
DEBUG [main] httpclient.wire - << "Cache-Control: no-cache[\r][\n]"
DEBUG [main] httpclient.wire - << "WWW-Authenticate: Basic 
realm="STING_Restricted"[\r][\n]"
DEBUG [main] httpclient.wire - << "IMW0254E 
<HTML><HEAD><TITLE>Error</TITLE></HEAD><BODY 
bgcolor="[0xfffd][0xfffd]FFF7E7"><H1>Error 401</H1>IMW0216E Not 
authorized.  Authentication failed.<P><HR><ADDRESS><A 
HREF="https://e-torg.no.ihost.com/" target="_top">IBM HTTP Server - 
North American Edition V5R3M0</A></ADDRESS></BODY></HTML"
DEBUG [main] httpclient.wire - >> "POST /sting/StingServlet 
HTTP/1.1[\r][\n]"
DEBUG [main] httpclient.wire - >> "User-Agent: Jakarta 
Commons-HttpClient/2.0final[\r][\n]"
DEBUG [main] httpclient.wire - >> "Host: e-torg.no.ihost.com[\r][\n]"
DEBUG [main] httpclient.wire - >> "Content-Length: 816[\r][\n]"
DEBUG [main] httpclient.wire - >> "Content-Type: 
application/x-www-form-urlencoded[\r][\n]"
DEBUG [main] httpclient.wire - >> "Authorization: Basic 
aTAweGRrOmVqYjl3aQ==[\r][\n]"
DEBUG [main] httpclient.wire - >> "[\r][\n]"
DEBUG [main] httpclient.wire - >> 
"xmlInput=%3C%3Fxml+version%3D%22[stuffdeleted]3E%0A%3C%2FstingRequest%3E%0A"
DEBUG [main] httpclient.wire - << ">HTTP/1.1 200 ok [\r][\n]"
DEBUG [main] httpclient.wire - << "Server: IBM HTTP Server/V5R3M0[\r][\n]"
DEBUG [main] httpclient.wire - << "Date: Fri, 14 May 2004 11:18:35 
GMT[\r][\n]"
DEBUG [main] httpclient.wire - << "Accept-Ranges: bytes[\r][\n]"
DEBUG [main] httpclient.wire - << "Transfer-Encoding: chunked[\r][\n]"
DEBUG [main] httpclient.wire - << "Content-Type: text/xml[\r][\n]"
DEBUG [main] httpclient.wire - << "Last-Modified: Fri, 14 May 2004 
11:18:35 GMT[\r][\n]"
DEBUG [main] httpclient.wire - << "Expires: Thu, 01 Dec 1994 16:00:00 
GMT[\r][\n]"
DEBUG [main] httpclient.wire - << "Set-Cookie: 
sesessionid=9Br13dKU6vHMOp6QVZGKGuJi;Path=/[\r][\n]"
DEBUG [main] httpclient.wire - << "Cache-Control: 
no-cache="set-cookie,set-cookie2"[\r][\n]"
DEBUG [main] httpclient.wire - << "[\r][\n]"
DEBUG [main] httpclient.wire - << "148E[\r][\n]"
DEBUG [main] httpclient.wire - << "<?xml version="1.0" 
encoding="UTF-8"?>[\r][\n]"
[xml contents from server follow]


Notice the funny ">" character before the second server response: "> 
HTTP/1.1 200 ok". This seems to be part of a HTML response from the 
first response further up the wire trace:

4000 [main] DEBUG httpclient.wire  - << "IMW0254E 
<HTML><HEAD><TITLE>Error</TITLE></HEAD><BODY 
bgcolor="[0xfffd][0xfffd]FFF7E7"><H1>Error 401</H1>IMW0216E Not 
authorized.  Authentication failed.<P><HR><ADDRESS><A 
HREF="https://e-torg.no.ihost.com/" target="_top">IBM HTTP Server - 
North American Edition V5R3M0</A></ADDRESS></BODY></HTML"

Notice there is no closing ">" character and this seems to be the cause 
of this particular problem.

Most of the code in this wire trace is done automatically by HTTP Client.

Why is this happening? Can anyone help?

Best regards
Lee Francis Wilhelmsen

---------------------------------------------------------------------
To unsubscribe, e-mail: commons-httpclient-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-httpclient-dev-help@jakarta.apache.org


Mime
View raw message