1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

I have a problem with commons-httpclient-3.1

Discussion in 'Java' started by osa, Oct 13, 2007.

  1. osa

    osa New Member

    Joined:
    Oct 13, 2007
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    I have a problem with commons-httpclient-3.1.
    When i try to get some page, cookies do not come to me.
    There is no error or exception message. Just null text.
    What could you advise me?
    Code:
    PHP:
    import java.net.*;
    import java.io.*;
    import java.util.*;

    import org.apache.commons.httpclient.Cookie;
    import org.apache.commons.httpclient.DefaultHttpMethodRetryHandler;
    import org.apache.commons.httpclient.Header;
    import org.apache.commons.httpclient.HttpClient;
    import org.apache.commons.httpclient.HttpException;
    import org.apache.commons.httpclient.HttpMethod;
    import org.apache.commons.httpclient.HttpState;
    import org.apache.commons.httpclient.HttpStatus;
    import org.apache.commons.httpclient.HttpVersion;
    import org.apache.commons.httpclient.NameValuePair;
    import org.apache.commons.httpclient.SimpleHttpConnectionManager;
    import org.apache.commons.httpclient.cookie.CookiePolicy;
    import org.apache.commons.httpclient.cookie.CookieSpec;
    import org.apache.commons.httpclient.methods.GetMethod;
    import org.apache.commons.httpclient.methods.PostMethod;
    import org.apache.commons.httpclient.params.HttpClientParams;
    import org.apache.commons.httpclient.params.HttpMethodParams;
    import org.apache.commons.httpclient.params.HttpParams;




    public class 
    GetUrl {
    private    
    Cookie[]  CookieString;
    HttpClient client = new HttpClient();
    HttpClientParams params = new HttpClientParams();
    HttpState initialState = new HttpState();
    public 
    GetUrl()
    {

          
    }
    public 
    String get(String url_name)
    {

         
    params.setParameter(HttpMethodParams.USER_AGENT"Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.7) Gecko/20070914 Firefox/2.0.0.7");
         
    params.setCookiePolicy(CookiePolicy.DEFAULT);
         
        
    initialState.addCookies(CookieString);
        
    client.setState(initialState);
        
    client.setParams(params);
        
    HttpMethod method null;
        
         
    method = new GetMethod(url_name);
         
    method.setFollowRedirects(true);
         try {
            
    client.executeMethod(method);
        } catch (
    HttpException e) {
            
            
    e.printStackTrace();
        } catch (
    IOException e) {
        
            
    e.printStackTrace();
        }
         
    String responseBody null;
         try {
            
    responseBody method.getResponseBodyAsString();
            
            
        } catch (
    IOException e) {
            
            
    e.printStackTrace();
        }
        
        
    /*System.out.println("Present cookies: ");
         for (int i = 0; i < header.length; i++) {
             System.out.println(" - " + header[i].getName());
         }*/
        
    CookieString =  initialState.getCookies();
         
    // Display the cookies
         
    System.out.println("Present cookies: ");
         for (
    int i 0CookieString.lengthi++) {
             
    System.out.println(" - " CookieString[i].toExternalForm());
         }
        
    System.out.println(responseBody);
        return 
    responseBody;
      }


            
        
    }
     
  2. pradeep

    pradeep Team Leader

    Joined:
    Apr 4, 2005
    Messages:
    1,646
    Likes Received:
    86
    Trophy Points:
    0
    Occupation:
    Programmer
    Location:
    Kolkata, India
    Home Page:
    Did you try it with different URLs? May be the one you are trying with doesn't set any cookies! And go through the class documentation at Sun's website.
     

Share This Page