Dealing with bad HTTP headers

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Colin Crossman

    #1

    Dealing with bad HTTP headers

    So, I'm trying to write a web spider for some data gathering from a
    public database webserver. Anyway, in response to a POST, the webserver
    returns "200 Script Results Follow" instead of "201 CREATED". While
    both IE and Firefox ignore the odd header, this response kills my
    HttpWebRequest with a WebException - "System.Net.Web Exception: The
    server committed a protocol violation. Section=Respons eStatusLine".

    Does anyone know of a workaround for this?

    Thanks,
    Colin

    PS Code:
    Code:

    using System;
    using System.Collecti ons.Generic;
    using System.Text;
    using System.Text.Reg ularExpressions ;
    using System.Net;
    using System.IO;
    using System.Web;

    namespace ConsoleApplicat ion1
    {
    public class InsertTest {
    public static void Main() {

    loHttp.Method = "POST";
    byte[] lbPostBuffer = System.Text.

    Encoding.GetEnc oding(1252).Get Bytes(lcPostDat a);
    loHttp.ContentL ength = lbPostBuffer.Le ngth;

    Stream loPostData = loHttp.GetReque stStream();
    loPostData.Writ e(lbPostBuffer, 0, lbPostBuffer.Le ngth);
    loPostData.Clos e();
    try
    {
    HttpWebResponse loWebResponse =
    (HttpWebRespons e)loHttp.GetRes ponse();
    Encoding enc = System.Text.Enc oding.GetEncodi ng(1252);

    StreamReader loResponseStrea m =
    new StreamReader(lo WebResponse.Get ResponseStream( ), enc);

    string lcHtml = loResponseStrea m.ReadToEnd();

    loWebResponse.C lose();
    loResponseStrea m.Close();
    }
    catch (WebException J)
    {
    Console.WriteLi ne("Protocol violation!\n {0}", J.ToString());
    }

    string adj22 = Console.ReadLin e();

    }
    }

    }
  • Colin Crossman

    #2
    Re: Dealing with bad HTTP headers

    Whoops, sent wrong code in last message:


    using System;
    using System.Collecti ons.Generic;
    using System.Text;
    using System.Text.Reg ularExpressions ;
    using System.Net;
    using System.IO;
    using System.Web;

    namespace ConsoleApplicat ion1
    {
    public class InsertTest {
    public static void Main() {
    string lcUrl = "http://patft.uspto.gov/netacgi/nph-Parser";
    HttpWebRequest loHttp =
    (HttpWebRequest )WebRequest.Cre ate(lcUrl);

    // *** Send any POST data
    string lcPostData =
    "Sect1=" + System.Web.Http Utility.UrlEnco de("PTO1") +
    "&Sect2=" + System.Web.Http Utility.UrlEnco de("HITOFF") +
    "&d=" + System.Web.Http Utility.UrlEnco de("PALL") +
    "&p=" + System.Web.Http Utility.UrlEnco de("1") +
    "&u=" +
    System.Web.Http Utility.UrlEnco de("/netahtml/srchnum.htm") +
    "&r=" + System.Web.Http Utility.UrlEnco de("1") +
    "&f=" + System.Web.Http Utility.UrlEnco de("G") +
    "&l=" + System.Web.Http Utility.UrlEnco de("50") +
    "&s1=" + System.Web.Http Utility.UrlEnco de("6223224.WKU .") +
    "&OS=" + System.Web.Http Utility.UrlEnco de("PN/6223224") +
    "&RS=" + System.Web.Http Utility.UrlEnco de("PN/6223224");


    loHttp.Method = "POST";
    byte[] lbPostBuffer = System.Text.

    Encoding.GetEnc oding(1252).Get Bytes(lcPostDat a);
    loHttp.ContentL ength = lbPostBuffer.Le ngth;

    Stream loPostData = loHttp.GetReque stStream();
    loPostData.Writ e(lbPostBuffer, 0, lbPostBuffer.Le ngth);
    loPostData.Clos e();
    try
    {
    HttpWebResponse loWebResponse =
    (HttpWebRespons e)loHttp.GetRes ponse();
    Encoding enc = System.Text.Enc oding.GetEncodi ng(1252);

    StreamReader loResponseStrea m =
    new StreamReader(lo WebResponse.Get ResponseStream( ), enc);

    string lcHtml = loResponseStrea m.ReadToEnd();

    loWebResponse.C lose();
    loResponseStrea m.Close();
    }
    catch (WebException J)
    {
    Console.WriteLi ne("Protocol violation!\n {0}", J.ToString());

    }
    string adj22 = Console.ReadLin e();

    }
    }
    }


    Comment

    • Joerg Jooss

      #3
      Re: Dealing with bad HTTP headers

      Colin Crossman wrote:
      [color=blue]
      > So, I'm trying to write a web spider for some data gathering from a
      > public database webserver. Anyway, in response to a POST, the
      > webserver returns "200 Script Results Follow" instead of "201
      > CREATED".[/color]

      A 201 should only be returned if a new resource was created on the
      server-side. If that is the case but you get a 200 response instead,
      it's a server-side error.

      Do note that "200 Script Results Follow" is a perfectly legal HTTP
      status line. The HTTP spec defines status codes, but only *recommends*
      reason phrases. Accordingly, "Script Results Follow" is as good as "OK"
      or "Aye aye, Sir".

      [color=blue]
      > While both IE and Firefox ignore the odd header, this
      > response kills my HttpWebRequest with a WebException -
      > "System.Net.Web Exception: The server committed a protocol violation.
      > Section=Respons eStatusLine".
      >
      > Does anyone know of a workaround for this?[/color]

      What .NET version are you using? I've tested this briefly in both 1.1
      and 2.0 Beta 2, and both work with arbitrary reason phrases:

      Cheers,
      --

      mailto:news-reply@joergjoos s.de

      Comment

      Working...