我正在尝试从窗口表单中使用WCF服务。我试图调用的方法接受两个参数:
public String Redirect(String code, String[] data)
{
//Some code here.
}
现在,当我尝试连接到该函数时,没有发送数据时,Web服务停止。我需要知道如何使用正确的格式发送数据,以便该功能接受DE连接。
pt:该功能仅接受JSON数据。
我用来连接到Web服务的代码:
String url = GetUrl();
WebRequest request = WebRequest.Create(url);
request.ContentType = "application/json";
request.Method = "POST";
System.Text.UTF8Encoding encoding = new System.Text.UTF8Encoding();
request.GetResponse();
谢谢
我假设您将WebInvoke
与POST
方法一起使用。解决方案非常简单:您忘了将消息添加到身体:
String url = GetUrl();
WebRequest request = WebRequest.Create(url);
request.ContentType = "application/json";
request.Method = "POST";
byte[] bytes = System.Text.Encoding.UTF8.GetBytes("Your JSON data");
request.ContentLength = bytes.Length;
var requestStream = request.GetRequestStream();
requestStream.Write(bytes, 0, bytes.Length);
You JSON data
将是这样的:
{
"code": "10",
"data": [
"hello",
"data"
]
}