CPK_2011
CPK_2011

Reputation: 1150

The remote server returned an error: (400) Bad Request

I am trying to consume the WCF Restful Service. The Service config is as follows

<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>

The Interface is defined as follows

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 

The Calling of Restful service in client application is as follows

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)

The input xml is this.

<GetItemTrackingDetails xmlns="http://localhost:8003/ItemTracker/"> 
<TrackingNo>A10001</TrackingNo> 
</GetItemTrackingDetails>

Instead of localhost system name is used

The output of GetItemTrackingDetails is xml. With this I am getting bad request 400 instead of xml

Is there anyone to help me out.

Upvotes: 0

Views: 3384

Answers (2)

tom redfern
tom redfern

Reputation: 31800

You don't need to specify the url to the actual service, just the url mask:

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

Furthermore you have to add a placeholder into your url mask to contain the input parameter to your operation:

UriTemplate:="GetItemTrackingDetails/{0}"

This allows you to call the actual url and pass the TrackingNo variable at the end of the url:

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

Upvotes: 0

Anders Lindahl
Anders Lindahl

Reputation: 42940

This url looks strange:

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

Does it work better if you remove ?wsdl?

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

Upvotes: 1

Related Questions