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 piHuv <ha...@yahoo.com> on 2010/05/06 19:17:21 UTC

HttpClient Post failed to work with ISA server

Hi 

    I am trying to do a POST from a search website and get the following
error message "HTTPHelp : ClientProtocolException :
org.apache.http.client.HttpResponseException: Bad Request ( The HTTP request
includes a non-supported header. Contact your ISA Server administrator.  )". 
The strange thing is that I can download the page using the GET method with
or without the setting the DEFAULT_PROXY.

    Below is the POST, the GET subroutines and the debug error message that
I get from doing a POST.  Thanks for your help inadvance.

--------------------- POST ---------------------------------

    List<NameValuePair> createNameValues(String query) {
        int i = 0;
        List<NameValuePair> formparams = new ArrayList<NameValuePair>();
        StringTokenizer st = new StringTokenizer(query, "&");
        while (st.hasMoreTokens()) {
            String nvPairs = st.nextToken();
            //System.out.println(i + " formData= " + nvPairs);
            String[] nameValue = nvPairs.split("=");
            if (nameValue.length == 2) {
                formparams.add(new BasicNameValuePair(nameValue[0],
nameValue[1]));
            } else {
                formparams.add(new BasicNameValuePair(nameValue[0], ""));
            }

            i++;
        }

        return formparams;
    }


public int postPage(String url, String query, String resultPage) throws
IOException {
        HttpResponse response = null;
        HttpPost httpPost = null;
        BufferedWriter bout = null;
        String data = null;
        int statusCode = 400;

        DefaultHttpClient httpClient = new DefaultHttpClient();

        //Proxy setting
        HttpHost proxy = new HttpHost("xxx.xx.xx.x", 80);
        httpClient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY,
proxy);

        HttpParams params = createParamsForPosting();
        httpClient.setParams(params);

        UrlEncodedFormEntity tmp = null;
        try {
            httpPost = new HttpPost(url);
            System.out.print("Request; " + httpPost.getRequestLine());

            httpPost.setHeader("User-Agent", "Mozilla/5.0 (X11; U; Linux " +
"i686; en-US; rv:1.8.1.6) Gecko/20061201 Firefox/2.0.0.6 (Ubuntu-feisty)");
            httpPost.setHeader("Accept", "text/html,application/xml," +
"application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5");
            httpPost.setHeader("Content-Type",
"application/x-www-form-urlencoded");

            List<NameValuePair> nameValues = createNameValues(query);
            tmp = new UrlEncodedFormEntity(nameValues, "UTF-8");
            tmp.setChunked(true);

            httpPost.setEntity(tmp);

        } catch (UnsupportedEncodingException e) {
            System.out.println("HTTPHelp : UnsupportedEncodingException : "
+ e);
        }

        try {
            response = httpClient.execute(httpPost);
            statusCode = response.getStatusLine().getStatusCode();
            System.out.println("ret " + statusCode);

            if (statusCode == 200) {
                HttpEntity responseEntity = response.getEntity();
                data = EntityUtils.toString(responseEntity);
                //System.out.println("data1= " + data1);
                bout = new BufferedWriter(new FileWriter(new
File(resultPage)));
                bout.write(data);
            }

        } catch (ClientProtocolException e) {
            System.out.println("HTTPHelp : ClientProtocolException : " + e);
        } catch (IOException e) {
            System.out.println("HTTPHelp : IOException : " + e);
        } finally {
            if (bout != null) {
                bout.flush();
                bout.close();
            }

            httpClient.getConnectionManager().shutdown();
        }

        return statusCode;
    }




-------------------- GET --------------------------------

public boolean getPage(String query, String pagename)
            throws Exception {

        HttpClient httpClient = null;
        BufferedWriter bout = null;
        String resultPage = "";
        String responseBody = "";
        boolean status = false;

        try {
            httpClient = new DefaultHttpClient();

            //Proxy setting
            HttpHost proxy = new HttpHost("xxx.xx.xx.x", 80);
           
httpClient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);
           
            HttpGet httpget = new HttpGet(query);

            ResponseHandler<String> responseHandler = new
BasicResponseHandler();
            responseBody = httpClient.execute(httpget, responseHandler);
            //System.out.println(responseBody);

            if (!"".equals(responseBody)) {
                resultPage = "work\\" + pagename;
                bout = new BufferedWriter(new FileWriter(new
File(resultPage)));
                bout.write(responseBody);
                status = true;
            } else {
                logR.logEntry("Search failed: " + responseBody);
            }
        } finally {
            if (bout != null) {
                bout.flush();
                bout.close();
            }
            httpClient.getConnectionManager().shutdown();
        }

        return status;
    }




---------------------- ERROR MESSAGE -----------------------
2010/05/06 12:55:05:814 EDT [DEBUG] SingleClientConnManager - Get connection
for route HttpRoute[{}->http://172.16.64.6:80->http://www.research.gov]
2010/05/06 12:55:05:830 EDT [DEBUG] RequestAddCookies - CookieSpec selected:
best-match
2010/05/06 12:55:05:845 EDT [DEBUG] DefaultHttpClient - Attempt 1 to execute
request
2010/05/06 12:55:05:845 EDT [DEBUG] DefaultClientConnection - Sending
request: POST
http://www.research.gov/rgov/anonymous.portal?_nfpb=true&_windowLabel=awardInfo_1_4&awardInfo_1_4_actionOverride=%2Fgov%2Fresearch%2Fservices%2FawardInfo%2FadvancedSearchCommand&_pageLabel=page_research_funding_search
HTTP/1.1
2010/05/06 12:55:05:845 EDT [DEBUG] headers - >> POST
http://www.research.gov/rgov/anonymous.portal?_nfpb=true&_windowLabel=awardInfo_1_4&awardInfo_1_4_actionOverride=%2Fgov%2Fresearch%2Fservices%2FawardInfo%2FadvancedSearchCommand&_pageLabel=page_research_funding_search
HTTP/1.1
2010/05/06 12:55:05:845 EDT [DEBUG] headers - >> User-Agent: Mozilla/5.0
(X11; U; Linux i686; en-US; rv:1.8.1.6) Gecko/20061201 Firefox/2.0.0.6
(Ubuntu-feisty)
2010/05/06 12:55:05:845 EDT [DEBUG] headers - >> Accept:
text/html,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5
2010/05/06 12:55:05:845 EDT [DEBUG] headers - >> Content-Type:
application/x-www-form-urlencoded
2010/05/06 12:55:05:845 EDT [DEBUG] headers - >> Transfer-Encoding: chunked
2010/05/06 12:55:05:845 EDT [DEBUG] headers - >> Host: www.research.gov
2010/05/06 12:55:05:845 EDT [DEBUG] headers - >> Proxy-Connection:
Keep-Alive
2010/05/06 12:55:05:845 EDT [DEBUG] headers - >> Expect: 100-Continue
2010/05/06 12:55:05:861 EDT [DEBUG] DefaultClientConnection - Receiving
response: HTTP/1.1 400 Bad Request ( The HTTP request includes a
non-supported header. Contact your ISA Server administrator.  )
2010/05/06 12:55:05:861 EDT [DEBUG] headers - << HTTP/1.1 400 Bad Request (
The HTTP request includes a non-supported header. Contact your ISA Server
administrator.  )
2010/05/06 12:55:05:861 EDT [DEBUG] headers - << Via: 1.1 DORAL
2010/05/06 12:55:05:861 EDT [DEBUG] headers - << Connection: Keep-Alive
2010/05/06 12:55:05:861 EDT [DEBUG] headers - << Proxy-Connection:
Keep-Alive
2010/05/06 12:55:05:861 EDT [DEBUG] headers - << Pragma: no-cache
2010/05/06 12:55:05:861 EDT [DEBUG] headers - << Cache-Control: no-cache
2010/05/06 12:55:05:861 EDT [DEBUG] headers - << Content-Type: text/html
2010/05/06 12:55:05:861 EDT [DEBUG] headers - << Content-Length: 4067  
2010/05/06 12:55:05:861 EDT [DEBUG] DefaultHttpClient - Connection can be
kept alive indefinitely
HTTPHelp : ClientProtocolException :
org.apache.http.client.HttpResponseException: Bad Request ( The HTTP request
includes a non-supported header. Contact your ISA Server administrator.  )
2010/05/06 12:55:05:861 EDT [DEBUG] SingleClientConnManager - Releasing
connection
org.apache.http.impl.conn.SingleClientConnManager$ConnAdapter@1820dda




-- 
View this message in context: http://old.nabble.com/HttpClient-Post-failed-to-work-with-ISA-server-tp28476924p28476924.html
Sent from the HttpClient-User mailing list archive at Nabble.com.


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


Re: HttpClient Post failed to work with ISA server

Posted by Oleg Kalnichevski <ol...@apache.org>.
On Thu, 2010-05-06 at 10:17 -0700, piHuv wrote:
> Hi 
> 
>     I am trying to do a POST from a search website and get the following
> error message "HTTPHelp : ClientProtocolException :
> org.apache.http.client.HttpResponseException: Bad Request ( The HTTP request
> includes a non-supported header. Contact your ISA Server administrator.  )". 
> The strange thing is that I can download the page using the GET method with
> or without the setting the DEFAULT_PROXY.
> 
>     Below is the POST, the GET subroutines and the debug error message that
> I get from doing a POST.  Thanks for your help inadvance.
> 

This is a known compatibility with MS ISA server. The problem is the
'expect-continue' handshake. Disable it and this should solve the
problem.

Oleg


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