HttpWebRequest on DotNet 2.0 driving you crazy for a B2B request? Disable ServicePointManager.Expect100Continue first!

Imagine, you have written a succesfull gateway to say, the European Community to check the validity of a VAT number.

It always works, but suddenly it does not work anymore. What's up? Could it be that some service pack modified the behaviour of the HttpWebRequest (including DotNet 2.0?) Sure  it is! see

This posting might spare you several hours. The idea of the Expect100Continue setting is, that postdata is sent to the webserver in a separate post sequence package. But not all web servers like IIS (sure I'm MVP Big Smile) are full featured so in this case, this feature was hard to detect, only by using Netmon, I could figure what the HttpWebRequest  was doing under the .NET runtime hood.

The Uri to the European VAT check is

And it does a POST request to
the previous obsolete URL was

It had worked before because it just dit a GET request instead of a POST.

The steps that I walk through normally to write such a gateway,

  1. opening my browser
  2. Fill the form
  3. Start Netmon 2.1 (or higher)
  4. Post the data
  5. Stop Netmon and filter on the HTTP protocol.

Now it's up to you to emulate the postdata and be sure that the character encoding is according to the supported one. In my case, I assume that UTF-8 is the worldstandard (it should work for most sites).

The code below is technically correct for the mentioned purpose but it performs no validation checking.
If you wish more flexibility some more work has to be done.

static bool doVatCheck(string vat, string state)


            string urlQuery = @"";

            //important! The WebLogic Server 8.1 SP3 clearly does not support IETF RFC 2616 Section 10.1.1.

// so we disable the expect100 header and HTTP behaviour

            ServicePointManager.Expect100Continue = false;

            HttpWebRequest req;

            StreamReader read=null;


            Encoding enc = System.Text.Encoding.UTF8;

            MemoryStream sw = new MemoryStream();

            advancePos(enc, sw, "ms"); //member state

            advancePos(sw, '=');

            advancePos(enc, sw, state);

            advancePos(sw, '&');

            advancePos(enc, sw, "iso"); //same as member state, ISO country code

            advancePos(sw, '=');

            advancePos(enc, sw, state);

            advancePos(sw, '&');

            advancePos(enc, sw, "vat"); // 12no VATno. Do not submit dots or spaces

            advancePos(sw, '=');

            advancePos(enc, sw, vat);

            req = (HttpWebRequest)HttpWebRequest.Create(urlQuery);

            req.Method = "POST";

            req.ContentType = "application/x-www-form-urlencoded";

// optional setting, UserAgent. A firewall could block if no browser is detected    

            req.UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)";

            long contLen = req.ContentLength = sw.Position;

            Stream str = req.GetRequestStream();

            str.Write(sw.GetBuffer(), 0, (int)contLen);




                read = new StreamReader(req.GetResponse().GetResponseStream(), enc);

                string buf = read.ReadToEnd();


                return buf.IndexOf("Yes, valid VAT number") >= 0;           


            catch (WebException ex)



                string errr = (urlQuery + ex.Message);


            return false;


static void advancePos(Encoding enc, MemoryStream sw, string val)


            byte[] buf = HttpUtility.UrlEncodeToBytes(val, enc);

            sw.Write(bufje, 0, buf.Length);


        static void advancePos(MemoryStream sw, char val)





blog comments powered by Disqus