开发者

400 error from webservice

Can anyone explain to me why im getting a http 400 error when trying to post to my webservice?

My service contract ::

[ServiceContract]
public interface IfldtWholesaleService {
    [OperationContract]
    [WebInvoke(Method = "POST",
        ResponseFormat = WebMessageFormat.Xml,
        BodyStyle = WebMessageBodyStyle.Wrapped,
        UriTemplate = "MAC")]
    string MAC(string input);

My call;

    private void postToWebsite()
    {
        HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(txtUrl.Text);
        req.Method = "POST";
        req.MediaType = "text/xml";


        string input = "dfwa";
        req.ContentLength = ASCIIEncoding.UTF8.GetByteCount(input);

        StreamWriter writer = new StreamWriter(req.GetRequestStream());
        writer.Write(input);
        writer.Close();
        var rsp = req.GetResponse().GetResponseStream();

        txtOut.Text = new StreamReader(rsp).ReadToEnd();
    }

My server config file

<system.serviceModel>
    <services>
        <service name="fldtRESTWebservice.fldtWholesaleService" behaviorConfiguration="httpBehaviour">
            <endpoint address="" binding="webHttpBinding" contract="fldtRESTWebservice.IfldtWholesaleService" behaviorConfiguration="httpEndpointBehavour">
                <identity>
                    <dns value="localhost"/>
                </identity>
            </endpoint>
            <endpoint address="mex"开发者_开发百科 binding="mexHttpBinding" contract="IMetadataExchange"/>
            <host>
                <baseAddresses>
                    <add baseAddress="http://localhost:8080/ContactService/"/>
                </baseAddresses>
            </host>
        </service>
    </services>
    <behaviors>
        <serviceBehaviors>
            <behavior name="httpBehaviour">
                <serviceMetadata httpGetEnabled="True"/>
                <serviceDebug includeExceptionDetailInFaults="False"/>
            </behavior>
        </serviceBehaviors>
        <endpointBehaviors>
            <behavior name="httpEndpointBehavour">
                <webHttp/>
            </behavior>
        </endpointBehaviors>
    </behaviors>
</system.serviceModel>

EDIT :: it also gives the same error when using MediaType "text/plain"


Your content type is text/xml, but your actual content is just "dfwa". That's not a valid XML document.

(You should also use a using block for req.GetResponse() by the way.)


An endpoint with webHttpBinding / webHttp behavior by default accepts requests in either XML or JSON format. And the XML you send needs to conform with what the service expects. The code below sends a request which your service expects. Also, notice that you need to set the ContentType property on the HttpWebRequest, not MediaType.

public class StackOverflow_6550019
{
    [ServiceContract]
    public interface IfldtWholesaleService
    {
        [OperationContract]
        [WebInvoke(Method = "POST",
            ResponseFormat = WebMessageFormat.Xml,
            BodyStyle = WebMessageBodyStyle.Wrapped,
            UriTemplate = "MAC")]
        string MAC(string input);
    }

    public class Service : IfldtWholesaleService
    {
        public string MAC(string input)
        {
            return input;
        }
    }

    private static void postToWebsite(string url)
    {
        HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(url);
        req.Method = "POST";
        req.ContentType = "text/xml";

        string input = @"<MAC xmlns=""http://tempuri.org/""><input>hello</input></MAC>";

        StreamWriter writer = new StreamWriter(req.GetRequestStream());
        writer.Write(input);
        writer.Close();
        var rsp = req.GetResponse().GetResponseStream();

        Console.WriteLine(new StreamReader(rsp).ReadToEnd());
    }

    public static void Test()
    {
        string baseAddress = "http://" + Environment.MachineName + ":8000/Service/";
        WebServiceHost host = new WebServiceHost(typeof(Service), new Uri(baseAddress));
        host.Open();
        Console.WriteLine("Host opened");

        // To find out the expected request, using a WCF client. Look at what it sends in Fiddler
        var factory = new WebChannelFactory<IfldtWholesaleService>(new Uri(baseAddress));
        var proxy = factory.CreateChannel();
        proxy.MAC("Hello world");

        postToWebsite(baseAddress + "/MAC");

        Console.Write("Press ENTER to close the host");
        Console.ReadLine();
        host.Close();
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜