Thursday, April 7, 2011

C# :httpwebrequest

I am trying to set value of referer parameter of httpwebrequest header, but it is giving error-

Function that I am using (C#):

webRequest.Headers.Set(HttpRequestHeader.Referer, "http://www.microsoft.com");

Error:

Invalid parameters. A required parameter is not found or contains invalid value.
From stackoverflow
  • Try this method from MSDN If you post your code I can better help you out.

    HttpWebRequest    myHttpWebRequest=(HttpWebRequest)WebRequest.Create(myUri);
    // Set referer property  to http://www.microsoft.com .
    myHttpWebRequest.Referer="http://www.microsoft.com";
    // Assign the response object of 'HttpWebRequest' to a 'HttpWebResponse' variable.
    HttpWebResponse myHttpWebResponse=(HttpWebResponse)myHttpWebRequest.GetResponse();
    // Display the contents of the page to the console.
    Stream streamResponse=myHttpWebResponse.GetResponseStream();
    StreamReader streamRead = new StreamReader( streamResponse );
    Char[] readBuffer = new Char[256];
    int count = streamRead.Read( readBuffer, 0, 256 );
    Console.WriteLine("\nThe contents of HTML page are.......");    
    while (count > 0) 
    {
        String outputData = new String(readBuffer, 0, count);
        Console.Write(outputData);
        count = streamRead.Read(readBuffer, 0, 256);
    }
    Console.WriteLine("\nHTTP Request  Headers :\n\n{0}",myHttpWebRequest.Headers);
    Console.WriteLine("\nHTTP Response Headers :\n\n{0}",myHttpWebResponse.Headers);    
    streamRead.Close();
    streamResponse.Close();
    // Release the response object resources.
    myHttpWebResponse.Close();
    Console.WriteLine("Referer to the site is:{0}",myHttpWebRequest.Referer);
    
  • i have already tried solution i.e. given is msdn. thats not working for me and also tried below code but it is also not working for me.

    webRequest.Headers.Set(HttpRequestHeader.Referer, "http://www.microsoft.com");

  • There are some reserved HTTP Headers which you can't set via webRequest.Headers.Set Referer is one of them.

    For these there is always a special property to set.

    In your case webRequest.Referer = "http://google.com" will do.

  • @kedar kamthe, you can't put HttpRequestHeader.Referer in there, because that is expecting a value, I believe, but not sure... I think you wanted to use this:

    webRequest.Headers.Set("Referer", "http://www.microsoft.com");

    But you should use it as Chris Ballance's example. That should work, but if that doesn't work, just try as I show above ;).

    You should put your code here, so we can see what is wrong.

  • Here is typical use:

    HttpWebRequest req = (HttpWebRequest)WebRequest.Create("");
    req.Referer = "http://www.google.com";
    

    Show your code.

0 comments:

Post a Comment

Note: Only a member of this blog may post a comment.