You are viewing a plain text version of this content. The canonical link for it is here.
Posted to httpclient-users@hc.apache.org by Eugeny N Dzhurinsky <bo...@redwerk.com> on 2006/07/12 12:18:53 UTC

wrong cookies passed back to httpclient?

Hello again!
I still fighting with weird situation while same page is displayed well using
browser, but with HttpClient error 500 is returned.

Here is my unit test below:

import java.io.IOException;

import junit.framework.Assert;
import junit.framework.TestCase;

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.HttpStatus;
import org.apache.commons.httpclient.HttpVersion;
import org.apache.commons.httpclient.URI;
import org.apache.commons.httpclient.URIException;
import org.apache.commons.httpclient.cookie.CookiePolicy;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.params.HttpMethodParams;
import org.apache.log4j.BasicConfigurator;
import org.apache.log4j.Level;
import org.apache.log4j.Logger;

public class HTTPClientTest extends TestCase {

    private static Logger log = Logger.getLogger(HTTPClientTest.class);

    HttpClient client;

    GetMethod get;

    String url;

    protected void setUp() throws Exception {
        client = new HttpClient();
        url = "http://atlanta.familypages.info/out-35.html";
        BasicConfigurator.configure();
        log.setLevel(Level.DEBUG);
    }

    public void testResultcode() {
        try {
            URI uri = getURI(url);
            get = new GetMethod();
            get.setURI(uri);
            setMethodParameters();
            client.executeMethod(get);
            get = handleRedirects(20);
            Assert.assertEquals(HttpStatus.SC_OK, get.getStatusCode());
        } catch (HttpException e) {
            e.printStackTrace(System.out);
        } catch (IOException e) {
            e.printStackTrace(System.out);
        }
    }

    private GetMethod handleRedirects(int redirects) throws URIException,
            IOException {
        switch (get.getStatusCode()) {
        case HttpStatus.SC_MOVED_PERMANENTLY:
        case HttpStatus.SC_MOVED_TEMPORARILY:
        case HttpStatus.SC_SEE_OTHER:
        case HttpStatus.SC_TEMPORARY_REDIRECT:
            Header[] headers = get.getResponseHeaders("location");
            if (headers != null && headers.length > 0) {
                String location = customEscape(headers[headers.length - 1]
                        .getValue());
                if (location.indexOf("://") == -1) {
                    URI uri = new URI(get.getURI(), location, location
                            .indexOf("%") > -1);
                    location = uri.getURI();
                }
                if (log.isDebugEnabled())
                    log.debug("Handling redirect for " + location + " level "
                            + redirects);
                get.releaseConnection();
                get = new GetMethod();
                get.setURI(getURI(location));
                setMethodParameters();
                client.executeMethod(get);
                if (redirects > 0)
                    handleRedirects(redirects - 1);
            }
        }
        return get;
    }

    private void setMethodParameters() {
        get.setFollowRedirects(false);
        HttpMethodParams p = get.getParams();
        p.setVersion(new HttpVersion(1, 1));
        p.setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY);
    }

    private URI getURI(String _url) throws URIException {
        _url = customEscape(_url);
        return new URI(_url, _url.indexOf('%') != -1);
    }

    private String customEscape(String _url) {
        String _internal_url = _url.indexOf(" ") > -1 && _url.indexOf("%") > -1 ? _url
                .replaceAll(" ", "%20")
                : _url;
        return _internal_url;
    }

}

for HttpClient headers are:

GET /madeline/default.asp?GCID=C1000x047&AID=10280172&PID=1453275 HTTP/1.1
User-Agent: Jakarta Commons-HttpClient/3.0.1
Host: www.allaboardtoys.com

HTTP/1.1 302 Object moved
Server: Microsoft-IIS/5.0
Date: Wed, 12 Jul 2006 09:57:38 GMT
P3P: policyref="http://www.allaboardtoys.com/w3c/p3p.xml"
X-Powered-By: ASP.NET
Pragma: no-cache
cache-control: no-store
Cache-Control: no-cache, must-revalidate
Location: http://www.allaboardtoys.com/madeline/?GCID=C1000x047&AID=10280172&PID=1453275
Location: /madeline
Content-Length: 130
Content-Type: text/html
Expires: Tue, 11 Jul 2006 09:57:38 GMT
Set-Cookie: ShopperManager%2F=sts=nwc45&ShopperManager%2F=A4AD8C16350E42B500ECC55B4F45FE8C; expires=Thu, 27-Jul-2006 09:57:38 GMT; domain=allaboardtoys.com; path=/
Set-Cookie: ASPSESSIONIDCSRAQQTS=GFIGGBPBPBAKFJMDODCENAKH; path=/
Cache-control: no-cache
----------------------------------------------------------
GET /madeline HTTP/1.1
User-Agent: Jakarta Commons-HttpClient/3.0.1
Host: www.allaboardtoys.com
Cookie: ShopperManager%2F=sts=nwc45&ShopperManager%2F=A4AD8C16350E42B500ECC55B4F45FE8C
Cookie: ASPSESSIONIDCSRAQQTS=GFIGGBPBPBAKFJMDODCENAKH

HTTP/1.1 302 Object Moved
Location: http://www.allaboardtoys.com/madeline/
Server: Microsoft-IIS/5.0
Content-Type: text/html
Content-Length: 161
----------------------------------------------------------
GET /madeline/ HTTP/1.1
User-Agent: Jakarta Commons-HttpClient/3.0.1
Host: www.allaboardtoys.com
Cookie: ShopperManager%2F=sts=nwc45&ShopperManager%2F=A4AD8C16350E42B500ECC55B4F45FE8C
Cookie: ASPSESSIONIDCSRAQQTS=GFIGGBPBPBAKFJMDODCENAKH

HTTP/1.1 500 Internal Server Error
Server: Microsoft-IIS/5.0
Date: Wed, 12 Jul 2006 09:57:38 GMT
P3P: policyref="http://www.allaboardtoys.com/w3c/p3p.xml"
X-Powered-By: ASP.NET
Pragma: no-cache
cache-control: no-store
Cache-Control: no-cache, must-revalidate
Content-Length: 318
Content-Type: text/html
Expires: Tue, 11 Jul 2006 09:57:38 GMT
Set-Cookie: ShopperManager%2F=sts=nwc45&ShopperManager%2F=A4AD8C16350E42B500ECC55B4F45FE8C%2CASPSESSIONIDCSRAQQTS%3DGFIGGBPBPBAKFJMDODCENAKH; expires=Thu, 27-Jul-2006 09:57:38 GMT; domain=allaboardtoys.com; path=/
Cache-control: no-cache


and for firefox:


GET /madeline/default.asp?GCID=C1000x047&AID=10280172&PID=1453275 HTTP/1.1
Host: www.allaboardtoys.com
User-Agent: Mozilla/5.0 (X11; U; FreeBSD i386; en-US; rv:1.8.0.4) Gecko/20060614 Firefox/1.5.0.4
Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5
Accept-Language: ru,en;q=0.7,en-us;q=0.3
Accept-Encoding: gzip,deflate
Accept-Charset: KOI8-R,utf-8;q=0.7,*;q=0.7
Keep-Alive: 300
Connection: keep-alive
Cookie: ShopperManager%2F=sts=nwc45&ShopperManager%2F=00B7DB4FE1034AF289DAA46000102466

HTTP/1.x 302 Object moved
Server: Microsoft-IIS/5.0
Date: Wed, 12 Jul 2006 10:05:18 GMT
X-Powered-By: ASP.NET
P3P: policyref="http://www.allaboardtoys.com/w3c/p3p.xml"
Pragma: no-cache
Cache-Control: no-store, no-cache, must-revalidate, no-cache
Location: /madeline
Content-Length: 130
Content-Type: text/html
Expires: Tue, 11 Jul 2006 10:05:18 GMT
Set-Cookie: ShopperManager%2F=sts=nwc45&ShopperManager%2F=00B7DB4FE1034AF289DAA46000102466; expires=Thu, 27-Jul-2006 10:05:18 GMT; domain=allaboardtoys.com; path=/
Set-Cookie: ASPSESSIONIDCATQCDDQ=COIDDADBJEONBCNBBAKPGIBM; path=/
----------------------------------------------------------
GET /madeline HTTP/1.1
Host: www.allaboardtoys.com
User-Agent: Mozilla/5.0 (X11; U; FreeBSD i386; en-US; rv:1.8.0.4) Gecko/20060614 Firefox/1.5.0.4
Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5
Accept-Language: ru,en;q=0.7,en-us;q=0.3
Accept-Encoding: gzip,deflate
Accept-Charset: KOI8-R,utf-8;q=0.7,*;q=0.7
Keep-Alive: 300
Connection: keep-alive
Cookie: ShopperManager%2F=sts=nwc45&ShopperManager%2F=00B7DB4FE1034AF289DAA46000102466; ASPSESSIONIDCATQCDDQ=COIDDADBJEONBCNBBAKPGIBM

HTTP/1.x 302 Object Moved
Location: http://www.allaboardtoys.com/madeline/
Server: Microsoft-IIS/5.0
Content-Type: text/html
Content-Length: 161
----------------------------------------------------------
GET /madeline/ HTTP/1.1
Host: www.allaboardtoys.com
User-Agent: Mozilla/5.0 (X11; U; FreeBSD i386; en-US; rv:1.8.0.4) Gecko/20060614 Firefox/1.5.0.4
Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5
Accept-Language: ru,en;q=0.7,en-us;q=0.3
Accept-Encoding: gzip,deflate
Accept-Charset: KOI8-R,utf-8;q=0.7,*;q=0.7
Keep-Alive: 300
Connection: keep-alive
Cookie: ShopperManager%2F=sts=nwc45&ShopperManager%2F=00B7DB4FE1034AF289DAA46000102466; ASPSESSIONIDCATQCDDQ=COIDDADBJEONBCNBBAKPGIBM

HTTP/1.x 200 OK
Server: Microsoft-IIS/5.0
Date: Wed, 12 Jul 2006 10:05:18 GMT
X-Powered-By: ASP.NET
P3P: policyref="http://www.allaboardtoys.com/w3c/p3p.xml"
Pragma: no-cache
Cache-Control: no-store, no-cache, must-revalidate, no-cache
Content-Length: 23672
Content-Type: text/html
Expires: Tue, 11 Jul 2006 10:05:18 GMT
Set-Cookie: ShopperManager%2F=sts=nwc45&ShopperManager%2F=00B7DB4FE1034AF289DAA46000102466; expires=Thu, 27-Jul-2006 10:05:18 GMT; domain=allaboardtoys.com; path=/

Is there anything I can try with HttpClient to avoid this error?

-- 
Eugene N Dzhurinsky

---------------------------------------------------------------------
To unsubscribe, e-mail: httpclient-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: httpclient-user-help@jakarta.apache.org


Re: wrong cookies passed back to httpclient?

Posted by Eugeny N Dzhurinsky <bo...@redwerk.com>.
On Wed, Jul 12, 2006 at 02:03:07PM +0200, Roland Weber wrote:
> Hello Eugeny,
> 
> please study the cookie guide:
> http://jakarta.apache.org/commons/httpclient/cookies.html
> 
> There is a paragraph about "http.protocol.single-cookie-header"
> in the "Browser Compatibility" section.

Well, I set this property, but it seems to wrong object :(

But setting this property to httpclient directly solved the issue

Many thanks!

-- 
Eugene N Dzhurinsky

---------------------------------------------------------------------
To unsubscribe, e-mail: httpclient-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: httpclient-user-help@jakarta.apache.org


Re: wrong cookies passed back to httpclient?

Posted by Roland Weber <RO...@de.ibm.com>.
Hello Eugeny,

please study the cookie guide:
http://jakarta.apache.org/commons/httpclient/cookies.html

There is a paragraph about "http.protocol.single-cookie-header"
in the "Browser Compatibility" section.

hope that helps,
  Roland


---------------------------------------------------------------------
To unsubscribe, e-mail: httpclient-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: httpclient-user-help@jakarta.apache.org