Creating WPF BitmapImage from MemoryStream png, gif

Add bi.CacheOption = BitmapCacheOption.OnLoad directly after your .BeginInit(): BitmapImage bi = new BitmapImage(); bi.BeginInit(); bi.CacheOption = BitmapCacheOption.OnLoad; … Without this, BitmapImage uses lazy initialization by default and stream will be closed by then. In first example you try to read image from possibly garbage-collected closed or even disposed MemoryStream. Second example uses file, which is … Read more

Mocking WebResponse’s from a WebRequest

I found this question while looking to do exactly the same thing. Couldn’t find an answer anywhere, but after a bit more digging found that the .Net Framework has built in support for this. You can register a factory object with WebRequest.RegisterPrefix which WebRequest.Create will call when using that prefix (or url). The factory object … Read more

How to force WebRequest to send Authorization header during POST

Here’s my solution. The value is in variable json. var myUri = new Uri(fullpath); var myWebRequest = WebRequest.Create(myUri); var myHttpWebRequest = (HttpWebRequest)myWebRequest; myHttpWebRequest.PreAuthenticate = true; myHttpWebRequest.Headers.Add(“Authorization”, “Bearer ” + AccessToken); myHttpWebRequest.Accept = “application/json”; var myWebResponse = myWebRequest.GetResponse(); var responseStream = myWebResponse.GetResponseStream(); if (responseStream == null) return null; var myStreamReader = new StreamReader(responseStream, Encoding.Default); var json … Read more

Reasons for a 409/Conflict HTTP error when uploading a file to sharepoint using a .NET WebRequest?

Since no answers were posted, I found the following here: The Web server (running the Web site) thinks that the request submitted by the client (e.g. your Web browser or our CheckUpDown robot) can not be completed because it conflicts with some rule already established. For example, you may get a 409 error if you … Read more

Making a web request to a web page which requires windows authentication

You should use Credentials property to pass the windows credentials to the web service. If you wish to pass current windows user’s credentials to the service then request.Credentials = CredentialCache.DefaultCredentials; should do the trick. Otherwise use NetworkCredential as follows: request.Credentials = new NetworkCredential(user, pwd, domain);

HttpWebRequest: Add Cookie to CookieContainer -> ArgumentException (Parametername: cookie.Domain)

CookieContainers can hold multiple cookies for different websites, therefor a label (the Domain) has to be provided to bind each cookie to each website. The Domain can be set when instantiating the individual cookies like so: Cookie chocolateChip = new Cookie(“CookieName”, “CookieValue”) { Domain = “DomainName” }; An easy way to grab the domain to … Read more

Test if a website is alive from a C# application

HttpWebResponse response = (HttpWebResponse)request.GetResponse(); if (response == null || response.StatusCode != HttpStatusCode.OK) As @Yanga mentioned, HttpClient is probably the more common way to do this now. HttpClient client = new HttpClient(); var checkingResponse = await client.GetAsync(url); if (!checkingResponse.IsSuccessStatusCode) { return false; }