hc-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Mark Hankey (JIRA)" <j...@apache.org>
Subject [jira] Created: (HTTPCLIENT-778) DefaultRedirectHandler not encoding url before making http request.
Date Tue, 27 May 2008 14:38:40 GMT
DefaultRedirectHandler not encoding url before making http request. 
--------------------------------------------------------------------

                 Key: HTTPCLIENT-778
                 URL: https://issues.apache.org/jira/browse/HTTPCLIENT-778
             Project: HttpComponents HttpClient
          Issue Type: Bug
          Components: HttpClient
    Affects Versions: 4.0 Alpha 4
         Environment: Windows XP, JRE_1.5.0_14
            Reporter: Mark Hankey


The redirect url from the server has a space and should be converted to %20 before the redirect
request is made.  The following test class reproduces the issue:
/*
 * $HeadURL: https://svn.apache.org/repos/asf/httpcomponents/httpclient/tags/4.0-alpha4/module-client/src/examples/org/apache/http/examples/client/ClientFormLogin.java
$
 * $Revision: 645074 $
 * $Date: 2008-04-05 12:59:59 +0200 (Sat, 05 Apr 2008) $
 * ====================================================================
 *
 *  Licensed to the Apache Software Foundation (ASF) under one or more
 *  contributor license agreements.  See the NOTICE file distributed with
 *  this work for additional information regarding copyright ownership.
 *  The ASF licenses this file to You under the Apache License, Version 2.0
 *  (the "License"); you may not use this file except in compliance with
 *  the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 * ====================================================================
 *
 * This software consists of voluntary contributions made by many
 * individuals on behalf of the Apache Software Foundation.  For more
 * information on the Apache Software Foundation, please see
 * <http://www.apache.org/>.
 *
 */

package org.apache.http.examples.client;

import java.util.ArrayList;
import java.util.List;
import java.util.Iterator;
import au.id.jericho.lib.html.*;
import java.util.*;

import org.apache.http.Header;
import org.apache.http.message.*;
import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.params.CookiePolicy;
import org.apache.http.client.params.ClientPNames;
import org.apache.http.conn.params.ConnRoutePNames;
import org.apache.http.cookie.Cookie;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;
import org.apache.http.params.*;


/**
 * A example that demonstrates how HttpClient APIs can be used to perform
 * form-based logon.
 */
public class ClientFormLogin {

    public static void main(String[] args) throws Exception {

        DefaultHttpClient httpclient = new DefaultHttpClient();
        final HttpHost proxy =        	
            // new HttpHost("127.0.0.1", 8666, "http");
         new HttpHost("135.214.42.162", 8080, "http");
        //added
        HttpResponse response = null;
        HttpEntity entity = null;
        httpclient.getParams().setParameter(
                ClientPNames.COOKIE_POLICY, CookiePolicy.BROWSER_COMPATIBILITY);
        httpclient.getParams().setParameter(
                ClientPNames.DEFAULT_HOST, "http://www.fultonassessor.org");
        
        
        //httpclient.getParams().setParameter(   ClientPNames.DEFAULT_HEADERS, "Accept-Language=en-us");
        httpclient.getParams().setParameter("User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0;
Windows NT 5.1; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727; .NET CLR 3.0.04506.30; InfoPath.1)");
        //httpclient.getParams().setParameter("http.language.Accept-Language", "en-us");
        //httpclient.getParams().setParameter("Accept", "image/gif, image/x-xbitmap, image/jpeg,
image/pjpeg, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword,*/*");
        
        
        httpclient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);
        
        //HttpGet httpget = new HttpGet("http://www.fultonassessor.org/Forms/Datalets.aspx?idx=1&parid=14%20007700061068&jur=000&roll=RE&taxyear=2007&ownseq=1&sIndex=0");
        //httpget.addHeader("Accept", "application/xaml+xml, image/gif, image/x-xbitmap, image/jpeg,
image/pjpeg, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword,*/*");
        
        /*
        HttpResponse response = httpclient.execute(httpget);
        HttpEntity entity = response.getEntity();

        System.out.println("Login form get: " + response.getStatusLine());
        if (entity != null) {
            entity.consumeContent();
        }
        System.out.println("Initial set of cookies:");
        List<Cookie> 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());
            }
        }
		*/
        HttpPost httpost = new HttpPost("http://www.fultonassessor.org/Search/GenericSearch.aspx?mode=PARID");
        
        List <NameValuePair> nvps = new ArrayList <NameValuePair>();
        /*
        nvps.add(new BasicNameValuePair("PARID", "14 007700061068"));
        nvps.add(new BasicNameValuePair("inpParid", "14 007700061068"));  
        nvps.add(new BasicNameValuePair("ReportsListParIDs", "14007700061068"));
        nvps.add(new BasicNameValuePair("selPageSize", "15"));  
        nvps.add(new BasicNameValuePair("selSortDir", "Ascending")); 
        nvps.add(new BasicNameValuePair("lblRoll", "Roll Type"));
        nvps.add(new BasicNameValuePair("inpRoll", "-Any-"));
        nvps.add(new BasicNameValuePair("selSortBy", "PARID"));
        nvps.add(new BasicNameValuePair("SortBy", "Parcel ID"));
        nvps.add(new BasicNameValuePair("SortDir", " asc"));
        nvps.add(new BasicNameValuePair("selSortBy", "Parcel ID"));
        nvps.add(new BasicNameValuePair("PageSize", "15"));
        */
        //nvps.add(new BasicNameValuePair("hdIDX", "14007700061068"));
        //nvps.add(new BasicNameValuePair("hdPin", "14 007700061068"));
        //nvps.add(new BasicNameValuePair("hdTaxYear", "2007"));
        //nvps.add(new BasicNameValuePair("hdJur", "000"));
        //nvps.add(new BasicNameValuePair("hdOwnSeq", "1"));
        //nvps.add(new BasicNameValuePair("hdMask", "1"));
        //nvps.add(new BasicNameValuePair("hdsIndex", "0"));   
        
        nvps.add(new BasicNameValuePair("PageNum", "1"));
        nvps.add(new BasicNameValuePair("SortBy", "PARID"));
        nvps.add(new BasicNameValuePair("SortDir", " asc"));
        nvps.add(new BasicNameValuePair("PageSize", "15"));
        nvps.add(new BasicNameValuePair("mode", "PARID"));
        nvps.add(new BasicNameValuePair("hdAction", "Search"));
        nvps.add(new BasicNameValuePair("hdListType", "PA"));
        nvps.add(new BasicNameValuePair("sIndex", "-1"));
        nvps.add(new BasicNameValuePair("inpParid", "14 -0056-0005-058-7"));
        nvps.add(new BasicNameValuePair("selSortBy", "PARID"));
        nvps.add(new BasicNameValuePair("selSortDir", " asc"));
        nvps.add(new BasicNameValuePair("selPageSize", "15"));
        
        nvps.add(new BasicNameValuePair("hdSelected", ""));
        nvps.add(new BasicNameValuePair("hdsIndex", ""));
        nvps.add(new BasicNameValuePair("hdSkip", ""));
        nvps.add(new BasicNameValuePair("inpRoll", ""));
        

        nvps.add(new BasicNameValuePair("__VIEWSTATE", "dDwtMTg0MjE5NjgzODt0PEAwPDtvPHQ+Oz47bDxpPDA+Oz47bDx0PDtsPGk8Nj47PjtsPHQ8cDxwPGw8VmlzaWJsZTs+O2w8bzxmPjs+Pjs+Ozs+Oz4+Oz4+Oz4="));
        
        
        
        
        
        //nvps.add(new BasicNameValuePair("IDToken2", "password"));

       httpost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));

        //response = httpclient.execute(httpost);
     // Add default headers
        List<BasicHeader> defaultHeaders = new ArrayList();
        defaultHeaders.add(new BasicHeader("Accept-Language", "en-us"));
        defaultHeaders.add(new BasicHeader("Accept", "image/gif, image/x-xbitmap, image/jpeg,
image/pjpeg, application/xaml+xml, application/vnd.ms-xpsdocument, application/x-ms-xbap,
application/x-ms-application, application/x-shockwave-flash, application/vnd.ms-excel, application/vnd.ms-powerpoint,
application/msword, */*"));
        defaultHeaders.add(new BasicHeader("Pragma", "no-cache"));
        defaultHeaders.add(new BasicHeader("Proxy-Connection", "Keep-Alive"));
        //defaultHeaders.add(new BasicHeader("Referer", "http://www.fultonassessor.org/Search/GenericSearch.aspx?mode=PARID"));
        
        
        
        defaultHeaders.add(new BasicHeader("Content-Type", "application/x-www-form-urlencoded"));
        
        //httpget.getParams().setParameter(ClientPNames.DEFAULT_HEADERS,defaultHeaders);
        httpost.getParams().setParameter(ClientPNames.DEFAULT_HEADERS,defaultHeaders);
  
        //response = httpclient.execute(httpget);
        response = httpclient.execute(httpost);
        entity = response.getEntity();
        try{
        entity = response.getEntity();

        System.out.println("----------------------------------------");
        System.out.println(response.getStatusLine());
        Header[] headers = response.getAllHeaders();
        for (int i=0; i<headers.length; i++) {
            System.out.println(headers[i]);
        }
        System.out.println("----------------------------------------");
        String responseString = null;
        if (response.getEntity() != null) {
        	responseString = EntityUtils.toString(response.getEntity());
            System.out.println(responseString);
        }
        
        //parseInJerico(responseString);

    } finally {
        // If we could be sure that the stream of the entity has been
        // closed, we wouldn't need this code to release the connection.
        // However, EntityUtils.toString(...) can throw an exception.

        // if there is no entity, the connection is already released
        if (entity != null)
            entity.consumeContent(); // release connection gracefully
    }
        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());
            }
        }
        */
    }
    
    /*
    
	public  static void parseInJerico(String htmlString) throws Exception {
		System.out.println("----------------------------------------");
		System.out.println("---PARSING IN JERICO---");
		System.out.println("----------------------------------------");
		// Call fullSequentialParse manually as most of the source will be parsed.
		Source source  = new Source(htmlString);
		source.fullSequentialParse();

		System.out.println("Document title:");
		String title=getTitle(source);
		System.out.println(title==null ? "(none)" : title);

		System.out.println("\nDocument description:");
		String description=getMetaValue(source,"description");
		System.out.println(description==null ? "(none)" : description);

		System.out.println("\nDocument keywords:");
		String keywords=getMetaValue(source,"keywords");
		System.out.println(keywords==null ? "(none)" : keywords);
	
		System.out.println("\nLinks to other documents:");
		List linkElements=source.findAllElements(HTMLElementName.A);
		for (Iterator i=linkElements.iterator(); i.hasNext();) {
			Element linkElement=(Element)i.next();
			String href=linkElement.getAttributeValue("href");
			if (href==null) continue;
			// A element can contain other tags so need to extract the text from it:
			String label=linkElement.getContent().getTextExtractor().toString();
			System.out.println(label+" <"+href+'>');
		}


		System.out.println("\nAll text from file (exluding content inside SCRIPT and STYLE elements):\n");
		System.out.println(source.getTextExtractor().setIncludeAttributes(true).toString());

		System.out.println("\nSame again but this time extend the TextExtractor class to also exclude
text from P elements and any elements with class=\"control\":\n");
		TextExtractor textExtractor=new TextExtractor(source) {
			public boolean excludeElement(StartTag startTag) {
				return startTag.getName()==HTMLElementName.P || "control".equalsIgnoreCase(startTag.getAttributeValue("class"));
			}
		};
		System.out.println(textExtractor.setIncludeAttributes(true).toString());
  }

	private static String getTitle(Source source) {
		Element titleElement=source.findNextElement(0,HTMLElementName.TITLE);
		if (titleElement==null) return null;
		// TITLE element never contains other tags so just decode it collapsing whitespace:
		return CharacterReference.decodeCollapseWhiteSpace(titleElement.getContent());
	}

	private static String getMetaValue(Source source, String key) {
		for (int pos=0; pos<source.length();) {
			StartTag startTag=source.findNextStartTag(pos,"name",key,false);
			if (startTag==null) return null;
			if (startTag.getName()==HTMLElementName.META)
				return startTag.getAttributeValue("content"); // Attribute values are automatically decoded
			pos=startTag.getEnd();
		}
		return null;
	}
	*/
}

-- 
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@hc.apache.org
For additional commands, e-mail: dev-help@hc.apache.org


Mime
View raw message