0

我正在尝试使用 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

有没有人可以帮帮我。

4

2 回答 2

1

这个网址看起来很奇怪:

Dim url As String = "http://localhost:8003/ItemTracker/ItemTrackerService.svc?wsdl/GetItemTrackingDetails/"

如果你删除它会更好?wsdl吗?

Dim url As String = "http://localhost:8003/ItemTracker/ItemTrackerService.svc/GetItemTrackingDetails/"
于 2012-05-29T09:08:50.133 回答
0

您不需要指定实际服务的 url,只需 url 掩码:

http://localhost:8003/ItemTracker/GetItemTrackingDetails/

此外,您必须在 url 掩码中添加一个占位符以包含操作的输入参数:

UriTemplate:="GetItemTrackingDetails/{0}"

这允许您调用实际的 url 并在 url 的末尾传递 TrackingNo 变量:

http://localhost:8003/ItemTracker/GetItemTrackingDetails/999AAA
于 2012-05-29T10:07:04.473 回答