2016-04-18 22 views
0

私はPOSTMANまたはFiddlerを使用してテストするときにC#WCFプロジェクトを作成しました。私は「400 Bad Request」というエラーを受け取りました。また、テストするC#のWindowsプロジェクトを作成しましたが、これも同じ結果です。ここに私のWCFプロジェクトがあります。400リクエストが間違っています - REST JSON c#

[ServiceContract] 
public interface IService1 
{ 

    [OperationContract] 
    [WebInvoke(Method = "POST", 
     ResponseFormat = WebMessageFormat.Json, 
     RequestFormat = WebMessageFormat.Json, 
     BodyStyle = WebMessageBodyStyle.Wrapped, 
     UriTemplate = "api/createpr.json")] 
    PRApplication AddPR(RequestData rData); 

} 

私はテスト時にこのコードを使用しています。

  try 
     { 
      var httpWebRequest = (HttpWebRequest)WebRequest.Create("http://localhost/WEB_Service_B1/Service1.svc/api/createpr.json"); 
      httpWebRequest.ContentType = "application/json; charset=utf-8"; 
      httpWebRequest.Method = "POST"; 

      using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream())) 
      { 
       string json = "{\"Test\"}"; 

       streamWriter.Write(json); 
       streamWriter.Flush(); 
       streamWriter.Close(); 
      } 

      var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse(); 
      using (var streamReader = new StreamReader(httpResponse.GetResponseStream())) 
      { 
       var result = streamReader.ReadToEnd(); 
      } 
     } 
     catch (Exception err) 
     { 
      MessageBox.Show(err.Message); 
     } 

私のWeb.configにも問題はありますか?

<?xml version="1.0"?> 
 
<configuration> 
 
    <system.web> 
 
     <compilation debug="true" targetFramework="4.0" /> 
 
     <authentication mode="Windows" /> 
 
     <pages controlRenderingCompatibilityVersion="3.5" clientIDMode="AutoID"/> 
 
    </system.web> 
 
    <system.webServer> 
 
    <directoryBrowse enabled="true"/> 
 
    </system.webServer> 
 
    <system.serviceModel> 
 
    <behaviors> 
 
     <serviceBehaviors> 
 
     <behavior name="WS_B1.Service1Behavior"> 
 
      <serviceMetadata httpGetEnabled="true" /> 
 
      <serviceDebug includeExceptionDetailInFaults="false" /> 
 
     </behavior> 
 
     </serviceBehaviors> 
 
     <endpointBehaviors> 
 
     <behavior name="web"> 
 
      <webHttp/> 
 
     </behavior> 
 
     </endpointBehaviors> 
 
    </behaviors> 
 
    <services> 
 
     <service name="WS_B1.Service1" behaviorConfiguration="WS_B1.Service1Behavior"> 
 
     <endpoint address="" binding="webHttpBinding" contract="WS_B1.IService1" behaviorConfiguration="web"> 
 
      <identity> 
 
      <dns value="localhost"/> 
 
      </identity> 
 
     </endpoint> 
 
     <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/> 
 
     </service> 
 
    </services> 
 
    </system.serviceModel> 
 
</configuration>

+0

ポートなしでlocalhostを使用していますか?これは、http:// localhost:58108/WEB_Service_B1/Service1.svc/api/createpr.jsonのようには見えません。あなたの関連するポートと? –

+0

はい、http:// localhost/WEB_Service_B1です。それはポート80を使用し、そのためのエイリアスはWEB_Service_B1です。 –

答えて

2
BodyStyle = WebMessageBodyStyle.Wrapped 

これは、要求が完全なJSONオブジェクトにする必要があることを意味します。以下のリクエストボディで試してみてください:

PropertyNameは、文字列型の RequestDataクラスのパブリックプロパティです
string json = "{\"rData\": {\"PropertyName\": \"Test\"}}"; 

+0

本当に感謝しています。しかし、私はそれをテストする:**基底の接続が閉じられた:受信時に予期しないエラーが発生しました。トランスポート接続からデータを読み取ることができません:既存の接続はリモートホストによって強制的に閉じられました** –

関連する問題