httpWebRequest

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • gerbrandc
    New Member
    • Feb 2008
    • 6

    httpWebRequest

    Hello,

    I hope somebody has a good explanation for this issue.

    Okay I'm having troubles with the httpWebRequest method. I need to download reports from a specified server (can't give these details). Now I don't have trouble with an "HTTPS" url but I with an "HTTP" url I get the error :

    error: The server committed a protocol violation. Section=Respons eHeader Detail=Header name is invalid

    This the first time I got this error in c#.net. I tried this in Java and there I didn't had any problems. So I'm kind of lost.

    I search the internet to see what the "ProtocolViolat ionException" throws. I found this on the msdn site:

    ProtocolViolati onException :
    Method is GET or HEAD, and either ContentLength is greater or equal to zero or SendChunked is true.
    -or-
    KeepAlive is true, AllowWriteStrea mBuffering is false, ContentLength is -1, SendChunked is false, and Method is POST or PUT.

    I did an output of these properties and some how these don't have the values mention to trigger the ProtocolViolati onException.

    So my question is, How is this possible?

    this is my testClass:
    Code:
    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.IO;
    using System.Net;
    
    namespace adlogix_views
    {
        static class httpTestClass
        {
            /// <summary>
            /// The main entry point for the application.
            /// </summary>
            [STAThread]
            static void Main()
            {
              
                string page = "";
                try
                {               
                    // write a line of text to the file
                    HttpWebRequest wr = (HttpWebRequest)WebRequest.Create("http://www.mywebsite.com");
    
                    string userPass = "myUsername:myPassword";
                    string encoding = Convert.ToBase64String(Encoding.ASCII.GetBytes(userPass));
    
                    wr.Headers.Add("Authorization", "Basic " + encoding);
                    wr.PreAuthenticate = true;
    
                    Console.WriteLine("Method = " + wr.Method);
                    Console.WriteLine("ContentLength = " + wr.ContentLength);
                    Console.WriteLine("SendChunked = " + wr.SendChunked);
                    Console.WriteLine("KeepAlive = " + wr.KeepAlive);
                    Console.WriteLine("AllowWriteStreamBuffering = " + wr.AllowWriteStreamBuffering);
    
                    HttpWebResponse ws = (HttpWebResponse)wr.GetResponse();
                    Stream str = ws.GetResponseStream();
                    StreamReader sr = new StreamReader(str);
                    while ((page = sr.ReadLine()) != null)
                    {
                        Console.WriteLine(page);
                    }
    
    
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
            }
        }
    }
    this is the output of the properties

    output: Method = GET
    ContentLength = -1
    SendChunked = False
    KeepAlive = True
    AllowWriteStrea mBuffering = True


    I hope somebody can help me with this.
    Last edited by Plater; Feb 27 '08, 02:58 PM. Reason: added code tags
  • Plater
    Recognized Expert Expert
    • Apr 2007
    • 7872

    #2
    Your posting the values of your reqeust, not of the response.
    You would need to examine the header values of the response to see what values the server is sending back.
    Since it says the server is commiting a protocol violation.

    Comment

    • gerbrandc
      New Member
      • Feb 2008
      • 6

      #3
      Okay, and how do I do that?
      Because the exception is triggered in the part "wr.GetResponse ()" ?

      Comment

      • Plater
        Recognized Expert Expert
        • Apr 2007
        • 7872

        #4
        If you declare an object of HttpWebResponse , then put in try/catch blocks around the .GetResponse() (assign the GetResponse() to your HttpWebResponse object) you should be able to still look at the values.

        So instead of
        Code:
        HttpWebResponse ws = (HttpWebResponse)wr.GetResponse();
        You would have:
        Code:
        HttpWebResponse ws;
        try
        {
           ws = (HttpWebResponse)wr.GetResponse();
        }
        catch(Exception ee)
        {
           bool setBreakPointHere=(ee.Message="");
        }
        Then should still be able to see what values are in ws

        Comment

        • gerbrandc
          New Member
          • Feb 2008
          • 6

          #5
          hmm,

          I tried this but when i'm looking at ws it says that it is "null". So I don't get to see his values.

          If I'm looking at the values in "wr.GetResponse ()" in the watch, is see that the Response = null
          and
          Status = ServerProtocolV iolation

          I looked also in the Non-Public members
          InternalStatus = RequestFatal
          m_InternalStatu s = RequestFatal

          Comment

          • Plater
            Recognized Expert Expert
            • Apr 2007
            • 7872

            #6
            I also question this line:
            Code:
            wr.PreAuthenticate = true;
            Seems like that would send security information before it was specifically requested, could that be the problem?

            Comment

            • gerbrandc
              New Member
              • Feb 2008
              • 6

              #7
              No I don't think so, I already putted that in comment. That was my first thought also.
              I don't see why it doesn't work. Especially when it does work in java.

              Comment

              • Plater
                Recognized Expert Expert
                • Apr 2007
                • 7872

                #8
                At this point I guess I would suggest getting ethereal and packet watching the network traffic. You can take a look at the headers and such that way.

                I am sort of guessing that even when called with HTTP (and not HTTPS) the server is still returning HTTPS encrypted data, which would not produce nice headers like regular http uses.

                Comment

                • gerbrandc
                  New Member
                  • Feb 2008
                  • 6

                  #9
                  Okay I solved it.
                  The solution was in the url itself. I'm passing thru table names to retrieve there statistics, now with that sniffer I saw I got a message that 2 tables that was passed thru, couldn't make a query. So I thought, lets remove those 2 tables from the query and suddenly I got my report.

                  So thanks for the help.

                  Comment

                  Working...