Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is HttpWebRequest implemented differently in mono and .net?

Tags:

I am trying to port the c# cloudinary api to mono and I am having some problems building up the http request.

I have separated out this method for setting up the request but the HttpWebRequest.ContentLength turns out to be -1 in mono, but is .net the content is properly built.

I am running the newest Xamarin Studio on a Mac and I am building a MONO / .NET 4.0 library Mono version: 2.10.12

EDIT: Simplified code, this test passes in Visual Studio but fails in Xamarin studio

EDIT: Code is pushed to github if anybody would like to help

    [Test]     public void StreamTest()     {         var request = System.Net.HttpWebRequest.Create("http://foo.com");         request.Method = "POST";         request.ContentType = "application/x-www-form-urlencoded";         using (var writer = new System.IO.StreamWriter(request.GetRequestStream()))         {             writer.Write("anything");         }          Assert.IsTrue(request.ContentLength > 0);     } 
like image 510
terjetyl Avatar asked May 10 '13 13:05

terjetyl


People also ask

What is the difference between HttpWebRequest and WebRequest?

WebRequest is an abstract class. The HttpWebRequest class allows you to programmatically make web requests to the HTTP server.

Is HttpWebRequest deprecated?

NET 6, the WebRequest, WebClient, and ServicePoint classes are deprecated. The classes are still available, but they're not recommended for new development. To reduce the number of analyzer warnings, only construction methods are decorated with the ObsoleteAttribute attribute.

What is the use of HttpWebRequest in C#?

The HttpWebRequest class provides support for the properties and methods defined in WebRequest and for additional properties and methods that enable the user to interact directly with servers using HTTP. Do not use the HttpWebRequest constructor. Use the WebRequest.


1 Answers

When you do

System.Net.HttpWebRequest.Create("http://foo.com"); 

you actually create an instance of the internal class System.Net.Browser.BrowserHttpWebRequest

Here is the inheritance hierarchy

System.Net.WebRequest
  System.Net.HttpWebRequest
    System.Net.Browser.PolicyBasedWebRequest
      System.Net.Browser.BrowserHttpWebRequest

The content length is handled in PolicyBasedWebRequest, initialized in ctor with -1 and never changed; I suggest you manually set it.

like image 108
alexb Avatar answered Mar 29 '23 03:03

alexb