使用HttpClient进行404 NotFound

时间:2014-09-10 17:34:17

标签: asp.net-web-api dotnet-httpclient

我正在使用HttpClient类(来自命名空间System.Net.Http)将POST数据发送到Windows窗体应用程序中的asp.net Web API操作。我的问题是我一直收到未找到状态码404 (找不到端点)。我错过了什么吗?请帮忙。提前谢谢。

以下是我的C#代码,它使用HttpClient向XML Web API发出POST请求:

private async Task PostCustomForm()
        {

            using (var client = new System.Net.Http.HttpClient())
            {
                NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger();

                try
                {
                    CustomFormLog customFormLog = new CustomFormLog();
                    customFormLog.DeviceId = 1234;
                    customFormLog.UserExperienceId = 33442036854775807;
                    customFormLog.NetworkItemId = 58595555;


                    client.BaseAddress = new Uri("http://xyzwebserver/");
                    client.DefaultRequestHeaders.Accept.Clear();
                    client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/xml"));

                    HttpResponseMessage response = await client.PostAsXmlAsync<CustomFormLog>("logdata/customform/1234", customFormLog); 



                    if (response.IsSuccessStatusCode)
                    {
                        logger.Info(response.IsSuccessStatusCode);

                    }
                    else
                    {
                        logger.Info(response);

                    }
                }
                catch (HttpRequestException ex)
                {
                    logger.Error(ex.Message + " - " + ex.InnerException.Message);
                    MessageBox.Show(ex.Message + " - " + ex.InnerException.Message);
                }
            }
        }

以下是来自该XML Web API网站的帮助文本:

Url: http://xyzwebserver/logdata/customform?device={DEVICEID}

HTTP Method: POST

Message direction   Format  Body
Request Xml Example,Schema
Response    N/A The Response body is empty.
The following is an example request Xml body:

<CustomFormLog>
  <OptIn>true</OptIn>
  <UserExperienceId>3372036854775807</UserExperienceId>
  <NetworkItemId>64093484</NetworkItemId>
  <FormData />
</CustomFormLog>
The following is the request Xml Schema:

<xs:schema elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="CustomFormLog" nillable="true" type="CustomFormLog" />
  <xs:complexType name="CustomFormLog">
    <xs:sequence>
      <xs:element minOccurs="1" maxOccurs="1" name="OptIn" type="xs:boolean" />
      <xs:element minOccurs="1" maxOccurs="1" name="UserExperienceId" type="xs:long" />
      <xs:element minOccurs="1" maxOccurs="1" name="NetworkItemId" type="xs:int" />
      <xs:element minOccurs="0" maxOccurs="1" name="FormData">
        <xs:complexType>
          <xs:sequence>
            <xs:any processContents="lax" />
          </xs:sequence>
        </xs:complexType>
      </xs:element>
    </xs:sequence>
  </xs:complexType>
</xs:schema>

最后,我的自定义DTO课程:

public class CustomFormLog
    {
        public int DeviceId { get; set; }
        public bool OptIn { get;set;}
        public long UserExperienceId { get; set; }
        public int NetworkItemId { get; set; }


        public XElement FormData { get; set; }
    }

2 个答案:

答案 0 :(得分:2)

好的,从帮助信息来看,似乎已经过了的网址是

Url: http://xyzwebserver/logdata/customform?device={DEVICEID}

但你发送的是

Url: http://xyzwebserver/logdata/customform/1234

这不会转到正确的URL,直到web api route config在其路由配置中定义了{device}。我建议使用他们提到的url格式并将Requesting line更改为

HttpResponseMessage response = 
await client.PostAsXmlAsync<CustomFormLog>("logdata/customform?device=1234", customFormLog); 

答案 1 :(得分:0)

检查你的route.config文件,你可能没有指向控制器。我不能确定它应该是什么,因为你还没有发布配置文件。要注意的是,如果使用默认的Web API模板,将会有两个路径配置文件,一个用于api控制器,另一个用于基本控制器。我的猜测是你的控制器在它的路由中有默认的/ api /。在这种情况下,您的问题的修复将是

HttpResponseMessage response = await client.PostAsXmlAsync<CustomFormLog>("api/logdata/customform/1234", customFormLog);