ASP.NET WebApi unit testing with Request.CreateResponse

Another way to solve this is to do the following: controller.Request = new HttpRequestMessage(); controller.Request.Properties.Add(HttpPropertyKeys.HttpConfigurationKey, new HttpConfiguration()); If you are upgrading to webapi 5.0, then you’ll need to change this to: controller.Request = new HttpRequestMessage(); controller.Request.SetConfiguration(new HttpConfiguration()); The reason why you need to do this is because you have to have Request populated on the … Read more

What does it mean when an HTTP request returns status code 0?

Many of the answers here are wrong. It seems people figure out what was causing status==0 in their particular case and then generalize that as the answer. Practically speaking, status==0 for a failed XmlHttpRequest should be considered an undefined error. The actual W3C spec defines the conditions for which zero is returned here: https://fetch.spec.whatwg.org/#concept-network-error As … Read more

.NET: Simplest way to send POST with data and read response

using (WebClient client = new WebClient()) { byte[] response = client.UploadValues(“http://dork.com/service”, new NameValueCollection() { { “home”, “Cosby” }, { “favorite+flavor”, “flies” } }); string result = System.Text.Encoding.UTF8.GetString(response); } You will need these includes: using System; using System.Collections.Specialized; using System.Net; If you’re insistent on using a static method/class: public static class Http { public static byte[] … Read more

Returning http status code from Web Api controller

I did not know the answer so asked the ASP.NET team here. So the trick is to change the signature to HttpResponseMessage and use Request.CreateResponse. [ResponseType(typeof(User))] public HttpResponseMessage GetUser(HttpRequestMessage request, int userId, DateTime lastModifiedAtClient) { var user = new DataEntities().Users.First(p => p.Id == userId); if (user.LastModified <= lastModifiedAtClient) { return new HttpResponseMessage(HttpStatusCode.NotModified); } return request.CreateResponse(HttpStatusCode.OK, … Read more

How to set HttpResponse timeout for Android in Java

In my example, two timeouts are set. The connection timeout throws java.net.SocketTimeoutException: Socket is not connected and the socket timeout java.net.SocketTimeoutException: The operation timed out. HttpGet httpGet = new HttpGet(url); HttpParams httpParameters = new BasicHttpParams(); // Set the timeout in milliseconds until a connection is established. // The default value is zero, that means the … Read more

Why should I use IHttpActionResult instead of HttpResponseMessage?

You might decide not to use IHttpActionResult because your existing code builds a HttpResponseMessage that doesn’t fit one of the canned responses. You can however adapt HttpResponseMessage to IHttpActionResult using the canned response of ResponseMessage. It took me a while to figure this out, so I wanted to post it showing that you don’t necesarily … Read more

Proper way to return JSON using node or Express

That response is a string too, if you want to send the response prettified, for some awkward reason, you could use something like JSON.stringify(anObject, null, 3) It’s important that you set the Content-Type header to application/json, too. var http = require(‘http’); var app = http.createServer(function(req,res){ res.setHeader(‘Content-Type’, ‘application/json’); res.end(JSON.stringify({ a: 1 })); }); app.listen(3000); // > … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)