You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@commons.apache.org by Ravi Mutyala <ra...@gawab.com> on 2004/06/01 12:18:32 UTC

[httpclient] problem in sending the correct http request

Hi,

I am trying to connect to HTTPS adaptor of an external system. I'm 
getting the correct response from the adaptor when  I connect to the 
system with the following html form submit.

<HTML>
<HEAD> </HEAD>
 <body onLoad = "javascript:document.forms[0].method = 
'POST';javascript:document.forms[0].submit()">
<FORM name ="sampleform" method = "POST" ENCTYPE="TEXT/PLAIN" action = 
https://url.com/progname">
<input type ="hidden" name="XMLMSG"  value = "<A1>  <B1> abc </B1> <B1> 
bd</B1> <B2> 1 </B2> </A1> "/>
</FORM>
</body></HTML>

Now, I'm trying to send the same request using the postmethod of 
httpclient. I wrote the following program. But now I'm able to connect 
to the Adaptor but the adaptor is retuning an error saying that the 
request is not proper. I know that the request is not properly formed. 
But I'm not able to find out where the problem is in making the request.

I have checked the httpclient documentation and tried to add the xml 
message in a number of ways but failed.

It would be of great help if someone can give me some clues on the 
problem. I tried using addParameter() and setRequestBody() but both gave 
me errors.
I can't get the exact html request thatz going to the external system. 
Is there any way I can find how the request object is formed.

Thanks in advance.

/
Ravi.

java program using httpclient.

...............................
.......................................
String connectString;
connectString =  "https://url.com/progname";
HttpClient httpclient = new HttpClient();

/* SSL code */
Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
 try {
            KeyManagerFactory kmf = 
KeyManagerFactory.getInstance("SunX509", "SunJSSE") ;
        } catch (NoSuchAlgorithmException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        } catch (NoSuchProviderException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
       
httppost = new PostMethod(connectString);

NameValuePair nvp1 = new NameValuePair();
nvp1.setName("XMLMSG");
nvp1.setValue("<A1>  <B1> abc </B1> <B1> bd</B1> <B2> 1 </B2> </A1>");
       
httppost.setRequestHeader("Content-type", "text/xml; charset=ISO-8859-1");

httppost.addParameter(nvp1);

// httppost.setRequestBody(<input type="hidden" name="XMLMSG" 
value="<A1>  <B1> abc </B1> <B1> bd</B1> <B2> 1 </B2> </A1>"); 
/* tried this way also but no result :( */

 try {
            httpclient.executeMethod(httppost);
          } catch (HttpException e) {
                e.printStackTrace();
        } catch (IOException e) {
              e.printStackTrace();
        }
       
          System.out.println(httppost.getStatusLine().toString());
..................................
....................................
.......................................

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


Re: [httpclient] problem in sending the correct http request

Posted by Ravi Mutyala <ra...@gawab.com>.
Oleg,

it worked!! thanx a lot.
I used the setReqeustBody() instead of addParameter() to avoid the 
encoding and it went thru.

rgds
,
Ravi
olegk@bluewin.ch wrote:

>Ravi,
>
>Per default HTTP POST uses so called URL-encoding to encode the request body.
>To override the default behaviour use PostMethod#setRequestBody(String)
>
>httppost.setRequestBody(
>  "XMLMSG=<A1> <B1> abc </B1> <B1> bd</B1> <B2> 1 </B2> </A1>");
>httppost.setRequestHeader("Content-type", "TEXT/PLAIN");
>
>Hope this helps
>
>Oleg
>
>  
>


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


Re: problem in sending the correct http request

Posted by ol...@bluewin.ch.
Ravi,

Per default HTTP POST uses so called URL-encoding to encode the request body.
To override the default behaviour use PostMethod#setRequestBody(String)

httppost.setRequestBody(
  "XMLMSG=<A1> <B1> abc </B1> <B1> bd</B1> <B2> 1 </B2> </A1>");
httppost.setRequestHeader("Content-type", "TEXT/PLAIN");

Hope this helps

Oleg



>-- Original Message --
>Reply-To: "Jakarta Commons Users List" <co...@jakarta.apache.org>
>From: "Ravi Mutyala" <ra...@gawab.com>
>To: "Jakarta Commons Users List" <co...@jakarta.apache.org>
>Subject: Re: problem in sending the correct http request
>Date: Tue, 01 Jun 2004 14:03:44 GMT
>
>
>
>hi,
>
>Changing request header to
>httppost.setRequestHeader("Content-type", "TEXT/PLAIN");  
>hasn't helped.
>
>I printed the request that I sent with the namevaluepair using
>   
>        String temp =  httppost.getRequestBodyAsString();
>        System.out.println( "Request  Object now is"+ temp);
>I got the requestbody is something below
>XMLMSG=%3CA1%3E+%3CB1%3abc%3C%2FB1%3E+%3CB2%3ECRT%3C%2FB2%3E+%........
>
>what can be the reason for this kind of ouput when i'm expecting
><A1> <B1> abc </B1> <B1> bd</B1> <B2> 1 </B2> </A1> ?
>
>Is this the way how the POSTed data is sent across? I checked
>out the POST data using an IE plugin and the HTML that is
>working has
>a name value pair XMLMSG and <A1> <B1> abc </B1> <B1> bd</B1>
><B2> 1 </B2> </A1>.
>
>thanks in advance.
>
>/
>Ravi.
>
>
>Oleg Kalnichevski writes:
>
>> Ravi,
>> 
>> If you are lucky setting content type to TEXT/PLAIN
>> 
>> httppost.setRequestHeader("Content-type", "TEXT/PLAIN");
>> 
>> instead of
>> 
>> httppost.setRequestHeader("Content-type", "text/xml;
>> charset=ISO-8859-1");
>> 
>> may do the trick. If you are unlucky, the one sure way to know would be
>> to get hold of the complete HTTP request sent by the browser. There are
>> several possibilities to do so: (1) use a proxy capable of HTTPS
>> proxying (not just tunneling) (2) use a browser plugin that can display
>> request/response headers for a particular HTTP session
>> 
>> In order to see what exactly HttpClient sends across the wire you way
>> want to activate the wire log
>> 
>> http://jakarta.apache.org/commons/httpclient/logging.html
>> 
>> Oleg
>________________________________
>15 Mbytes Free Web-based and  POP3
>Sign up now: http://www.gawab.com
>
>---------------------------------------------------------------------
>To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
>For additional commands, e-mail: commons-user-help@jakarta.apache.org
>


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


Re: problem in sending the correct http request

Posted by Ravi Mutyala <ra...@gawab.com>.
hi,

Changing request header to
httppost.setRequestHeader("Content-type", "TEXT/PLAIN");  
hasn't helped.

I printed the request that I sent with the namevaluepair using
   
        String temp =  httppost.getRequestBodyAsString();
        System.out.println( "Request  Object now is"+ temp);
I got the requestbody is something below
XMLMSG=%3CA1%3E+%3CB1%3abc%3C%2FB1%3E+%3CB2%3ECRT%3C%2FB2%3E+%........

what can be the reason for this kind of ouput when i'm expecting
<A1> <B1> abc </B1> <B1> bd</B1> <B2> 1 </B2> </A1> ?

Is this the way how the POSTed data is sent across? I checked
out the POST data using an IE plugin and the HTML that is
working has
a name value pair XMLMSG and <A1> <B1> abc </B1> <B1> bd</B1>
<B2> 1 </B2> </A1>.

thanks in advance.

/
Ravi.


Oleg Kalnichevski writes:

> Ravi,
> 
> If you are lucky setting content type to TEXT/PLAIN
> 
> httppost.setRequestHeader("Content-type", "TEXT/PLAIN");
> 
> instead of
> 
> httppost.setRequestHeader("Content-type", "text/xml;
> charset=ISO-8859-1");
> 
> may do the trick. If you are unlucky, the one sure way to know would be
> to get hold of the complete HTTP request sent by the browser. There are
> several possibilities to do so: (1) use a proxy capable of HTTPS
> proxying (not just tunneling) (2) use a browser plugin that can display
> request/response headers for a particular HTTP session
> 
> In order to see what exactly HttpClient sends across the wire you way
> want to activate the wire log
> 
> http://jakarta.apache.org/commons/httpclient/logging.html
> 
> Oleg
________________________________
15 Mbytes Free Web-based and  POP3
Sign up now: http://www.gawab.com

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


Re: [httpclient] problem in sending the correct http request

Posted by Oleg Kalnichevski <ol...@bluewin.ch>.
Ravi,

If you are lucky setting content type to TEXT/PLAIN

httppost.setRequestHeader("Content-type", "TEXT/PLAIN");

instead of

httppost.setRequestHeader("Content-type", "text/xml;
charset=ISO-8859-1");

may do the trick. If you are unlucky, the one sure way to know would be
to get hold of the complete HTTP request sent by the browser. There are
several possibilities to do so: (1) use a proxy capable of HTTPS
proxying (not just tunneling) (2) use a browser plugin that can display
request/response headers for a particular HTTP session

In order to see what exactly HttpClient sends across the wire you way
want to activate the wire log

http://jakarta.apache.org/commons/httpclient/logging.html

Oleg

On Tue, 2004-06-01 at 12:18, Ravi Mutyala wrote:
> Hi,
> 
> I am trying to connect to HTTPS adaptor of an external system. I'm 
> getting the correct response from the adaptor when  I connect to the 
> system with the following html form submit.
> 
> <HTML>
> <HEAD> </HEAD>
>  <body onLoad = "javascript:document.forms[0].method = 
> 'POST';javascript:document.forms[0].submit()">
> <FORM name ="sampleform" method = "POST" ENCTYPE="TEXT/PLAIN" action = 
> https://url.com/progname">
> <input type ="hidden" name="XMLMSG"  value = "<A1>  <B1> abc </B1> <B1> 
> bd</B1> <B2> 1 </B2> </A1> "/>
> </FORM>
> </body></HTML>
> 
> Now, I'm trying to send the same request using the postmethod of 
> httpclient. I wrote the following program. But now I'm able to connect 
> to the Adaptor but the adaptor is retuning an error saying that the 
> request is not proper. I know that the request is not properly formed. 
> But I'm not able to find out where the problem is in making the request.
> 
> I have checked the httpclient documentation and tried to add the xml 
> message in a number of ways but failed.
> 
> It would be of great help if someone can give me some clues on the 
> problem. I tried using addParameter() and setRequestBody() but both gave 
> me errors.
> I can't get the exact html request thatz going to the external system. 
> Is there any way I can find how the request object is formed.
> 
> Thanks in advance.
> 
> /
> Ravi.
> 
> java program using httpclient.
> 
> ...............................
> .......................................
> String connectString;
> connectString =  "https://url.com/progname";
> HttpClient httpclient = new HttpClient();
> 
> /* SSL code */
> Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
>  try {
>             KeyManagerFactory kmf = 
> KeyManagerFactory.getInstance("SunX509", "SunJSSE") ;
>         } catch (NoSuchAlgorithmException e1) {
>             // TODO Auto-generated catch block
>             e1.printStackTrace();
>         } catch (NoSuchProviderException e1) {
>             // TODO Auto-generated catch block
>             e1.printStackTrace();
>         }
>        
> httppost = new PostMethod(connectString);
> 
> NameValuePair nvp1 = new NameValuePair();
> nvp1.setName("XMLMSG");
> nvp1.setValue("<A1>  <B1> abc </B1> <B1> bd</B1> <B2> 1 </B2> </A1>");
>        
> httppost.setRequestHeader("Content-type", "text/xml; charset=ISO-8859-1");
> 
> httppost.addParameter(nvp1);
> 
> // httppost.setRequestBody(<input type="hidden" name="XMLMSG" 
> value="<A1>  <B1> abc </B1> <B1> bd</B1> <B2> 1 </B2> </A1>"); 
> /* tried this way also but no result :( */
> 
>  try {
>             httpclient.executeMethod(httppost);
>           } catch (HttpException e) {
>                 e.printStackTrace();
>         } catch (IOException e) {
>               e.printStackTrace();
>         }
>        
>           System.out.println(httppost.getStatusLine().toString());
> ..................................
> ....................................
> .......................................
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: commons-user-help@jakarta.apache.org
> 


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