1

I have created a simple web api and when I access this from IE the web api returns exactly what I expected. However, now I tried to access the same web api from a web forms application using httpclient. But now I get a 404 error. But the api seems to work, because I do receive results when using a browser. Any ideas what goes wrong? This is the code:

 HttpClientHandler handler = new HttpClientHandler()
 {
     UseDefaultCredentials = true
 };

 HttpClient client = new HttpClient(handler);
 client.BaseAddress = new Uri("http://server/appdir");
 client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

 HttpResponseMessage response = client.GetAsync("/api/environment").Result;
7
  • 1
    if you debug, the request url is the same than your webapi url? Commented Oct 21, 2013 at 21:42
  • I'll leave this to the WCF experts to answer, but until they work their magic...have tried to run wireshark or something similar to see whats going on under the covers? Commented Oct 21, 2013 at 21:43
  • Oh and just in case its all on one box (connection to localhost), most packet sniffers don't work. For monitoring local host, I've had luck with RawCap netresec.com/?page=RawCap, and this winsock based sniffer from nirsoft: nirsoft.net/utils/socket_sniffer.html Commented Oct 21, 2013 at 21:46
  • maybe use client.GetAsync("/appdir/api/environment/").Result; ? Commented Oct 21, 2013 at 21:49
  • Fiddler would sniff local to local. - fiddler2.com Commented Oct 21, 2013 at 22:10

3 Answers 3

2

This is the code that worked.

 HttpClientHandler handler = new HttpClientHandler()
 {
     UseDefaultCredentials = true
 };

 HttpClient client = new HttpClient(handler);

 client.BaseAddress = new Uri("http://eetmws10v");

 client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

 HttpResponseMessage response = client.GetAsync("/aim2/api/environment").Result;
Sign up to request clarification or add additional context in comments.

Comments

1

Wow. I was experiencing this same error this weekend and I cannot imagine how flimsy the cause of the problem was.

This is what worked for me:

string _baseAddress = "http://localhost/webapi/";
string controllerURL = "api/School";

client.BaseAddress = new Uri(_baseAddress);
string result = client.GetStringAsync("controllerURL").Result.ToString();

The combinations of _baseAddress and controllerURL that caused the 404 error for me are as follows:

string _baseAddress = "http://localhost/webapi/";
string controllerURL = "/api/School";

or

string _baseAddress = "http://localhost/webapi";
string controllerURL = "api/School";

or

string _baseAddress = "http://localhost/webapi";
string controllerURL = "/api/School";

I sincerely hope something is done soon to remove this needless rigidity that is likely to waste many a developer's useful man hours.

Comments

1

I work on this problem 2 days,and what work for me is to add custom user agent, like in this link

code

var client = new HttpClient();
client.DefaultRequestHeaders.Add("User-Agent", "Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; WOW64; Trident/6.0)");

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.