来自webservice的400错误

时间:2011-07-01 15:32:00

标签: c# wcf http

有人可以向我解释为什么我在尝试发布到我的网络服务时出现http 400错误吗?

我的服务合约::

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

我的电话;

    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();
    }

我的服务器配置文件

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

编辑::当使用MediaType“text / plain”

时,它也会出现相同的错误

2 个答案:

答案 0 :(得分:3)

您的内容类型为text / xml,但您的实际内容只是“dfwa”。这不是一个有效的XML文档。

(顺便说一下,您还应该使用usingreq.GetResponse()。)

答案 1 :(得分:1)

默认情况下,具有webHttpBinding / webHttp行为的端点接受XML或JSON格式的请求。您发送的XML需要符合服务期望的内容。以下代码发送您的服务所需的请求。另请注意,您需要在HttpWebRequest上设置 ContentType 属性,而不是 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();
    }
}
相关问题