locked
The remote server returned an error: (400) Bad Request. RRS feed

  • Question

  • Hi guys,

        I am getting this error for the following Restful WCF Service client. When the content type is "text/xml" it is asking to change it to "application/soap+xml; charset=utf-8". When I changed to this, I am getting bad request.

            Dim content As String
            Dim Method As String = "POST"
            Dim uri As String = "http://localhost:8003/Item/ItemService.svc"
            Dim req As HttpWebRequest = TryCast(WebRequest.Create(uri), HttpWebRequest)
            req.KeepAlive = False
            req.Method = Method.ToUpper()
            Dim FilePath As String = AppDomain.CurrentDomain.BaseDirectory & "\test.xml"
            content = (File.OpenText(FilePath)).ReadToEnd()
            Dim buffer As Byte() = Encoding.ASCII.GetBytes(Content)
            req.ContentLength = buffer.Length
            req.ContentType = "application/soap+xml; charset=utf-8"
            Dim PostData As Stream = req.GetRequestStream()
            PostData.Write(buffer, 0, buffer.Length)
            PostData.Close()
            Dim resp As HttpWebResponse = TryCast(req.GetResponse(), HttpWebResponse)
            Dim enc As Encoding = System.Text.Encoding.GetEncoding(1252)
            Dim loResponseStream As New StreamReader(resp.GetResponseStream(), enc)
            Dim Response1 As String = loResponseStream.ReadToEnd()
            loResponseStream.Close()
            resp.Close()
            Response.Write(Response1)

    The WCF Restful Service Interface is as follows

    Imports System.ServiceModel.Web
    <ServiceContract([Namespace]:="http://localhost:8003/Item/")> _
    Public Interface IItemService
        <OperationContract()> _
        <WebInvoke(Method:="POST", RequestFormat:=WebMessageFormat.Xml, ResponseFormat:=WebMessageFormat.Xml, UriTemplate:="GetItemDetails")> _
        Function GetItemDetails(ByVal ItemNumber As String) As String
    End Interface

    The input xml for the REST Client is as follows

    <GetItemDetails xmlns="http://localhost:8003/Item/">  <ItemNumber>A10001</ItemNumber></GetItemDetails>


    Regards, CPK_2011

    Tuesday, May 29, 2012 12:51 PM

Answers

  • The default "body style" of the `<WebInvoke>` attribute is "Bare", which means that the input must be sent without a wrapping for the operation name. Since your input is wrapped, you must set the body style accordingly:

    <ServiceContract([Namespace]:="http://localhost:8003/Item/")> _
    Public Interface IItemService
        <OperationContract()> _
        <WebInvoke(Method:="POST", RequestFormat:=WebMessageFormat.Xml, ResponseFormat:=WebMessageFormat.Xml, UriTemplate:="GetItemDetails", BodyStyle:=WebMessageBodyStyle.WrappedRequest)> _
        Function GetItemDetails(ByVal ItemNumber As String) As String
    End Interface


    Carlos Figueira

    Tuesday, May 29, 2012 4:30 PM