C# HttpWebRequest The underlying connection was closed: An unexpected error occurred on a send

In 4.0 version of the .Net framework the ServicePointManager.SecurityProtocol only offered two options to set: Ssl3: Secure Socket Layer (SSL) 3.0 security protocol. Tls: Transport Layer Security (TLS) 1.0 security protocol In the next release of the framework the SecurityProtocolType enumerator got extended with the newer Tls protocols, so if your application can use th …

Read more

Get HTTP requests and responses made using HttpWebRequest/HttpWebResponse to show in Fiddler

The Fiddler FAQ gives the answer to this. You essentially route your HTTP traffic through Fiddler (i.e. Use Fiddler as a proxy). Here’s some links that will help: Fiddler Web Debugging – Configuring Clients Which in turn links to here: Take the Burden Off Users with Automatic Configuration in .NET You can achieve this via …

Read more

Is it possible to transfer authentication from Webbrowser to WebRequest

If the question is only “How to transfer Webbrowser authentication information to Webrequest or Webclient?” this code is enough: You can call the GetUriCookieContainer method that returns you a CookieContainer that can be used for subsequent call with WebRequest object. [DllImport(“wininet.dll”, SetLastError = true)] public static extern bool InternetGetCookieEx( string url, string cookieName, StringBuilder cookieData, …

Read more

How to set the content of an HttpWebRequest in C#?

The following should get you started byte[] buffer = …request data as bytes var webReq = (HttpWebRequest) WebRequest.Create(“http://127.0.0.1/target”); webReq.Method = “REQUIRED METHOD”; webReq.ContentType = “REQUIRED CONTENT TYPE”; webReq.ContentLength = buffer.Length; var reqStream = webReq.GetRequestStream(); reqStream.Write(buffer, 0, buffer.Length); reqStream.Close(); var webResp = (HttpWebResponse) webReq.GetResponse();

Why does HttpWebRequest throw an exception instead returning HttpStatusCode.NotFound?

Ya this can be quite annoying when web pages use status codes heavily and not all of them are errors. Which can make processing the body quite a pain. Personally I use this extension method for getting the response. public static class HttpWebResponseExt { public static HttpWebResponse GetResponseNoException(this HttpWebRequest req) { try { return (HttpWebResponse)req.GetResponse(); …

Read more

How to use Fiddler to debug traffic from Any app (eg. C#/WPF app)

I found the solution at this fiddler2.com page Why don’t I see traffic sent to http://localhost or http://127.0.0.1? Internet Explorer and the .NET Framework are hardcoded not to send requests for Localhost through any proxies, and as a proxy, Fiddler will not receive such traffic. The simplest workaround is to use your machine name as …

Read more

How to add parameters into a WebRequest?

Use stream to write content to webrequest string data = “username=<value>&password=<value>”; //replace <value> byte[] dataStream = Encoding.UTF8.GetBytes(data); private string urlPath = “http://xxx.xxx.xxx/manager/”; string request = urlPath + “index.php/org/get_org_form”; WebRequest webRequest = WebRequest.Create(request); webRequest.Method = “POST”; webRequest.ContentType = “application/x-www-form-urlencoded”; webRequest.ContentLength = dataStream.Length; Stream newStream=webRequest.GetRequestStream(); // Send the data. newStream.Write(dataStream,0,dataStream.Length); newStream.Close(); WebResponse webResponse = webRequest.GetResponse();