On Wed, 2011-03-09 at 07:21 -0800, Desholo wrote:
> Hello everyone!
> I'm new with HttpClient package so my question could be found silly, but I
> searched a solution for some time with no answer.
> I've to do an application that performs a POST request in a certain host,
> then navigates some pages, finds and retrieves some data and waits for
> instructions.
> I've found lots of examples about POST request but no one seems to work.
> After the request always get back the form page, as the server doesn't
> receive or ignore the POST.
> I'm using the last version of HttpClient (4.1).
> This is the code I'm using to send the POST request (is the one in the
> Apache site [
> http://hc.apache.org/httpcomponents-client-ga/httpclient/examples/org/apache/http/examples/client/ClientFormLogin.java
> link ]):
> 
>       DefaultHttpClient httpclient = new DefaultHttpClient();
> 
>         List<Cookie> cookies = httpclient.getCookieStore().getCookies();
>         
>         HttpPost httpost = new
> HttpPost("http://localhost/prova/index.html";);
> 
>         List <NameValuePair> nvps = new ArrayList <NameValuePair>();
>         nvps.add(new BasicNameValuePair("user", "prova"));
>         nvps.add(new BasicNameValuePair("password", "prova"));
> 
>         httpost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));
> 
>         HttpResponse response = httpclient.execute(httpost);
>         HttpEntity entity = response.getEntity();
> 
>         System.out.println("Login form get: " + response.getStatusLine());
>         if (entity != null) {
>             entity.consumeContent();
>         }
> 
>         System.out.println("Post logon cookies:");
>         cookies = httpclient.getCookieStore().getCookies();
>         if (cookies.isEmpty()) {
>             System.out.println("None");
>         } else {
>             for (int i = 0; i < cookies.size(); i++) {
>                 System.out.println("- " + cookies.get(i).toString());
>             }
>         }
> 
> I'd like to know if the code I'm using is correct, how to retrieve the HTML
> code of the target page of the form and if I can continue to navigate the
> site without the session expires.


Login related questions are notoriously difficult as different sites
tend to handle authentication differently. It is very unlikely you are
going to get any help unless you are prepared to do most of the work by
yourself: (1) capture HTTP sessions generated by a browser during the
authentication process using a packet sniffer or a browser plugin (2)
simulate the process using HttpClient.

Oleg 


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to