我正在尝试使用 WCF Restful 服务。服务配置如下
<system.serviceModel>
<bindings>
<wsHttpBinding>
<binding name="wsHttp" maxReceivedMessageSize ="50000000" maxBufferPoolSize="50000000" >
<readerQuotas maxDepth="500000000" maxArrayLength="500000000" maxBytesPerRead="500000000" maxNameTableCharCount="500000000" maxStringContentLength="500000000"/>
<security mode="None" />
</binding>
</wsHttpBinding>
</bindings>
<services>
<service behaviorConfiguration="ItemTracker.ItemTrackerServiceBehavior" name="ItemTracker.ItemTrackerService">
<endpoint address="http://localhost:8003/ItemTracker/ItemTrackerService.svc" binding="wsHttpBinding" contract="ItemTracker.IItemTrackerService" bindingConfiguration="wsHttp">
<identity>
<dns value="localhost"/>
</identity>
</endpoint>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="ItemTracker.ItemTrackerServiceBehavior">
<!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment -->
<serviceMetadata httpGetEnabled="true"/>
<!-- To receive exception details in faults for debugging purposes, set the value below to true. Set to false before deployment to avoid disclosing exception information -->
<serviceDebug includeExceptionDetailInFaults="true"/>
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
接口定义如下
Imports System.ServiceModel.Web
<ServiceContract([Namespace]:="http://localhost:8003/ItemTracker/")> _
Public Interface IItemTrackerService
<OperationContract()> _
<WebInvoke(Method:="POST", RequestFormat:=WebMessageFormat.Xml, ResponseFormat:=WebMessageFormat.Xml, UriTemplate:="GetItemTrackingDetails")> _
Function GetItemTrackingDetails(ByVal TrackingNo As String) As String
End Interface
客户端应用中Restful服务的调用如下
Dim req As HttpWebRequest = Nothing
Dim res As HttpWebResponse = Nothing
Dim url As String = "http://localhost:8003/ItemTracker/ItemTrackerService.svc?wsdl/GetItemTrackingDetails/"
req = DirectCast(WebRequest.Create(url), HttpWebRequest)
req.Method = "POST"
req.ContentType = "application/soap+xml; charset=utf-8"
req.Timeout = 30000
req.Headers.Add("SOAPAction", url)
Dim xmlDoc As New System.Xml.XmlDocument()
xmlDoc.XmlResolver = Nothing
xmlDoc.Load(AppDomain.CurrentDomain.BaseDirectory & "\test.xml")
Dim sXML As String = xmlDoc.InnerXml
req.ContentLength = sXML.Length
Dim sw As New System.IO.StreamWriter(req.GetRequestStream())
sw.Write(sXML)
sw.Close()
res = DirectCast(req.GetResponse(), HttpWebResponse)
输入xml是这样的。
<GetItemTrackingDetails xmlns="http://localhost:8003/ItemTracker/">
<TrackingNo>A10001</TrackingNo>
</GetItemTrackingDetails>
代替 localhost 系统名称被使用
GetItemTrackingDetails 的输出是 xml。有了这个,我得到了错误的请求 400 而不是 xml
有没有人可以帮帮我。