0

私は.net 3.5でwcfサービスを使用して、httpポストリクエストを使用してxmlを送信しています。

しかし、問題は、request.ContentType = "text/xml" を設定すると、以下の例外がスローされることです

リモート サーバーがエラーを返しました: (400) 不正な要求。

私は多くの記事を調べましたが、可能な解決策を見つけることができませんでした..

ContentType="text/xml" をサポートしていないのはなぜですか?

ここに私のコードがあります

// サービス契約

[OperationContract(Name = "PostSampleMethod")]
[WebInvoke(Method = "POST",UriTemplate = "PostSampleMethod/New")]
string PostSampleMethod(Stream data);

//.....

 public string PostSampleMethod(Stream data)
    {
        // convert Stream Data to StreamReader
        StreamReader reader = new StreamReader(data);
        // Read StreamReader data as string
        string xmlString = reader.ReadToEnd();
        XmlDocument doc = new XmlDocument();
        doc.LoadXml(xmlString);

        string returnValue = doc.InnerXml;
        // return the XMLString data
        return returnValue;
    }

//ウェブ設定...

      <?xml version="1.0"?>
<configuration>

  <system.web>
    <compilation debug="true" targetFramework="4.0" />
  </system.web>
  <system.serviceModel>

    <services>
      <service name="DemoHttpPost.HttpPost" behaviorConfiguration="ServiceBehaviour">
        <endpoint address="" binding="webHttpBinding" contract="DemoHttpPost.IHttpPost" behaviorConfiguration="web" >

        </endpoint>

      </service>

    </services>

    <behaviors>

      <serviceBehaviors>

        <behavior name="ServiceBehaviour">
          <!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment -->
          <serviceMetadata httpGetEnabled="true"/>
          <!-- To receive exception details in faults for debugging purposes, set the value below to true.  Set to false before deployment to avoid disclosing exception information -->
          <serviceDebug includeExceptionDetailInFaults="false"/>
        </behavior>
      </serviceBehaviors>
      <endpointBehaviors>

        <behavior name="web" >
          <webHttp/>
        </behavior>
      </endpointBehaviors>
    </behaviors>
    <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
  </system.serviceModel>
 <system.webServer>
    <modules runAllManagedModulesForAllRequests="true"/>
  </system.webServer>

</configuration>

//....

Service を呼び出すコードは次のとおりです。

private void button1_Click(object sender, EventArgs e)
        {

            // Create a request using a URL that can receive a post. 
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://localhost/DemoHttpPost/HttpPost.svc/PostSampleMethod/New");
            // Set the Method property of the request to POST.
            request.Method = "POST";
            // Create POST data and convert it to a byte array.
            StringBuilder messagereturn = new StringBuilder();
            messagereturn.Append("<?xml version='1.0'?><id>");
            messagereturn.Append("123456");
            messagereturn.Append("</id>");
            XmlDocument doc = new XmlDocument();
            doc.LoadXml(messagereturn.ToString());

            string postData =doc.InnerXml;
            byte[] byteArray = Encoding.UTF8.GetBytes(postData);
            // Set the ContentType property of the WebRequest.
            request.ContentType = "text/xml";
            // Set the ContentLength property of the WebRequest.
            request.ContentLength = byteArray.Length;
            // Get the request stream.
            Stream dataStream = request.GetRequestStream();
            // Write the data to the request stream.
            dataStream.Write(byteArray, 0, byteArray.Length);
            // Close the Stream object.
            dataStream.Close();
            // Get the response.

            WebResponse response = request.GetResponse();
            // Display the status.
            HttpWebResponse webres= (HttpWebResponse)response;

            StreamReader reader = new StreamReader(webres.GetResponseStream());
            // Read the content.
            string responseFromServer = reader.ReadToEnd();
            // Display the content.
            richTextBox1.Text = responseFromServer;
            // Clean up the streams.
            reader.Close();
            dataStream.Close();
            response.Close();
        }
4

1 に答える 1

0

例えば

サービス契約

    [WebInvoke(Method="POST")]
    [OperationContract]
    void Send(string data);

サービス

    public void Send(string data)
    {
        Console.WriteLine(data);
    }

クライアント。「WebService」はエンドポイント名であり、IServiceサービス コントラクトです。

    using (var factory = new WebChannelFactory<IService>("WebService"))
    {
        var channel = factory.CreateChannel();
        channel.Send(@"<system.web>5</system.web>");
    }
于 2013-06-01T22:17:01.477 に答える