Go4Expert

Go4Expert (http://www.go4expert.com/)
-   Java (http://www.go4expert.com/forums/java/)
-   -   I have a problem with commons-httpclient-3.1 (http://www.go4expert.com/forums/i-commons-httpclient-31-t6817/)

osa 14Oct2007 02:11

I have a problem with commons-httpclient-3.1
 
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 Code:

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


        
    



pradeep 16Oct2007 07:54

Re: I have a problem with commons-httpclient-3.1
 
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.


All times are GMT +5.5. The time now is 15:32.