hc-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From aruba bulk <b...@jacquesbotha.net>
Subject cookie question
Date Mon, 22 Dec 2003 15:23:56 GMT
Hi there

I'm using HttpClient to write a Cactus unit test. I'd
like to simulate a browser, sending a cookie to the
server with a request. So the cookie originates at the
client side :

HttpClient client = new HttpClient();
HttpMethod method = new getMethod(url);
String cookieValue = "testValue";
String cookieName = "testname";
Cookie cookie = new Cookie("localhost", cookieName,
cookieValue);
cookie.setPath("/");

HttpState httpState=new HttpState();
httpState.addCookie(cookie);
client.setState(httpState);

// Execute the method.
int statusCode = -1;
try {
statusCode = client.executeMethod(method);
  } catch (HttpRecoverableException e) {
	dynamoRequest.logDebug(
	  "A recoverable exception occurred, retrying." 		 
e.getMessage());			  } catch (IOException e) {
				dynamoRequest.logDebug("Failed to download
file.");
				e.printStackTrace();
				fail();
			  }
			}
			// Check that we didn't run out of retries.
			if (statusCode == -1) {
				dynamoRequest.logDebug("Failed to recover from
exception.");
			  	fail();
			}

			// Read the response body.
			byte[] responseBody = method.getResponseBody();
			
			// print out response headers
			Header[] responseHeaders =
method.getResponseHeaders();
			for (int i=0;i<responseHeaders.length; i++) {
				dynamoRequest.logDebug("response header '" +
responseHeaders[i].getName() +
					" has value : '" + responseHeaders[i].getValue()
+ "'");
			}
			
			// print out cookies
			Cookie[] cookies = httpState.getCookies();
			for (int i=0;i<cookies.length; i++) {
				dynamoRequest.logDebug("httpstate cookie '" +
cookies[i].getName() +
										" has value : '" + cookies[i].getValue() +
"'");
			}
			
						
			// Release the connection.
			method.releaseConnection();
	
			// Deal with the response.
			// Use caution: ensure correct character encoding
and is not binary data
			dynamoRequest.logDebug(new String(responseBody));
			
		}
	}


=====
"All one's life is music, if one touches the notes rightly, and in time."
    - John Ruskin

---------------------------------------------------------------------
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