[
Date Prev][
Date Next][
Thread Prev][
Thread Next][
Date Index][
Thread Index]
[
List Home]
Re: [jetty-users] hi, why is there no response when i use the jetty http client?
|
On 12/08/2010 11:21 AM, beneo_7 wrote:
then i use testng write a dome, but i found there is no response when
i use get http://www.google.com, WHY??
Because you are using the default asynchronous behaviour of HttpClient,
which means the response is not available the way you are trying to call it.
http://docs.codehaus.org/display/JETTY/Jetty+HTTP+Client gives two ways
of using HttpExchange. You should use the first one, for example:
package test;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import org.eclipse.jetty.client.ContentExchange;
import org.eclipse.jetty.client.HttpClient;
import org.eclipse.jetty.client.HttpExchange;
import org.eclipse.jetty.util.thread.QueuedThreadPool;
public class HTTPClientTest {
public static void main(String[] args) {
try {
HttpClient client = new HttpClient();
client.setConnectorType(HttpClient.CONNECTOR_SELECT_CHANNEL);
client.setMaxConnectionsPerAddress(4); // max 200 concurrent connections
to every address
client.setThreadPool(new QueuedThreadPool(250)); // max 250 threads
client.setTimeout(1000); // 30 seconds timeout; if no server reply, the
request expires
client.start();
ContentExchange exchange = new ContentExchange()
{
// define the callback method to process the response when you get it back
protected void onResponseComplete() throws IOException
{
super.onResponseComplete();
String responseContent = this.getResponseContent();
int code = this.getResponseStatus();
// do something with the response content
System.out.println("response code " + code + ", response: (" +
responseContent + ")");
// if (code == HttpServletResponse.SC_FOUND) {
// System.out.println("looking for Location response header..");
// String location = this.getResponseFields().getStringField("Location");
// System.out.println("Moved to: " + location);
}
}
};
// Optionally set the HTTP method
exchange.setMethod("GET");
// Or, equivalently, this:
exchange.setURL("http://www.google.com/");
exchange.addRequestHeader("Host", "foo.com");
client.send(exchange);
System.out.println("Exchange sent - " + exchange.toString());
int exchangeState = exchange.waitForDone();
if (exchangeState == HttpExchange.STATUS_COMPLETED) {
System.out.println("STATUS_COMPLETED " + exchange.getStatus());
//System.out.println(exchange.getRequestContent());
} else if (exchangeState == HttpExchange.STATUS_EXCEPTED) {
} else if (exchangeState == HttpExchange.STATUS_EXPIRED) {
}
} catch (Exception e) {
System.out.println("ERROR: " + e + "( " + e.getMessage() + ")");
}
}
}
Note that I have commented out your getRequestContent() line, instead
creating an anonymous ContentExchange class with overridden
onResponseComplete function. I also added a 'Host' request header, as I
believe this is required for HTTP 1.1. For me, this prints:
Exchange sent - @3902281=GET//www.google.com:80/#1
response code 302, response: (<HTML><HEAD><meta
http-equiv="content-type" content="text/html;charset=utf-8">
<TITLE>302 Moved</TITLE></HEAD><BODY>
<H1>302 Moved</H1>
The document has moved
<A HREF="http://www.google.com/">here</A>.
</BODY></HTML>
)
The '302' response should come with a Location response header to
indicate where the resource can be found, but when I try to call
this.getResponseFields() and get the Location header, it hangs on me
(see commented out block where code == SC_FOUND). This is the first time
I've tried using these classes, so I can't really say why it hangs,
except that perhaps you have to provide a onResponseHeader() callback
function, like onResponseComplete(), that will capture the headers. See
the javadoc for HttpExchange at
http://download.eclipse.org/jetty/stable-7/apidocs/.
Nick