远程服务器返回错误:(400)错误请求

时间:2012-05-29 09:00:32

标签: wcf

我正在尝试使用WCF Restful Service。服务配置如下

<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

有没有人可以帮助我。

2 个答案:

答案 0 :(得分: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/"

答案 1 :(得分:0)

您无需为实际服务指定网址,只需指定网址掩码:

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

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

UriTemplate:="GetItemTrackingDetails/{0}"

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

http://localhost:8003/ItemTracker/GetItemTrackingDetails/999AAA