【问题标题】:RESTful WCF returns error code 415?RESTful WCF 返回错误代码 415?
【发布时间】:2011-11-08 01:46:11
【问题描述】:

当我针对我创建的 RESTful Web 服务运行客户端时,我收到了 {"The remote server returned an error: (415) Unsupported Media Type."}。客户端中的内容类型正确。该服务在 vs 2010 中的 WCF 测试客户端下正常运行。我认为我的问题出在我的客户端代码或我发送/格式化 XML 的方式中。非常感谢任何建议。

使用VS2010,.net4

网络服务代码:

public Charge GetCharge(Charge aCharge)
    {
        return aCharge;
    }

[ServiceContract(Namespace = "http://www.test.com/Charge")]
public interface IService1
{

    [OperationContract]
    [WebInvoke(Method = "POST", UriTemplate = "getcharge",
      RequestFormat = WebMessageFormat.Xml,
      ResponseFormat = WebMessageFormat.Xml,
      BodyStyle = WebMessageBodyStyle.Bare)]
    Charge GetCharge(Charge aCharge);
}


// Use a data contract as illustrated in the sample below to add composite types to service operations.
[DataContract(Namespace = "http://www.test.com/Charge")]
public class Charge
{
    [DataMember]
    public string ID
    {
        get;
        set;
    }

    [DataMember]
    public string Hours
    {
        get;
        set;
    }


    [DataMember]
    public string Comment
    {
        get;
        set;
    }

    [DataMember]
    public string DateT
    {
        get;
        set;
    }
}

客户端代码:

HttpWebRequest req = null;
        HttpWebResponse res = null;
        try
        {
            string url = "http://localhost:1657/Service1.svc/getcharge";
            req = (HttpWebRequest)WebRequest.Create(url);
            req.Method = "POST";
            req.ContentType = "application/xml";
            req.Timeout = 30000;
            req.Headers.Add("SOAPAction", url);

            System.Xml.XmlDocument xmlDoc = new System.Xml.XmlDocument();
            xmlDoc.XmlResolver = null;
            xmlDoc.Load(@"c:\file\benCharge.xml");
            string sXML = xmlDoc.InnerXml;
            req.ContentLength = sXML.Length;
            System.IO.StreamWriter sw = new System.IO.StreamWriter(req.GetRequestStream());
            sw.Write(sXML);
            sw.Close();

            res = (HttpWebResponse)req.GetResponse();

        }
        catch (Exception ex)
        {
            System.Console.WriteLine(ex.Message);
        }

客户端 XML:

<Charge xmlns="http://www.test.com/Charge"><ID>1</ID><Hours>10</Hours><Comment>Mobile app development</Comment><DateT>01/01/2011</DateT></Charge>

【问题讨论】:

    标签: .net wcf web-services rest httpresponse


    【解决方案1】:

    您能否在客户端创建相同的数据协定,而不是从 xml 文件中读取创建对象,将其转换为字节数组并将其发布到流中,然后查看您的请求是否成功。

    确保使用 Fiddler 监控您的请求。

    我猜错误的原因与您的 xml 文件有关。您只需要确保使用 DataContractSerializer 反序列化时的 xml 以与您的对象相同的格式返回。

    一些使用 HttpWebRequest 对象发布的代码:

    byte[] requestBodyBytes = ToByteArrayUsingDataContractSer(requestBody);
                    request.ContentLength = requestBodyBytes.Length;
                    using (Stream postStream = request.GetRequestStream())
                        postStream.Write(requestBodyBytes, 0, requestBodyBytes.Length);
    
    
    private static byte[] ToByteArrayUsingDataContractSer<T>(T requestBody)
            {
                byte[] bytes = null;
                var serializer1 = new DataContractSerializer(typeof(T));
                var ms1 = new MemoryStream();
                serializer1.WriteObject(ms1, requestBody);
                ms1.Position = 0;
                var reader = new StreamReader(ms1);
                bytes = ms1.ToArray();
                return bytes;
            }
    

    我猜你的客户没有将消息作为它需要的消息正文的一部分传递。希望以上信息对您有所帮助。

    【讨论】:

      【解决方案2】:

      您是否使用 Fiddler 检查了线路上的实际消息标头?检查 ContentType(即您发送的内容)和 Accept(您想要返回的内容)。任何一种 MIME 类型都可能导致此问题。

      【讨论】:

        【解决方案3】:

        我有点困惑 - 你说这是一个 RESTful 服务,但稍后在你的帖子中你展示了一个名为 SOAP 的东西 - SOAP 和 REST 不一样!

        使用FiddlerWireshark 查看您的客户端和工作客户端之间的差异,然后进行相应更改。

        对于与您类似的配置中的工作源代码,请参阅http://social.msdn.microsoft.com/Forums/en/wcf/thread/d11a7997-d60b-4895-8f69-9ef2175087e2

        基本上,他们在ContentType 中添加了charset,并且他们不在标题中使用SOAPAction

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2014-01-17
          • 2017-01-26
          • 2019-01-26
          • 2011-08-31
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-07-18
          相关资源
          最近更新 更多