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 Roland Weber <RO...@de.ibm.com> on 2006/12/06 14:27:04 UTC

Re: PostMethod - parameter in BASE64String

Hello Marcelo,

it is not the same encoding used in URLs.
HttpClient does not have a Base64 encoder,
but it uses commons-codec for this purpose.

hope that helps,
  Roland

"Marcelo Chryssovergis" <ma...@iea.org.br> wrote on 06.12.2006 
15:16:46:

> Hello,
> 
> I´m trying to login to a asp.net site.
> 
> I´m using the postmethod to send the data, but one of the fields of 
> the framework .Net (VIEWSTATE) has to be converted in a 
> Base64String. I believe that´s the same encoding used in URLs.
> 
> Is there any function in httpclient to encode the string to base64?
> 
> Thanks,
> Marcelo

Re: PostMethod - parameter in BASE64String

Posted by Jake C <bu...@hotmail.com>.
You shouldn't have to "convert" the value unless you are generating it in 
the first place, which I don't think you are doing.

I just went through this exercise, and (in our case, at least) __VIEWSTATE 
is only necessary when posting a secondary form after you are already logged 
in, not in the original login form. And when posting a secondary form, you 
must use the __VIEWSTATE that is already encoded in the form source. You 
should not be generating a new one.

Check out http://wiki.apache.org/jakarta-httpclient/ForAbsoluteBeginners for 
instructions on how to post a form with HttpClient. I think IIS always 
redirects after a login, so read 
http://jakarta.apache.org/commons/httpclient/redirects.html as well.

>From: "Julius Davies" <ju...@gmail.com>
>Reply-To: "HttpClient User Discussion" <ht...@jakarta.apache.org>
>To: "HttpClient User Discussion" <ht...@jakarta.apache.org>
>Subject: Re: PostMethod - parameter in BASE64String
>Date: Wed, 6 Dec 2006 09:05:11 -0500
>
>import org.apache.commons.codec.binary.Base64;
>
>byte[] bytes = "I love my byte array".getBytes();
>byte[] base64Bytes = Base64.encodeBase64( bytes );
>
>System.out.println( new String( base64Bytes ) );
>Outputs:
>
>SSBsb3ZlIG15IGJ5dGUgYXJyYXk=
>
>Good luck!  Like Roland said, "commons-codec.jar" is already on your
>classpath.  The Base64 class is available for you to use.
>
>yours,
>
>Julius
>
>
>On 12/6/06, Roland Weber <RO...@de.ibm.com> wrote:
>>
>>Hello Marcelo,
>>
>>it is not the same encoding used in URLs.
>>HttpClient does not have a Base64 encoder,
>>but it uses commons-codec for this purpose.
>>
>>hope that helps,
>>   Roland
>>
>>"Marcelo Chryssovergis" <ma...@iea.org.br> wrote on 06.12.2006
>>15:16:46:
>>
>> > Hello,
>> >
>> > I´m trying to login to a asp.net site.
>> >
>> > I´m using the postmethod to send the data, but one of the fields of
>> > the framework .Net (VIEWSTATE) has to be converted in a
>> > Base64String. I believe that´s the same encoding used in URLs.
>> >
>> > Is there any function in httpclient to encode the string to base64?
>> >
>> > Thanks,
>> > Marcelo
>>
>>
>>
>
>
>--
>yours,
>
>Julius Davies
>416-652-0183
>http://juliusdavies.ca/

_________________________________________________________________
All-in-one security and maintenance for your PC.  Get a free 90-day trial! 
http://clk.atdmt.com/MSN/go/msnnkwlo0050000002msn/direct/01/?href=http://clk.atdmt.com/MSN/go/msnnkwlo0050000001msn/direct/01/?href=http://www.windowsonecare.com/?sc_cid=msn_hotmail


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


Re: PostMethod - parameter in BASE64String

Posted by Roland Weber <ht...@dubioso.net>.
Hi Julius,

> byte[] bytes = "I love my byte array".getBytes();

tsk tsk tsk. That's depending on the platform default
encoding. You should never convert strings to bytes
without specifying the encoding to be used... ;-)

byte[] bytes = "I love my byte array".getBytes("UTF-8");

A co-worker once searched for several hours why his
code wouldn't work on a mainframe because of this.
I was only wondering why the Taiwan test team hadn't
detected the problem before, but I guess the test
servers had an ISO default encoding.

cheers,
  Roland


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


Re: PostMethod - parameter in BASE64String

Posted by Roland Weber <ht...@dubioso.net>.
Hi Julis, Marcelo,

> Here's an example "URL-Encoder" in Java:
> 
> char c = ...  // character to encode
> switch ( c ) {
>  case ' ':
>    buf.append( '+' );
>    break;
>  case '$':
>  case '<':
>  case '>':
>  case '&':
>  case '+':
>  case ',':
>  case '/':
>  case ':':
>  case ';':
>  case '=':
>  case '?':
>  case '@':
>  case '#':
>  case '%':
>    buf.append( '%' );
>    buf.append( Integer.toHexString( c ) );
>    break;
>  default:
>    buf.append( c );
>    break;
> }

Or else you just use org.apache.commons.codec.net.URLCodec,
like our EncodingUtil class does. No need to duplicate code,
you've got a dependency on commons-codec anyway.

cheers,
  Roland

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


Re: Re: PostMethod - parameter in BASE64String

Posted by Julius Davies <ju...@gmail.com>.
":" to %3A
"/" to %2F
"+" to %2B

That's not Base64!  Maybe that could be called "Base16"?  Usually it's just
called URL-Encoding, hence the mime-type "application/x-*www-url-encoded".
*Integer.toHexString( int ) is all you need to create these things.  And
Integer.parseInt( String, 16 ) is all you need to parse these things.

Here's an example "URL-Encoder" in Java:

char c = ...  // character to encode
switch ( c ) {
  case ' ':
    buf.append( '+' );
    break;
  case '$':
  case '<':
  case '>':
  case '&':
  case '+':
  case ',':
  case '/':
  case ':':
  case ';':
  case '=':
  case '?':
  case '@':
  case '#':
  case '%':
    buf.append( '%' );
    buf.append( Integer.toHexString( c ) );
    break;
  default:
    buf.append( c );
    break;
}


yours,

Julius



On 12/6/06, Marcelo Chryssovergis <ma...@iea.org.br> wrote:
>
> Hello,
>
> I believe we have similar situations.
>
> Actually I don´t need (and I can´t) encode the whole string, only chars
> considered invalid to base64.
> What I am doing is the same as you: I´m replacing some chars explicitly to
> the corresponding one in base64.
>
> Example:
> ":" to %3A
> "/" to %2F
> "+" to %2B
>
> The problem here, is that I missing some char and getting an error from
> server: "Invalid character in a Base-64 string."
>
> Well, back to my research :)
>
> Thanks,
> Marcelo.
>
>
> ----- Original Message -----
> From: <da...@gmx.net>
> To: "HttpClient User Discussion" <ht...@jakarta.apache.org>
> Sent: Wednesday, December 06, 2006 11:44 AM
> Subject: Re: Re: PostMethod - parameter in BASE64String
>
>
> > hello,
> >
> > I am using NameValuePairs to transfer some strings
> > to a server with the following content type:
> >
> > application/x-www-form-urlencoded
> >
> > My content change from  "!"  to  "%12"
> >
> > Can I also use encoder/decoder for my strings or
> > is  there a better way to transfer data correctly?
> >
> > Which kind of charset should I use?
> >
> > bastian
> >
> >
> >
> >>  import org.apache.commons.codec.binary.Base64;
> >>
> >> byte[] bytes = "I love my byte array".getBytes();
> >> byte[] base64Bytes = Base64.encodeBase64( bytes );
> >>
> >> System.out.println( new String( base64Bytes ) );
> >> Outputs:
> >>
> >> SSBsb3ZlIG15IGJ5dGUgYXJyYXk=
> >>
> >> Good luck!  Like Roland said, "commons-codec.jar" is already on your
> >> classpath.  The Base64 class is available for you to use.
> >>
> >> yours,
> >>
> >> Julius
> >>
> >>
> >> On 12/6/06, Roland Weber <RO...@de.ibm.com> wrote:
> >> >
> >> > Hello Marcelo,
> >> >
> >> > it is not the same encoding used in URLs.
> >> > HttpClient does not have a Base64 encoder,
> >> > but it uses commons-codec for this purpose.
> >> >
> >> > hope that helps,
> >> >   Roland
> >> >
> >> > "Marcelo Chryssovergis" <ma...@iea.org.br> wrote on 06.12.2006
> >> > 15:16:46:
> >> >
> >> > > Hello,
> >> > >
> >> > > I´m trying to login to a asp.net site.
> >> > >
> >> > > I´m using the postmethod to send the data, but one of the fields of
> >> > > the framework .Net (VIEWSTATE) has to be converted in a
> >> > > Base64String. I believe that´s the same encoding used in URLs.
> >> > >
> >> > > Is there any function in httpclient to encode the string to base64?
> >> > >
> >> > > Thanks,
> >> > > Marcelo
> >> >
> >> >
> >> >
> >>
> >>
> >> --
> >> yours,
> >>
> >> Julius Davies
> >> 416-652-0183
> >> http://juliusdavies.ca/
> >
> > --
> > Der GMX SmartSurfer hilft bis zu 70% Ihrer Onlinekosten zu sparen!
> > Ideal für Modem und ISDN: http://www.gmx.net/de/go/smartsurfer
> >
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: httpclient-user-unsubscribe@jakarta.apache.org
> > For additional commands, e-mail: httpclient-user-help@jakarta.apache.org
> >
> >
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: httpclient-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: httpclient-user-help@jakarta.apache.org
>
>


-- 
yours,

Julius Davies
416-652-0183
http://juliusdavies.ca/

Re: PostMethod - parameter in BASE64String

Posted by Roland Weber <ht...@dubioso.net>.
Hi Marcelo,

> I just put a
> httpClient.getParams().setParameter("http.protocol.content-type",
> "application/x-www-form-urlencoded");
> 
> and send the viewstate back to the site. Much simpler than I thought.
> 
> I´m working with httpClient for about 1,5 year (not an absolute
> begginer), but this is the first situation I need to set this parameter
> to encode correctly the form fields.

You haven't mentioned how you create the request. A PostMethod
fed with parameters should set that content type automatically.


> The HttpClient´s documentation is not the best, but the mailing list is
> great. Thanks!

We do what we can. Thanks for the thanks :-)

cheers,
  Roland


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


Re: PostMethod - parameter in BASE64String

Posted by Marcelo Chryssovergis <ma...@iea.org.br>.
Thank you all, now it´s working :)

I just put a
httpClient.getParams().setParameter("http.protocol.content-type", 
"application/x-www-form-urlencoded");

and send the viewstate back to the site. Much simpler than I thought.

I´m working with httpClient for about 1,5 year (not an absolute begginer), 
but this is the first situation I need to set this parameter to encode 
correctly the form fields.

The HttpClient´s documentation is not the best, but the mailing list is 
great. Thanks!

Marcelo 


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


Re: PostMethod - parameter in BASE64String

Posted by Roland Weber <ht...@dubioso.net>.
Hello Marcelo,

> Actually I don´t need (and I can´t) encode the whole string, only chars
> considered invalid to base64.
> What I am doing is the same as you: I´m replacing some chars explicitly
> to the corresponding one in base64.
> 
> Example:
> ":" to %3A
> "/" to %2F
> "+" to %2B

This is NOT base64! This is www-url-encoded.

> The problem here, is that I missing some char and getting an error from
> server: "Invalid character in a Base-64 string."

That may be because you're not generating a base64 string at all.

Please follow the instructions in the Client HTTP Programming Primer
to analyze _what_ you should actually be sending. You may have to
install a network sniffer to be sure about what's sent by a browser.
If something needs to be base64-encoded, it is either generated by
the server or you'll find JavaScript that performs base64 encoding.
http://wiki.apache.org/jakarta-httpclient/ForAbsoluteBeginners

If you need www-url-encoding, just add the name/value pairs as parameters
to the PostMethod, the method should take care of the encoding.
http://jakarta.apache.org/commons/httpclient/apidocs/org/apache/commons/httpclient/methods/PostMethod.html#addParameter(org.apache.commons.httpclient.NameValuePair)

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


Re: Re: PostMethod - parameter in BASE64String

Posted by Marcelo Chryssovergis <ma...@iea.org.br>.
Hello,

I believe we have similar situations.

Actually I don´t need (and I can´t) encode the whole string, only chars 
considered invalid to base64.
What I am doing is the same as you: I´m replacing some chars explicitly to 
the corresponding one in base64.

Example:
":" to %3A
"/" to %2F
"+" to %2B

The problem here, is that I missing some char and getting an error from 
server: "Invalid character in a Base-64 string."

Well, back to my research :)

Thanks,
Marcelo.


----- Original Message ----- 
From: <da...@gmx.net>
To: "HttpClient User Discussion" <ht...@jakarta.apache.org>
Sent: Wednesday, December 06, 2006 11:44 AM
Subject: Re: Re: PostMethod - parameter in BASE64String


> hello,
>
> I am using NameValuePairs to transfer some strings
> to a server with the following content type:
>
> application/x-www-form-urlencoded
>
> My content change from  "!"  to  "%12"
>
> Can I also use encoder/decoder for my strings or
> is  there a better way to transfer data correctly?
>
> Which kind of charset should I use?
>
> bastian
>
>
>
>>  import org.apache.commons.codec.binary.Base64;
>>
>> byte[] bytes = "I love my byte array".getBytes();
>> byte[] base64Bytes = Base64.encodeBase64( bytes );
>>
>> System.out.println( new String( base64Bytes ) );
>> Outputs:
>>
>> SSBsb3ZlIG15IGJ5dGUgYXJyYXk=
>>
>> Good luck!  Like Roland said, "commons-codec.jar" is already on your
>> classpath.  The Base64 class is available for you to use.
>>
>> yours,
>>
>> Julius
>>
>>
>> On 12/6/06, Roland Weber <RO...@de.ibm.com> wrote:
>> >
>> > Hello Marcelo,
>> >
>> > it is not the same encoding used in URLs.
>> > HttpClient does not have a Base64 encoder,
>> > but it uses commons-codec for this purpose.
>> >
>> > hope that helps,
>> >   Roland
>> >
>> > "Marcelo Chryssovergis" <ma...@iea.org.br> wrote on 06.12.2006
>> > 15:16:46:
>> >
>> > > Hello,
>> > >
>> > > I´m trying to login to a asp.net site.
>> > >
>> > > I´m using the postmethod to send the data, but one of the fields of
>> > > the framework .Net (VIEWSTATE) has to be converted in a
>> > > Base64String. I believe that´s the same encoding used in URLs.
>> > >
>> > > Is there any function in httpclient to encode the string to base64?
>> > >
>> > > Thanks,
>> > > Marcelo
>> >
>> >
>> >
>>
>>
>> -- 
>> yours,
>>
>> Julius Davies
>> 416-652-0183
>> http://juliusdavies.ca/
>
> -- 
> Der GMX SmartSurfer hilft bis zu 70% Ihrer Onlinekosten zu sparen!
> Ideal für Modem und ISDN: http://www.gmx.net/de/go/smartsurfer
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: httpclient-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: httpclient-user-help@jakarta.apache.org
>
> 


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


Re: PostMethod - parameter in BASE64String

Posted by Roland Weber <ht...@dubioso.net>.
Hello Bastian,

> I am using NameValuePairs to transfer some strings
> to a server with the following content type:
> 
>  application/x-www-form-urlencoded
> 
> My content change from  "!"  to  "%12"
> 
> Can I also use encoder/decoder for my strings or
> is  there a better way to transfer data correctly?

The content is transferred correctly. That's what
x-www-form-urlencoded is all about: special characters
are escaped as %xx sequences. Server-side APIs like
the Servlet API will automatically decode that format.

cheers,
  Roland

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


Re: Re: PostMethod - parameter in BASE64String

Posted by da...@gmx.net.
hello,

I am using NameValuePairs to transfer some strings
to a server with the following content type:

 application/x-www-form-urlencoded

My content change from  "!"  to  "%12"

Can I also use encoder/decoder for my strings or
is  there a better way to transfer data correctly?

Which kind of charset should I use?

bastian



>  import org.apache.commons.codec.binary.Base64;
> 
> byte[] bytes = "I love my byte array".getBytes();
> byte[] base64Bytes = Base64.encodeBase64( bytes );
> 
> System.out.println( new String( base64Bytes ) );
> Outputs:
> 
> SSBsb3ZlIG15IGJ5dGUgYXJyYXk=
> 
> Good luck!  Like Roland said, "commons-codec.jar" is already on your
> classpath.  The Base64 class is available for you to use.
> 
> yours,
> 
> Julius
> 
> 
> On 12/6/06, Roland Weber <RO...@de.ibm.com> wrote:
> >
> > Hello Marcelo,
> >
> > it is not the same encoding used in URLs.
> > HttpClient does not have a Base64 encoder,
> > but it uses commons-codec for this purpose.
> >
> > hope that helps,
> >   Roland
> >
> > "Marcelo Chryssovergis" <ma...@iea.org.br> wrote on 06.12.2006
> > 15:16:46:
> >
> > > Hello,
> > >
> > > I´m trying to login to a asp.net site.
> > >
> > > I´m using the postmethod to send the data, but one of the fields of
> > > the framework .Net (VIEWSTATE) has to be converted in a
> > > Base64String. I believe that´s the same encoding used in URLs.
> > >
> > > Is there any function in httpclient to encode the string to base64?
> > >
> > > Thanks,
> > > Marcelo
> >
> >
> >
> 
> 
> -- 
> yours,
> 
> Julius Davies
> 416-652-0183
> http://juliusdavies.ca/

-- 
Der GMX SmartSurfer hilft bis zu 70% Ihrer Onlinekosten zu sparen! 
Ideal für Modem und ISDN: http://www.gmx.net/de/go/smartsurfer

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


Re: PostMethod - parameter in BASE64String

Posted by Julius Davies <ju...@gmail.com>.
 import org.apache.commons.codec.binary.Base64;

byte[] bytes = "I love my byte array".getBytes();
byte[] base64Bytes = Base64.encodeBase64( bytes );

System.out.println( new String( base64Bytes ) );
Outputs:

SSBsb3ZlIG15IGJ5dGUgYXJyYXk=

Good luck!  Like Roland said, "commons-codec.jar" is already on your
classpath.  The Base64 class is available for you to use.

yours,

Julius


On 12/6/06, Roland Weber <RO...@de.ibm.com> wrote:
>
> Hello Marcelo,
>
> it is not the same encoding used in URLs.
> HttpClient does not have a Base64 encoder,
> but it uses commons-codec for this purpose.
>
> hope that helps,
>   Roland
>
> "Marcelo Chryssovergis" <ma...@iea.org.br> wrote on 06.12.2006
> 15:16:46:
>
> > Hello,
> >
> > I´m trying to login to a asp.net site.
> >
> > I´m using the postmethod to send the data, but one of the fields of
> > the framework .Net (VIEWSTATE) has to be converted in a
> > Base64String. I believe that´s the same encoding used in URLs.
> >
> > Is there any function in httpclient to encode the string to base64?
> >
> > Thanks,
> > Marcelo
>
>
>


-- 
yours,

Julius Davies
416-652-0183
http://juliusdavies.ca/