Fog Creek Software
Discussion Board




Welcome! and rules

Joel on Software

Post data to an ASP using AxWebBrowser (solution)

I am trying to write a program which POSTs some data to an ASP page, using an AxWebBrowser.

This component (the Microsoft WebBrowser aka IE) is highly undocumented. :-( Just check the MSDN - lots of method parameters are simply NOT explained.

Anyway - for the Navigate method:

the PostData parameter must be an array of bytes (otherwise C# sends everything as Unicode).

Also.. there is one BIG gotcha: in order for ASP (or IIS, or whatever the fsck it is that screws things up in MS web products) to understand the fields you post, you must add this line in the headers:

Content-Type: application/x-www-form-urlencoded

If this is not in the headers, then the Request.Form collection of ASP will be empty.

Microsoft SUXXXXXXXX big time for not documented the god damn fscking WebBrowser method parameters!!!! What is more basic, than a web browser??????!!!!!!!

I know that I behave like an angry teenager, but I spent about 6 fscking hours debugging the STUPID IDIOTIC problem!!!!

And please don't delete my post - it is VERY useful to others who want to do the same thing!

Knight Rider
Thursday, June 19, 2003

Ah, one more thing...

The Navigate method has several parameters.

For the Header parameter, it is ok to pass a C# string.

For the PostData parameter, it is not ok to pass a C# string - you have to convert it to standard ASCIIZ first (every character must have 8 bits).

Why this highly ilogical behavior?

I know - the PostData goes after the header, and the Header goes into the, well, HTTP header.

But still - why this difference?

There is no logic in this!

Why did they choose to automatically convert some params from Unicode to ASCII, and not to convert some params from Unicode to ASCII?

Because the POSTed values CAN be Unicode, whereas the header can't be Unicode (the HTTP standard says so).

So in fact it is very logical.

Why doesn't Microsoft write this VERY IMPORTANT detail in the MSDN, in the page about the Navigate method?

If Bill Gates ever comes to my country, I shall come to the airport with a big sign saying "please update the god damn MSDN"!

:-)

Knight Rider
Thursday, June 19, 2003

Ah, just one more thing!

Why doesn't C# and VB .NET include a type of string (let's call it AsciiString) with 8 bits per character?

When you say:

string sUnicode = "bla-bla-bla";
AsciiString sAscii;

sAscrii=sUnicode;

then the 16-bit-per-character string should automatically be converted to ASCII 8-bit-per-character string.


The Unicode Hater Society - supporting string usage freedom

Hey Microsoft - Leave our strings alone!


Excuse me, but I am very nervous!

Knight Rider
Thursday, June 19, 2003

I hope someone will release a NEW language for .NET, called C# Pro, which will have STANDARD HONEST ASCII STRINGS!

We hate Unicode!

Knight Rider
Thursday, June 19, 2003

// Here is an Unicode to ASCII C# conversion function:
// Written by The Unicode Hater Society
// function is tested and works!
public byte[] UnicodeToAscii(string src)  {
    int len=src.Length;
    byte[] result = new Byte[len+1];

    for(int i=0; i<len; i++)
        result[i] = Convert.ToByte(src[i]);
    result[len]=0;

    return result;
}

Knight Rider
Thursday, June 19, 2003

Windows API and low-level programming forever!

Yeah, man!

Knight Rider
Thursday, June 19, 2003

K&R are gonna rock your world! :)

Down with Unic0de!

Knight Rider
Thursday, June 19, 2003

K&R C is for wimps. I like Forth and ASM better. :)

J. Cobra Coder
Thursday, June 19, 2003

Yeah, man!

The day Intel will add wimpy Unicode string operations in the x86, I shall switch to MacIntosh with it's PowerPC, even if it should be called, in fact, SlowCPU. :)

Or.. horrors! They may have extended the strings operations behind my back, when I wasn't looking!

Does the x86 alread have Unicode string operations???

Only the allmighty AMD Athlon 64 may save the x86, now!

:-)

Knight Rider
Thursday, June 19, 2003

BTW.. I almost finished the app!

Knight Rider
Thursday, June 19, 2003

"We hate Unicode!"

Speak for yourself. MBCS is a serious pain. Unicode is much better.

Brad Wilson (dotnetguy.techieswithcats.com)
Thursday, June 19, 2003

Knight Rider -- an alternative for your UnicodeToAscii() method:

byte[] bytes = System.Text.Encoding.ASCII.GetBytes(str)

Not all of us speak English and use our particular alphabet you know (but I'm sure you were joking anyway).

Duncan Smart
Thursday, June 19, 2003

Have you considered reading the RFCs or some other primer on HTTP rather than blaming Microsoft for you not understanding how forms work?

SomeBody
Thursday, June 19, 2003

Actually Knight Rider it's just struck me the folly of your ways -- what on earth are you using Interop with AxWebBrowser for in the first place?? The .NET BCL already has robust support for POSTing, GETting and whatever to an HTTP server - namely System.Net.WebClient:

using System;
using System.Text;
using System.Net;
using System.Collections.Specialized;
//...

string url = "http://www.amazon.co.uk/exec/obidos/search-handle-form";
NameValueCollection formData = new NameValueCollection();
formData["field-keywords"] = "Harry Potter";

WebClient webClient = new WebClient();
byte[] responseBytes = webClient.UploadValues(url, "POST", formData);
string response = Encoding.UTF8.GetString(responseBytes);

Console.WriteLine(response);

Duncan Smart
Thursday, June 19, 2003

Duncan, thank you VERY much for your extremely informative reply.

I am using axWebBrowser because I need it to manage cookies automatically: I login, the site gives me a cookie, and I want this cookie sent back to the server in the next requests.

I know I could have done this using some HTTP component or object, BUT by using axWebBrowser, I don't need to bother.

The MSDN documentation for axWebBrowser.Navigate is really extremely poor - they don't give the type of arguments, etc.

As for Unicode - I think that people should get over it and use only the English language. Let's face it, English has won! And I'm saying this as a non-native English speaker...

Knight Rider
Thursday, June 19, 2003

> Have you considered reading the RFCs or some other
> primer on HTTP rather than blaming Microsoft for you not
> understanding how forms work?

I have read the entire HTTP RFC about 5 years ago (I wrote a multithreaded web spider then), and I didn't remember the needed information.

But that's not the point.

axWebBrowser is a high level component. It should offer abstraction and SHIELD me from the implementation details.

A good printing component, for example, should print without requiring me to know anything about the print codes sent to the printer.

Knight Rider
Friday, June 20, 2003

Ok, since it's Microsoft's responsibility to shield you from such details, did you consider searching MSDN (or Google) for something such as WebBrowser PostData?  When I do so, it's link #1 on Google and link #4 on MSDN.

SomeBody
Friday, June 20, 2003

*  Recent Topics

*  Fog Creek Home