【发布时间】: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 工作了,所以我也必须进行实验。但对我来说,它看起来不错。