C# 400来自webservice的错误

C# 400来自webservice的错误,c#,wcf,http,C#,Wcf,Http,有人能解释一下为什么我在尝试发布到我的Web服务时遇到http 400错误吗 我的服务合同: [ServiceContract] public interface IfldtWholesaleService { [OperationContract] [WebInvoke(Method = "POST", ResponseFormat = WebMessageFormat.Xml, BodyStyle = WebMessageBodyStyle.W

有人能解释一下为什么我在尝试发布到我的Web服务时遇到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”时也会出现相同的错误。

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


(顺便说一下,您还应该为
req.GetResponse()
使用
using
块。)

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


(顺便说一句,对于
req.GetResponse()
,您还应该使用
using
块。)

默认情况下,具有webHttpBinding/webHttp行为的端点接受XML或JSON格式的请求。您发送的XML需要符合服务的期望。下面的代码发送您的服务期望的请求。另外,请注意,您需要在HttpWebRequest上设置ContentType属性,而不是MediaType

公共类堆栈溢出\u 6550019
{
[服务合同]
公共接口IFLDTwesteralService
{
[经营合同]
[WebInvoke(Method=“POST”,
ResponseFormat=WebMessageFormat.Xml,
BodyStyle=WebMessageBodyStyle.Wrapped,
UriTemplate=“MAC”)]
字符串MAC(字符串输入);
}
公共类服务:IFLDTwesteralService
{
公共字符串MAC(字符串输入)
{
返回输入;
}
}
私有静态void postToWebsite(字符串url)
{
HttpWebRequest req=(HttpWebRequest)HttpWebRequest.Create(url);
请求方法=“POST”;
req.ContentType=“text/xml”;
字符串输入=@“hello”;
StreamWriter writer=新的StreamWriter(req.GetRequestStream());
writer.Write(输入);
writer.Close();
var rsp=req.GetResponse().GetResponseStream();
Console.WriteLine(新的StreamReader(rsp).ReadToEnd());
}
公共静态无效测试()
{
string baseAddress=“http://“+Environment.MachineName+”:8000/Service/”;
WebServiceHost主机=新的WebServiceHost(类型(服务),新的Uri(基地址));
host.Open();
Console.WriteLine(“主机已打开”);
//要找到预期的请求,请使用WCF客户端。查看它在Fiddler中发送的内容
var factory=new-WebChannelFactory(新Uri(基地址));
var proxy=factory.CreateChannel();
MAC(“你好世界”);
postToWebsite(基本地址+“/MAC”);
控制台。写入(“按ENTER键关闭主机”);
Console.ReadLine();
host.Close();
}
}

默认情况下,具有webHttpBinding/webHttp行为的端点接受XML或JSON格式的请求。您发送的XML需要符合服务的期望。下面的代码发送您的服务期望的请求。另外,请注意,您需要在HttpWebRequest上设置ContentType属性,而不是MediaType

公共类堆栈溢出\u 6550019
{
[服务合同]
公共接口IFLDTwesteralService
{
[经营合同]
[WebInvoke(Method=“POST”,
ResponseFormat=WebMessageFormat.Xml,
BodyStyle=WebMessageBodyStyle.Wrapped,
UriTemplate=“MAC”)]
字符串MAC(字符串输入);
}
公共类服务:IFLDTwesteralService
{
公共字符串MAC(字符串输入)
{
返回输入;
}
}
私有静态void postToWebsite(字符串url)
{
HttpWebRequest req=(HttpWebRequest)HttpWebRequest.Create(url);
请求方法=“POST”;
req.ContentType=“text/xml”;
字符串输入=@“hello”;
StreamWriter writer=新的StreamWriter(req.GetRequestStream());
writer.Write(输入);
writer.Close();
var rsp=req.GetResponse().GetResponseStream();
Console.WriteLine(新的StreamReader(rsp).ReadToEnd());
}
公共静态无效测试()
{
string baseAddress=“http://“+Environment.MachineName+”:8000/Service/”;
WebServiceHost主机=新的WebServiceHost(类型(服务),新的Uri(基地址));
host.Open();
Console.WriteLine(“主机已打开”);
//要找到预期的请求,请使用WCF客户端。查看它在Fiddler中发送的内容
var factory=new-WebChannelFactory(新Uri(基地址));
var proxy=factory.CreateChannel();
MAC(“你好世界”);
postToWebsite(基本地址+“/MAC”);
控制台。写入(“按ENTER键关闭主机”);
Console.ReadLine();
host.Close();
}
}

请不要使用“ascienceoding.UTF8”-使用“Encoding.UTF8”。(ASCII和UTF8不是一回事。这段代码可以工作,但你真的不应该使用它。)请共享客户端和服务器的相关配置部分。我添加了服务器配置文件。除了im用于消费的项目的默认appconfig之外,没有其他内容请不要使用“ascienceoding.UTF8”-使用“Encoding.UTF8”。(ASCII和UTF8不是一回事。这段代码可以工作,但你真的不应该使用它。)请共享客户端和服务器的相关配置部分。我添加了服务器配置文件。除了我用来消费的项目的默认appconfig之外,没有其他内容更改为纯文本help@Tom:我猜web服务可能需要XML。。。或者可能形成编码数据。我怀疑你能否逃脱一个仅仅是输入参数值的实体
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();
    }
}