我在c#中创建了一个安静的Web服务。我调用POST方法时收到400错误请求错误。我在fiddler检查我的请求,POST请求完全没问题。我不明白我的计划有什么问题。
以下是一些代码段。如果您有任何其他代码要查看,请询问。
接口
[ServiceContract]
public interface IRead
{
[OperationContract]
[WebInvoke(UriTemplate = "GetCard", Method = "POST")]
someObject GetCard(Session session);
}
NFCSession是一个具有名称session的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>
来自Fiddler的POST请求XML
<?xml version="1.0" encoding="utf-8"?>
<Session>
<session>63315152</session>
</Session>
我收到此错误
The server encountered an error processing the request.
注意:当我提供XmlSerializer格式属性时,它工作正常。但是我希望我的服务能够在稍后的时候为JSON请求工作。从我的观点来看,没有必要明确提供format属性。我真的不明白这段代码有什么问题。
伙计们请帮助!!!
答案 0 :(得分:0)
尝试在行为标记内添加<enableWebScript />
:
...
<behaviors>
<serviceBehaviors>
<behavior>
<serviceMetadata httpGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="false"/>
<enableWebScript />
</behavior>
</serviceBehaviors>
</behaviors>
...
这里讨论:http://social.msdn.microsoft.com/Forums/en/wcf/thread/fb48999f-cd41-49b8-ae9a-f0c8f5aebbd2
答案 1 :(得分:0)
我的网络服务现在正常运作。
我所做的更改是在DataContract属性中。
最初是这样的:
[DataContract]
我把它更改为:
[DataContract(Namespace="")]
它开始工作......
提示:运行Web服务时,您可以在Web服务的“/ help”页面中看到示例POST请求。我从来没有关注过这个页面。但它非常有用。当我将这个例子与Fiddler中的实际请求进行比较时,我发现命名空间是不同的。 :d