2016-10-23 146 views
0

我使用WCF POST方法,一旦我添加参数POST到服务其返回错误400错误的请求,如果我留下参数空它可以访问我的服务。WCF POST方法获取错误400错误的请求

这是我的接口:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Runtime.Serialization; 
using System.ServiceModel; 
using System.Text; 
using System.ServiceModel.Web; 
using System.IO; 
namespace SampleArticle 
{ 
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "IRestService" in both code and config file together. 
    [ServiceContract(Namespace="IRestService/JSONData")] 
    public interface IRestService 
    { 

     [OperationContract] 
     [WebInvoke(Method = "POST", ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Wrapped,UriTemplate = "authorize")] 
     Stream authorize(Stream streamdata); 

    } 
} 

这是我的web.config使用Msxml2.ServerXMLHTTP为POST

Dim objXmlHttpMain , URL 

URL="http://localhost/SampleArticle/RestService.svc/authorize" 

strJSONToSend = "{""acctId"": ""Test10001""," 
strJSONToSend = strJSONToSend & """language"": 200," 
strJSONToSend = strJSONToSend & """Code"": ""Test""," 
strJSONToSend = strJSONToSend & """token"": ""abcd123412341234""," 
strJSONToSend = strJSONToSend & """serialNo"": ""20161020160455982841""}" 

// if i set the parameter to empty i can access to the service 
'strJSONToSend = "" 
Set objXmlHttpMain = CreateObject("Msxml2.ServerXMLHTTP") 
'on error resume next 
objXmlHttpMain.open "POST",URL, False 

// if i change the "application/json" to "application/x-www-form-urlencoded" it works 
'objXmlHttpMain.setRequestHeader "Content-Type", "application/json" 
objXmlHttpMain.setRequestHeader "Content-Type", "application/x-www-form-urlencoded" 

objXmlHttpMain.send strJSONToSend 


//check for output 
S = objXmlHttpMain.responseText 
response.write S 

set objJSONDoc = nothing 
set objResult = nothing 

Server日志味精

<?xml version="1.0" encoding="UTF-8"?> 
<configuration> 

    <appSettings> 

    <add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" /> 
    </appSettings> 

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

    </system.web> 

    <system.serviceModel> 


    <services> 
     <service name="SampleArticle.RestService" behaviorConfiguration="serviceBehavior"> 
     <endpoint address="" binding="webHttpBinding" contract="SampleArticle.IRestService" behaviorConfiguration="web"></endpoint>   
     </service> 
    </services> 

    <behaviors> 
     <serviceBehaviors> 

     <behavior name="serviceBehavior"> 
      <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/> 
      <serviceDebug includeExceptionDetailInFaults="false" /> 
     </behavior> 

     </serviceBehaviors> 

     <endpointBehaviors> 
     <behavior name="web"> 
      <webHttp /> 
     </behavior> 
     </endpointBehaviors> 

    </behaviors> 

    <protocolMapping> 
     <add binding="basicHttpsBinding" scheme="https" /> 
    </protocolMapping> 


    <serviceHostingEnvironment multipleSiteBindingsEnabled="true" /> 
    </system.serviceModel> 


<system.webServer> 
    <modules runAllManagedModulesForAllRequests="true" /> 
    <directoryBrowse enabled="true" /> 

    </system.webServer> 

</configuration> 

用于操作'授权'(与'IRestService/JSONData'名称空间签订'IRestService')的传入消息包含无法识别的http正文格式值'Json'。预期的身体格式值为'原始'。这可能是因为WebContentTypeMapper尚未在绑定上配置。有关更多详细信息,请参阅WebContentTypeMapper的文档。

如果我改变的内容类型 “应用/ JSON的” 到 “应用程序/ x-WWW的形式,进行了urlencoded” 它的工作原理,但我需要JSON格式的数据。 有没有我想念的任何设置?请指教。

+0

您可能正在做出不好的要求:) - 您真的应该展示您如何致电服务。有关发布代码的指导,请参阅[MCVE]。 –

+0

你确定'strJSONToSend'具有正确的JSON格式吗? – Aria

+0

我想是的......任何建议? –

回答

0

我已经解决了我的问题,我添加了自定义WebContentTypeMapper。 这里是我的示例代码:

创建新类,允许接收数据的RAW类型

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.ServiceModel; 
using System.ServiceModel.Channels; 
namespace SampleArticle 
{ 
    public class MyWebContentTypeMapper : WebContentTypeMapper 
    { 
     public override WebContentFormat GetMessageFormatForContentType(string contentType) 
     { 

      return WebContentFormat.Raw; 
     } 
    } 
} 

Web.Config中添加自定义绑定到服务

<system.serviceModel> 
    <services> 
     <service name="SampleArticle.RestService" behaviorConfiguration="serviceBehavior"> 
      <endpoint address="" binding="customBinding" bindingConfiguration="RawReceiveCapable" contract="SampleArticle.IRestService" behaviorConfiguration="web"></endpoint> 
     </service> 
    </services> 
    <behaviors> 
     <serviceBehaviors> 
      <behavior name="serviceBehavior"> 
       <serviceMetadata httpGetEnabled="true" httpsGetEnabled="false"/> 
       <serviceDebug includeExceptionDetailInFaults="false"/> 
      </behavior> 
      <behavior name=""> 
       <serviceMetadata httpGetEnabled="true" httpsGetEnabled="false"/> 
       <serviceDebug includeExceptionDetailInFaults="false"/> 
      </behavior> 
     </serviceBehaviors> 
     <endpointBehaviors> 
      <behavior name="web"> 
       <webHttp/> 
      </behavior> 
     </endpointBehaviors> 
    </behaviors> 


    <bindings> 
     <customBinding> 
      <binding name="RawReceiveCapable"> 
       <webMessageEncoding webContentTypeMapperType="SampleArticle.MyWebContentTypeMapper, SampleArticle, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"/> 
       <httpTransport manualAddressing="true" maxReceivedMessageSize="524288000" transferMode="Streamed"/> 
      </binding> 
     </customBinding> 
    </bindings> 
    <serviceHostingEnvironment multipleSiteBindingsEnabled="true"/> 
</system.serviceModel> 

这解决了我的问题,也希望它的帮助。感谢您的帮助

+0

干得好,正如你所看到的,我在我的一个评论中说过你应该强制返回Raw,所以自定义'WebContentTypeMapper'必须被实现。 – Aria

+0

@Aria非常感谢您的帮助! –

+0

你最wlc! – Aria