【问题标题】:How to send base64 string in a url?如何在 url 中发送 base64 字符串?
【发布时间】:2019-06-03 12:52:25
【问题描述】:

我有一个离子应用程序,我想通过它向我的 WCF 服务发送一个 base64 字符串。由于 base64 字符串很大,我收到以下错误-“HTTP 错误 414。请求 URL 太长。”我已在 IIS 上托管此服务。有什么办法可以让这个工作吗?我尝试在网上搜索这个问题,但没有任何办法。

这是我的方法 -

 [OperationContract]
        [WebInvoke(Method = "POST",UriTemplate = "/ReportaBug?base64String={base64String}", BodyStyle = WebMessageBodyStyle.Bare, RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json )]
        void ReportaBug(string base64String);

这是我的 web.config 文件 -

 <system.web>
    <compilation debug="true" targetFramework="4.6.1" />
    <httpRuntime  targetFramework="4.6.2" maxUrlLength="32768" maxQueryStringLength="32768"/>
  </system.web>
  <system.serviceModel>
    <behaviors>
      <serviceBehaviors>
        <behavior name="ServiceBehavior">
          <!-- To avoid disclosing metadata information, set the values below to false before deployment -->
          <serviceMetadata httpGetEnabled="true" httpsGetEnabled="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>
    <protocolMapping>
      <add binding="basicHttpsBinding" scheme="https" />
    </protocolMapping>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" minFreeMemoryPercentageToActivateService="0"/>
    <services>
      <service name="FusionRestService.FusionRestService" behaviorConfiguration="ServiceBehavior">
        <endpoint binding="webHttpBinding" contract="FusionRestService.IFusionRestService" behaviorConfiguration="web"></endpoint>
        <endpoint
            address="mex"
            binding="mexHttpBinding"
            contract="IMetadataExchange"/>
      </service>
    </services>
  </system.serviceModel>
  <system.webServer>
    <security>
      <requestFiltering>
         <requestLimits maxQueryString="32768"/>
      </requestFiltering>
    </security>
    <modules runAllManagedModulesForAllRequests="true"/>
    <directoryBrowse enabled="true"/>
  </system.webServer>

【问题讨论】:

  • 在正文中传输您的数据,而不是 URL。 URL 有一个最大长度 - 句点。我们必须处理这个问题。显而易见的事情是在消息体(它所属的地方)中传输数据。
  • 如何发送消息体中的数据?您能否展示一些示例代码或参考资料?
  • 已经有一段时间了,不过如果我没记错的话,你只需要把uriTemplate中的参数去掉就可以了。不确定您是否需要使用 Stream 作为方法参数的类型并读取它,或者它是否“正常工作”......也许您需要使用除 Bare 之外的其他 BodyStyle(实际上非​​常肯定)。
  • [OperationContract] [WebInvoke(Method = "POST",UriTemplate = "/ReportaBug, BodyStyle = WebMessageBodyStyle.Wrapped, RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json )] void ReportaBug(string base64String ); 这就是你在说的吗?
  • 我想,是的。我已经有一段时间没有在 WCF 工作了,所以我也必须进行实验。但对我来说,它看起来不错。

标签: c# wcf url base64


【解决方案1】:

这是一个 IIS 错误,告诉您给定 URL 的长度超过了限制。 IIS 的最大 URL 长度由 HttpRuntimeSection.MaxUrlLength 属性定义。其值为 260 个字符。这可能会导致超过配置的maxUrlLength URL 出现问题,以下是解决此问题的方法: https://www.saotn.org/the-length-url-request-exceeds-configured-maxurllength-value/

更新 我在我的本地主机中测试了你的代码,它工作得很好。我刚刚在我的web.config中添加了这个参数

<httpRuntime targetFramework="4.6.2" maxUrlLength="32768" maxQueryStringLength="32768" />

和

<security>
  <requestFiltering>
    <requestLimits maxQueryString="32768"/>
  </requestFiltering>
</security>

【讨论】:

  • 我更改了 maxURLlength,但我仍然收到相同的错误 - HTTP 错误 414。请求 URL 太长。
  • 您的网络服务器在哪里?您是通过 Internet 还是公共网络访问它?有时路由器会阻止异常流量。
  • 它在共享网络中。我在 IIS 上托管了该服务。
  • 我用 300 个字符的 url 测试了你的代码,它工作得很好。
  • 是的。 300个字符工作正常。我发送的 base64 字符串是 25824 个字符。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-10-15
  • 1970-01-01
  • 2013-12-21
  • 1970-01-01
  • 1970-01-01
  • 2019-04-11
相关资源
最近更新 更多