简体   繁体   中英

HttpClient Call to WebApi Service on IIS 7.5 - 404 Not Found

I am new to WebApi and we have a requirement to create\\maintain a token issuing web service. A colleague of mine provided his solution (beta) which he has allowed me to use. If I run the web service from my dev environment (VS 2012) I am able to query it and get the token back from a web browser (Chrome). I then wrote a test app to use HttpClient to query the service and get the token. This works fine from the service hosted in my dev environment but if I try to query it when hosted in IIS using my little test app I get a '404 Not Found' exception. But what is puzzling me is if I connect to the web service hosted in IIS from chrome it works?

My controller method is as follows:

[HttpGet]
public IHttpActionResult GetToken(string username, string password, string realm)
{            
    return Json("TOKEN-STRING");
}

And my config:

<system.webServer>
<validation validateIntegratedModeConfiguration="false" />    
<modules runAllManagedModulesForAllRequests="true">
  <remove name="UrlRoutingModule-4.0" />
  <add name="UrlRoutingModule-4.0" type="System.Web.Routing.UrlRoutingModule" preCondition="" />
</modules>
<handlers>
  <remove name="ExtensionlessUrlHandler-ISAPI-4.0_32bit" />
  <remove name="ExtensionlessUrlHandler-ISAPI-4.0_64bit" />
  <remove name="ExtensionlessUrlHandler-Integrated-4.0" />
  <remove name="OPTIONSVerbHandler" />
  <remove name="TRACEVerbHandler" />
  <add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="*" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
  <add name="ExtensionlessUrlHandler-ISAPI-4.0_64bit" path="*." verb="GET,HEAD,POST,PUT,DELETE,DEBUG" modules="IsapiModule" scriptProcessor="%windir%\Microsoft.NET\Framework64\v4.0.30319\aspnet_isapi.dll" resourceType="Unspecified" requireAccess="Script" preCondition="classicMode,runtimeVersionv4.0,bitness64" responseBufferLimit="0" />
  <add name="ExtensionlessUrlHandler-ISAPI-4.0_32bit" path="*." verb="GET,HEAD,POST,PUT,DELETE,DEBUG" modules="IsapiModule" scriptProcessor="%windir%\Microsoft.NET\Framework\v4.0.30319\aspnet_isapi.dll" resourceType="Unspecified" requireAccess="Script" preCondition="classicMode,runtimeVersionv4.0,bitness32" responseBufferLimit="0" />
</handlers>

I have set my application pool to 'Integrated' within IIS.

My logic says to me that if I am able to retrieve the token from Chrome but not from my app, then there should be nothing wrong with IIS but rather a config\\setting\\programming error in my app.

Here is the little method I wrote to get the token:

async private void GetToken()
    {
        HttpClient client = new HttpClient();

        client.BaseAddress = new Uri("http://localhost/TokenServer/");
        client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

        var response = client.GetAsync("/api/TokenServer/GetToken/name/password/realm").Result;
        string result =  await response.Content.ReadAsStringAsync();

        if (!response.IsSuccessStatusCode)
        {
            //Error handling
            return;
        }

        //Extract token from response
        JavaScriptSerializer json_serializer = new JavaScriptSerializer();
        Token = DeserializeJsonToken(result);
    }

I have read and tried many suggestions but none seem to work for me. I have been stuck with this for two days now and the deadline is creeping :)

Tx

The issue here is with the Url where you have a leading / which causes the TokenServer part of the baseaddress Url to be not considered. So modify the call like the following:

client.GetAsync("api/TokenServer/GetToken/name/password/realm").Result;

A tip: Tools like Fiddler can be of great use in situations like these. It helps in diagnosing the raw requests that are sent to the service.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM