2011-11-08 6 views
2

作成した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> 

答えて

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

を私はあなたがそれを必要とするメッセージ本文の一部としてメッセージを渡していないクライアントから推測します。上記の情報があなたに役立つことを願っています。

0

あなたは、実際のメッセージを確認しましたワイヤー上のヘッダーusiフィドラー? ContentType、つまり送信しているものと受け入れたものの両方をチェックして、返品したいものを確認してください。どちらのMIMEタイプでもこの問題が発生する可能性があります。

0

私はちょっと混乱しています。これはRESTfulなサービスだと言いますが、後であなたの投稿にSOAP-SOAPという名前のものを表示し、RESTは同じではありません!

FiddlerまたはWiresharkを使用して、クライアントと作業クライアントの違いを確認し、それに応じて変更してください。あなたがhttp://social.msdn.microsoft.com/Forums/en/wcf/thread/d11a7997-d60b-4895-8f69-9ef2175087e2

を見るように同様の設定でソースコードを作業用

は基本的に彼らはcharsetContentTypeに追加しており、それらは、ヘッダーでSOAPActionを使用しないでください。

関連する問題