简体   繁体   中英

Bad Request when sending request to WCF Service?

When I use the methods below to send an xml request to an asmx service, it works fine, the only difference is is that the content type is application/soap+xml . I am getting the error: 400 Bad Request. Here is the method I am using below to send the request via HTTP Post to the WCF Service:

private static void SendRequest(string request)
{
    var req = (HttpWebRequest) WebRequest.Create("http://urltoservice.svc");
    req.ContentType = "text/xml";
    req.Method = "POST";

    using (var stm = req.GetRequestStream())
    {
        using (var stmw = new StreamWriter(stm))
        {

            stmw.Write(request);
        }
    }


    byte[] myData;
    using (var webResponse = req.GetResponse())
    {

        var responseStream = webResponse.GetResponseStream();
        myData = ReadFully(responseStream);
    }

    // Do whatever you need with the response
    string responseString = Encoding.ASCII.GetString(myData);
}

It seems to throw it at the line: var webResponse = req.GetResponse()

What is the type of service that you are trying to call. Is it REST WCF service or SOAP WCF Service?

You can monitor your request using Fiddler to see how your request looks when it works and when it doesnt.

Also enable Tracing on your service to know why you get a 400 Bad Request.

No idea why this works, if someone can explain it it would be great. I needed to append the method name to URI in order for it to work, for example,

http://urltoservice.svc/MethodToCall

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM