C# c WCF JSON POST-(400)错误请求
我什么都试过了 当我试图从客户端调用WCF服务时,我总是遇到一个400错误的请求错误 我想通过JSON发送给服务器一个“用户”类 我的代码: 计算 接口ICalc Web.config 编辑:C# c WCF JSON POST-(400)错误请求,c#,json,wcf,C#,Json,Wcf,我什么都试过了 当我试图从客户端调用WCF服务时,我总是遇到一个400错误的请求错误 我想通过JSON发送给服务器一个“用户”类 我的代码: 计算 接口ICalc Web.config 编辑: 什么是basicURL,为什么var str=basicURL+UplaodData特别是关于..lao..的部分,代理是如何初始化的,以及从接口中引用postaser方法的位置?@dmay我编辑了消息。var str=basicURL+UplaodData用于检查。@user3868442将引导您完成W
什么是basicURL,为什么var str=basicURL+UplaodData特别是关于..lao..的部分,代理是如何初始化的,以及从接口中引用postaser方法的位置?@dmay我编辑了消息。var str=basicURL+UplaodData用于检查。@user3868442将引导您完成WCF,WCF依赖于wsdl,可以像您尝试的那样使用,但可以说不应该使用。不过,您应该看看WebAPI。如果您正试图实现这一点,那么它与使用简单的自发现HTTP REST更为一致,而无需签订协议?
[OperationContract]
[WebInvoke(Method="POST",
RequestFormat=WebMessageFormat.Json,
ResponseFormat=WebMessageFormat.Json,
UriTemplate = "/UplaodData")]
void PostUser(Users user);
public void PostUser(Users user)
{
int userId = user.id;
.....
}
<?xml version="1.0"?>
<!--
For more information on how to configure your ASP.NET application, please visit
http://go.microsoft.com/fwlink/?LinkId=169433
-->
<configuration>
<connectionStrings>
<add name="mydbConnectionString" connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\mydb.mdf;Integrated Security=True;User Instance=True" providerName="System.Data.SqlClient"/>
</connectionStrings>
<system.web>
<compilation debug="true" targetFramework="4.0">
<assemblies>
<add assembly="System.Data.Linq, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>
<add assembly="mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>
</assemblies>
</compilation>
</system.web>
<system.webServer>
<modules>
<remove name="WebDAVModule"/>
</modules>
<handlers>
<remove name="WebDAV"/>
</handlers>
</system.webServer>
<system.serviceModel>
<services>
<service name="Calc">
<endpoint address="" behaviorConfiguration="DualHttp"
binding="webHttpBinding" contract="ICalc"/>
</service>
</services>
<behaviors>
<endpointBehaviors>
<!-- we added this -->
<behavior name="DualHttp">
<webHttp/>
</behavior>
</endpointBehaviors>
<serviceBehaviors>
<behavior name="">
<serviceMetadata httpGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="false"/>
</behavior>
</serviceBehaviors>
</behaviors>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true"/>
</system.serviceModel>
</configuration>
private void btnAdd_Click(object sender, EventArgs e)
{
if(ValidateChildren(ValidationConstraints.Enabled))
{
var user = new Users();
user.id = 1;
user.birthday = new DateTime(2009,1,1);
WebClient Proxy = new WebClient();
Proxy.Headers["Content-type"] = "application/json";
MemoryStream ms = new MemoryStream();
var serializerToUpload = new DataContractJsonSerializer(typeof(Users));
serializerToUpload.WriteObject(ms, user);
proxy.UploadData(basicURL + "UplaodData", "POST", ms.ToArray());
}
}
private string basicURL = string.Format("http://localhost:20524/finalProjectServer/Service.svc/");
private WebClient proxy = new WebClient();