【发布时间】:2017-12-16 23:15:48
【问题描述】:
var dataToSend = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(mi));
var req = HttpWebRequest.Create("http://localhost/Service1.svc/json/MethodName");
req.ContentType = "application/json";
req.ContentLength = dataToSend.Length;
req.Method = "POST";
req.GetRequestStream().Write(dataToSend, 0, dataToSend.Length);
var response = req.GetResponse();
这里的“/json”是我的端点地址,我的服务配置了多个端点。根据此处的图片,我发送的请求在服务器上收到 null。
如果我的请求格式不正确,请建议调用此服务的正确方法。
//服务接口
[ServiceContract]
public interface IService
{
[OperationContract]
[WebInvoke(Method="POST")]
Response MethodName(Request request);
}
// 服务1
public class Service1 : IService
{
public Response MethodName(Request request)
{
some logical operation....
}
}
//端点配置(Web config)
<endpoint address="json" behaviorConfiguration="jsonBehavior"
binding="webHttpBinding" bindingConfiguration="webHttpBindingJson"
name="jsonn" contract="Service1.IService" />
<endpoint address="xml" behaviorConfiguration="poxBehavior" binding="webHttpBinding"
bindingConfiguration="webHttpBindingXml" name="xmll" contract="Service1.IService" />
<endpointBehaviors>
<behavior name="jsonBehavior">
<enableWebScript />
</behavior>
<behavior name="poxBehavior">
<enableWebScript />
</behavior>
</endpointBehaviors>
<webHttpBinding>
<binding name="webHttpBindingJson">
<security mode="None" />
</binding>
<binding name="webHttpBindingXml">
<security mode="None" />
</binding>
</webHttpBinding>
// 请求类
[DataContract]
public class Request
{
string userMobile;
string otp;
[DataMember]
public string UserMobile
{
get { return userMobile; }
set { userMobile = value; }
}
[DataMember]
public string OTP
{
get { return otp; }
set { otp = value; }
}
}
【问题讨论】:
-
您的服务是否真的将 json 作为输入?你从未展示过定义。
-
是的,它通过端点配置获取 json 和 xml。
-
接口配置为 [WebInvoke(Method="POST")] Response MethodName(Request request);
-
请用所有信息更新您的代码。
-
请查看以上更新详情。
标签: wcf