作成したRESTful Webサービスに対してクライアントを実行すると、{"The remote server returned an error: (415) Unsupported Media Type."}
が表示されます。コンテンツタイプはクライアントで正しいです。このサービスは2010年のWCFテストクライアントの下で正しく動作します。私の問題はクライアントコード内にあると思います。あるいはXMLを送信/書式設定する方法です。どんなアドバイスも大いに感謝しています。RESTful WCFがエラーコード415を返しますか?
VS2010を使用して、.net4
Webサービスコード:
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>