2016-09-12 5 views
0

Webサービスからデータを受信しようとすると、このエラーメッセージが表示されます。ここでコンテンツタイプapplication/json。レスポンスメッセージのcharset = utf-8がバインディングのコンテンツタイプと一致しません(text/xml; charset = utf-8)

は私のコードです:

ChannelFactory<IInterface> factory = new ChannelFactory<IInterface>(new BasicHttpBinding(), new EndpointAddress("http://example.net/MyService.svc/Test")); 
var client = factory.CreateChannel(); 

MyObj x = client.Test(); 

私はエラーを取得しますが、私はエラーメッセージで応答(JSON文字列)を見ることができます。私はバインドをWebHttpBindingに変更し、エンドポイントの動作をWebHttpBehaviorとして追加しようとしましたが、これは単にnullオブジェクトを返します。

答えて

0

私はそれを解決しました。当初はWebHttpBindingを使用していましたが、エンドポイントの動作では少し修正する必要がありました。作業コードを聞きます:

ChannelFactory<IInterface> factory = new ChannelFactory<IInterface>(new WebHttpBinding(), new EndpointAddress("http://example.net/MyService.svc/Test")); 

WebHttpBehavior behavior = new WebHttpBehavior() 
{ 
    DefaultOutgoingResponseFormat = WebMessageFormat.Json, 
    DefaultBodyStyle = WebMessageBodyStyle.Wrapped, 
    HelpEnabled = true, 
    DefaultOutgoingRequestFormat = WebMessageFormat.Json 
}; 

factory.Endpoint.Behaviors.Add(behavior); 

var client = factory.CreateChannel(); 

MyObj x = client.Test(); 
関連する問題