Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/321.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Webapi中需要JSON/XML数据_C#_Asp.net Web Api_Webrequest_Httpwebresponse - Fatal编程技术网

C# Webapi中需要JSON/XML数据

C# Webapi中需要JSON/XML数据,c#,asp.net-web-api,webrequest,httpwebresponse,C#,Asp.net Web Api,Webrequest,Httpwebresponse,当我请求一个webapi请求时,我得到了一个错误,比如需要JSON/XML数据 try { string oRequest = _xml.UpdateInvereqxml(UserName, Password,OTAhotelid, Invy); string uri = @"service/update"; System.Net.WebRequest req = System.Net.We

当我请求一个webapi请求时,我得到了一个错误,比如需要JSON/XML数据

try         
{                
    string oRequest = _xml.UpdateInvereqxml(UserName, Password,OTAhotelid, Invy);                
    string uri = @"service/update";
    System.Net.WebRequest req = System.Net.WebRequest.Create(uri);
    req.Method = "POST";
    req.ContentType = "text/xml";
    System.IO.StreamWriter writer = new System.IO.StreamWriter(req.GetRequestStream());
    writer.WriteLine(oRequest);
    writer.Close();
    System.Net.WebResponse rsp = req.GetResponse();
    Stream istrm = rsp.GetResponseStream();
    string StreamReader = new StreamReader(istrm).ReadToEnd();
}

我建议使用HttpClient与ASP.NET Web API通信。该类提供了许多使用REST服务()的函数


如何解决上述错误?可能是服务器引发了异常,因为您发送了错误的内容类型。或者可能您缺少正确的接受标题
public EnumProduct Post(EnumProduct product)
{
HttpResponseMessage reponse = httpClient.PostAsJsonAsync("api/enumproducts/Post", product).Result;
if (reponse.IsSuccessStatusCode)
{
var enumProduct = reponse.Content.ReadAsAsync().Result;
return enumProduct;
}
else
return null;
}