1

我正在露天开发基于 REST 的网络服务以进行数据传输,并且想知道我可以通过 REST 协议发送/获取的最大数据量是多少?

任何参考都会非常有帮助。

问候。

4

1 回答 1

2

最大数据量为 2147000000。这就是为什么如果您的数据足够大,建议将其流式传输到您的 REST 服务。这是一个例子。

发件人/上传者应用程序或客户端

        var sb = new StringBuilder();
        sb.Append("Just test data. You can send large one also");
        var postData = sb.ToString();
        var url = "REST Post method example http://localhost:2520/DataServices/TestPost";
        var memoryStream = new MemoryStream();
        var dataContractSerializer = new DataContractSerializer(typeof(string));
        dataContractSerializer.WriteObject(memoryStream, postData);
        var xmlData = Encoding.UTF8.GetString(memoryStream.ToArray(), 0, (int)memoryStream.Length);
        var client = new WebClient();
        client.UploadStringAsync(new Uri(url), "POST", "");
        client.Headers["Content-Type"] = "text/xml";
        client.UploadStringCompleted += (s, ea) =>
        {
            if (ea.Error != null) Console.WriteLine("An error has occured while processing your request");
            var doc = XDocument.Parse(ea.Result);
            if (doc.Root != null) Console.WriteLine(doc.Root.Value);
            if (doc.Root != null && doc.Root.Value.Contains("1"))
            {
                string test = "test";
            }
        };

REST 服务方法

[WebInvoke(UriTemplate = "TestPost", Method = "POST", BodyStyle = WebMessageBodyStyle.WrappedRequest, RequestFormat = WebMessageFormat.Xml)]
    public string Publish(string market,string sportId, Stream streamdata)
    {
        var reader = new StreamReader(streamdata);
        var res = reader.ReadToEnd();
        reader.Close();
        reader.Dispose();

    }

不要忘记将以下配置设置放在您的 REST 服务配置文件中,如果您没有此设置,则会引发错误

<system.web>
    <compilation debug="true" targetFramework="4.0" />
    <httpRuntime maxRequestLength="2147000000" maxQueryStringLength="2097151" maxUrlLength="2097151"/>
  </system.web>
  <system.webServer>
    ........
  </system.webServer>
  <system.serviceModel>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" />
    <standardEndpoints>
      <webHttpEndpoint>
        <standardEndpoint name="" helpEnabled="true" automaticFormatSelectionEnabled="true" maxReceivedMessageSize="2147000000" maxBufferPoolSize="2147000000" maxBufferSize="2147000000"/>
      </webHttpEndpoint>
    </standardEndpoints>
  </system.serviceModel>
于 2013-01-25T07:20:15.340 回答