c# - 尝试使用 WebRequest 调用 WCF 服务

标签 c# wcf rest post webrequest

我有一个需要由第 3 方应用程序调用的 WCF 服务,并发布一些原始 XML。

我正在尝试通过构建一个简单的 WebRequest 并向服务发出请求来测试我的服务。

这是我的服务代码:

界面:

    [ServiceContract(Namespace = "http://test.mydomain.com")]
public interface ITest
{
    [WebInvoke(UriTemplate = "", BodyStyle = WebMessageBodyStyle.Bare, Method="POST")]
    [OperationContract]
    Stream SaveXML(Stream input);
}

服务:
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
[ServiceBehavior(Namespace = "http://test.mydomain.com")]
public class Test : ITest
{
    public Stream SaveXML(Stream input)
    {
        StreamReader streamReader = new StreamReader(input);
        string rawString = streamReader.ReadToEnd();
        streamReader.Dispose();

        // here need to save the input stream to xml format file
        Encoding encoding = Encoding.GetEncoding("ISO-8859-1");
        WebOperationContext.Current.OutgoingResponse.ContentType = "text/xml";
        byte[] returnBytes = encoding.GetBytes(rawString);
        return new MemoryStream(returnBytes);
    }
}

配置:
    <services>
  <service behaviorConfiguration="Blah.TestBehavior" name="Blah.Test">
    <endpoint address="http://localhost:51494/Blah/Test.svc" binding="basicHttpBinding" contract="Blah.ITest">
      <identity>
        <dns value="localhost" />
      </identity>
    </endpoint>
    <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
  </service>
</services>

错误的客户端代码:
            string postData = "<Message version=\"1.5\" xmlns=\"http://test.mydomain.com\" ><books>Blah</books></Message>";
        WebRequest request = WebRequest.Create("http://localhost:51494/Blah/Test.svc/SaveXML");
        request.Method = "POST";

        byte[] byteArray = Encoding.UTF8.GetBytes(postData);
        request.ContentType = "application/x-www-form-urlencoded";
        //request.ContentType = "text/xml; charset=utf-8";
        //request.ContentType = "text/xml;";
        //request.ContentType = "application/xml;";
        request.ContentLength = byteArray.Length;

        Stream dataStream = request.GetRequestStream();
        dataStream.Write(byteArray, 0, byteArray.Length);
        dataStream.Close();

        // Get the response.
        WebResponse response = request.GetResponse();

在最后一行,我收到 400(错误请求)或 415(不支持的媒体类型)错误,具体取决于我指定的 ContentType。

此外,如果我在客户端应用程序中添加服务引用,并使用 API 调用服务,它工作正常。任何见解都将不胜感激,因为我是 WCF 的新手并且完全被难住了。

最佳答案

我认为你在这里混淆了两种不同的东西:

  • WebRequestPOST[WebInvoke]属性表明您正在尝试执行类似 REST
  • 的操作
  • 但是,您的服务配置有 basicHttpBinding - 一个 SOAP 协议(protocol),它不会与 WebRequest 一起使用

  • 所以——下定决心!

    你想使用 SOAP 吗?然后您可以按原样使用 basicHttpBinding,但是您不能像使用 POST 从 WebRequest 访问 SOAP 服务 - 您需要使用由 Visual Studio 或 svcutil.exe 生成的 SOAP 客户端在命令行上。

    你想使用 WebRequest 和一个简单的 POST 请求吗?然后您需要创建一个基于 REST 的 WCF 服务 - 使用 webHttpBindingWebServiceHost (相对于普通的 ServiceHost)。

    对于基于 SOAP 的 WCF 服务,请查看 WCF Developer Center on MSDN .

    对于基于 REST 的 WCF 服务(您可以在浏览器中导航到,并且可以从 WebRequest 调用),请查看 WCF REST Developer Center on MSDN看看优秀的screencast series by Pluralsight关于 WCF 中基于 REST 的开发 - 最值得注意的是:
  • Building RESTful Services with WCF
  • Building RESTful Services with WCF Part 2
  • 关于c# - 尝试使用 WebRequest 调用 WCF 服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2382567/

    相关文章:

    c# - Azure blob 作为图像 Windows 窗体

    c# - TaskCreationOptions.LongRunning 选项和 ThreadPool

    Spring Boot Rest Controller 如何返回不同的 HTTP 状态码?

    javascript - XMLHTTPRequest 发送数据

    java - Http 获取 Web 服务上的请求不起作用

    C# 日期时间 : Conversion for different time zones

    c# - 导致绑定(bind)不更新的验证错误

    wcf - 数据应该存储在 MVVM 中的什么位置?

    c# - WCF 服务 - 不要等待外部 Web 服务调用

    wcf - 如何保护 Android 应用程序将使用的 WCF 服务?