467,116 Members | 1,135 Online
Bytes | Developer Community
Ask Question

Home New Posts Topics Members FAQ

Post your question to a community of 467,116 developers. It's quick & easy.

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]import java.net.*;
import java.io.*;
import java.util.*;

import org.apache.commons.httpclient.Cookie;
import org.apache.commons.httpclient.DefaultHttpMethodRet ryHandler;
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.SimpleHttpConnection Manager;
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.HttpClientPar ams;
import org.apache.commons.httpclient.params.HttpMethodPar ams;
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 = 0; i < CookieString.length; i++) {
System.out.println(" - " + CookieString[i].toExternalForm());
}
System.out.println(responseBody);
return responseBody;
}




}[/PHP]
Oct 13 '07 #1
  • viewed: 1331
Share:

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

2 posts views Thread by Jon Dellaria | last post: by
1 post views Thread by Steven Mason | last post: by
reply views Thread by Joe Bloggs | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.