我在c#中創建了一個寧靜的web服務。我在調用POST方法時遇到400錯誤的請求錯誤。我正在檢查我的請求在提琴手和POST請求是完全正常的。我不明白我的程序有什麼問題。REST服務獲取400錯誤請求
以下是一些代碼片段。如果您需要任何其他代碼,請查看。
接口
[ServiceContract]
public interface IRead
{
[OperationContract]
[WebInvoke(UriTemplate = "GetCard", Method = "POST")]
someObject GetCard(Session session);
}
的NFCSession是具有名稱會話的int變量的對象。
客戶端生成POST請求
public void GetCard()
{
string strGetCard = "http://localhost:8384/Reader/GetCard";
byte[] dataByte = GenerateNFCSession(63315152);
HttpWebRequest POSTRequest = (HttpWebRequest)WebRequest.Create(strGetCard);
POSTRequest.Method = "POST";
POSTRequest.ContentType = "application/xml;charset=UTF-8";
POSTRequest.ContentLength = dataByte.Length;
Stream POSTstream = POSTRequest.GetRequestStream();
POSTstream.Write(dataByte, 0, dataByte.Length);
HttpWebResponse POSTResponse = (HttpWebResponse)POSTRequest.GetResponse();
StreamReader reader =
new StreamReader(POSTResponse.GetResponseStream(), Encoding.UTF8);
Console.WriteLine("Response");
Console.WriteLine(reader.ReadToEnd().ToString());
}
XML生成
private static byte[] GenerateXML(int sessionID)
{
MemoryStream mStream = new MemoryStream();
XmlTextWriter xmlWriter = new XmlTextWriter(mStream, Encoding.UTF8);
xmlWriter.Formatting = Formatting.Indented;
xmlWriter.WriteStartDocument();
xmlWriter.WriteStartElement("Session");
xmlWriter.WriteStartElement("session");
xmlWriter.WriteString(sessionID.ToString());
xmlWriter.WriteEndElement();
xmlWriter.WriteEndElement();
xmlWriter.WriteEndDocument();
xmlWriter.Flush();
xmlWriter.Close();
Console.WriteLine(mStream.ToArray().ToString());
return mStream.ToArray();
}
的web.config
<?xml version="1.0"?>
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.0" />
</system.web>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true">
<add name="UrlRoutingModule" type="System.Web.Routing.UrlRoutingModule,
System.Web,Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
</modules>
</system.webServer>
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior>
<serviceMetadata httpGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="false"/>
</behavior>
</serviceBehaviors>
</behaviors>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true"/>
<standardEndpoints>
<webHttpEndpoint>
<standardEndpoint name="" helpEnabled="true" faultExceptionEnabled="true"
automaticFormatSelectionEnabled="true"></standardEndpoint>
</webHttpEndpoint>
</standardEndpoints>
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true"/>
</system.webServer>
</configuration>
POST請求從提琴手
XML<?xml version="1.0" encoding="utf-8"?>
<Session>
<session>63315152</session>
</Session>
我得到這個錯誤
The server encountered an error processing the request.
注:我公司提供XmlSerializer的格式屬性,它工作正常。但是我希望我的服務能在以後的JSON請求中工作。從我的角度來看,沒有必要明確提供格式屬性。我真的不明白這段代碼有什麼問題。
夥計請幫助!
如果REST Api,離開WCF,使用ASP.NET web Api,這很簡單 – 2013-02-19 15:24:26
你的意思是ASP.NET MVC 4? – Newbee 2013-02-19 15:38:47
鏈接:http://www.asp.net/web-api,Web Api不是ASP.NET MVC 4,兩者只是在相同的包中 – 2013-02-19 15:40:52