Hello Roland,
Considering URL-1 as http://www.apache.org/ and URL-2 as
http://www.apache.org/foundation/faq.html, below is the piece of code.
As per the steps and advice, which i followed as said, but it is again
giving ResponseBody for URL-2 as the that of URL-1.
Am i missing something, please correct me.
Requirement: URL-2 is embeded in the page of URL-1. I need to simulate
the hyper-link(URL-2) clicking in the page of URL-1 using HTTPClient.
Please help,
Thanks in Advance,
Sanjeev Kumar Neemkar
Java Code used as below:
******************************
import java.io.IOException;
import org.apache.commons.httpclient.DefaultHttpMethodRetryHandler;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.params.HttpMethodParams;
public class HttpClientTutorial {
private static String url = "http://www.apache.org/";
public static void main(String[] args) {
// Create an instance of HttpClient.
HttpClient client = new HttpClient();
// Create a method instance.
GetMethod method1 = new GetMethod(url); //URL-1
//http://www.apache.org/foundation/faq.html
GetMethod method2 = new GetMethod(url + "foundation/faq.html"); //URL-2
// Provide custom retry handler is necessary
method1.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
new DefaultHttpMethodRetryHandler(3, false));
// Provide custom retry handler is necessary
method2.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
new DefaultHttpMethodRetryHandler(3, false));
try {
// Execute the method.
int statusCode = client.executeMethod(method1);
if (statusCode != HttpStatus.SC_OK) {
System.err.println("Method1 failed: " + method1.getStatusLine());
}
// Read the response body.
byte[] responseBody1 = method1.getResponseBody();
//Release the connection.
method1.releaseConnection();
// Deal with the response.
// Use caution: ensure correct character encoding and is not binary
// data
System.out.println("URL-1::" + new String(responseBody1));
////////////////////URL-2/////////////////////
// Execute the method.
int statusCode2 = client.executeMethod(method2);
if (statusCode2 != HttpStatus.SC_OK) {
System.err.println("Method2 failed: " + method2.getStatusLine());
}
// Read the response body.
byte[] responseBody2 = method1.getResponseBody();
//Release the connection.
method2.releaseConnection();
// Deal with the response.
// Use caution: ensure correct character encoding and is not binary
// data
System.out.println("URL-2::" + new String(responseBody2));
} catch (HttpException e) {
System.err.println("Fatal protocol violation: " + e.getMessage());
e.printStackTrace();
} catch (IOException e) {
System.err.println("Fatal transport error: " + e.getMessage());
e.printStackTrace();
} finally {
// Release the connection.
//method1.releaseConnection();
}
}
}
.......................................................................................................................................................................................................
On 2/21/06, Roland Weber <[EMAIL PROTECTED]> wrote:
>
> Hello Sanjeev,
>
> > My requirement is, to follow the hyper-link present in the response html
> > (Welcome/Home Page). I mean i want to click on test-hyper-link and get
> the
> > response.
> >
> > Is there any way to do this ?
>
> You've got the page. Scan it for the hyperlink to get the new URL.
>
> > Link URL on Welcome Page:
> >
> https://abcdev.test.com/ABCProject/servlet/PageDirector?choice=upload&userRole=ABCUser
>
> That link looks so static you can probably hard-wire it into your
> application.
>
> > It can be Get Method or Post Method in real scenario.
>
> How is that? It's either the action attribute of a form, then the FORM
> says
> which one it is. Or it is not in a form, then it is a GET request. If the
> same URL is accessed both ways, then the server doesn't care which one it
> is.
>
> > Our current understanding is we have to use same method which we used
> for
> > first request/login.
>
> If you mean "method object", then you are wrong. HttpClient method objects
> can not be re-used, or at least should not. There were some methods for
> re-using, but it is rather pointless to try. Just create a new one.
>
> > Becuase it is storing the actual connection and session.
>
> The connection is kept in the method so you can release it when you're
> done with the response. The connection manager will try to keep the
> connection alive for the next request, but that is strictly a performance
> optimization.
> The session is in no way related to a connection. Session cookies are
> stored
> in the HttpState. If you didn't create an extra HttpState, the default
> state
> in the HttpClient will be used.
>
> I recommend that you create a new method object to access the second link,
> and execute it with the same HttpClient object as the first one. Don't
> forget to release the connection after each request, but not before you're
> done with reading the response.
>
> hope that helps,
> Roland
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
>
>