我使用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格式的数据。 有没有我想念的任何设置?请指教。
您可能正在做出不好的要求:) - 您真的应该展示您如何致电服务。有关发布代码的指导,请参阅[MCVE]。 –
你确定'strJSONToSend'具有正确的JSON格式吗? – Aria
我想是的......任何建议? –