zk120860728 2017-03-20 02:40 采纳率: 0%
浏览 3414

WCF 如何通过POST 接收和返回参数为空

WCF作为服务端,目前是寄宿在IIS,客户端是普通的winform,就想简单的做一个“发送请求”、“返回结果”的功能。 但是服务端显示参数一直是null。
用的json格式。

WCF接口代码:
[OperationContract]
[WebInvoke(Method = "POST", UriTemplate = "GetConnection", ResponseFormat = WebMessageFormat.Json,
RequestFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Wrapped)]
ResultInfo GetConnection(KeyValuePair data);

接口实现:
 public ResultInfo GetConnection(KeyValuePair<string, object> data)

{

//这里监视到的data就是null
return new ResultInfo(){IsSuccessed = true,}; //返回值是自己定义的一个类
}

 客户端调用:    

 public static string SendHttpRequest(string requestURI, string requestMethod, string json,int timeOut = 300000)
    {
        //json格式请求数据
        string requestData = json;
        //拼接URL
        string serviceUrl = string.Format("{0}/{1}", requestURI, requestMethod);
        HttpWebRequest myRequest = null;

        myRequest = (HttpWebRequest)WebRequest.Create(serviceUrl);


        //utf-8编码
        byte[] buf = Encoding.GetEncoding("UTF-8").GetBytes(requestData);
        //post请求
        myRequest.Method = "POST";
        myRequest.ContentLength = buf.Length;

        myRequest.ContentType = "application/json";
        myRequest.MaximumAutomaticRedirections = 1;
        myRequest.AllowAutoRedirect = true;
        myRequest.Timeout = timeOut;
        Stream newStream = myRequest.GetRequestStream();
        newStream.Write(buf, 0, buf.Length);
        newStream.Close();


        HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse();
        StreamReader reader = new StreamReader(myResponse.GetResponseStream(), Encoding.UTF8);
        string ReqResult = reader.ReadToEnd();
        reader.Close();
        myResponse.Close();
        return ReqResult;
    }

传入的json在客户端看是正常的: Key是Guid,Value是一个自己定义的类型
{"Key":"6aafd4a3-6d9d-4bab-adb6-f5fe718437c1","Value":{"ComputerName":"Desk-Top","IpAddress":"192.169.6.126","Mac":null}}

也查了不少资料,可能悟性有限,一直没能解决,头大。
试过加上数据契约 比如
[DataContract]
public class PcInfo
{
[DataMember]
public string ComputerName{set;get;}.......
}
服务端接受的一直是null。

求大牛不吝赐教。。小弟感激不尽

  • 写回答

2条回答 默认 最新

  • qq_494856288 2022-06-20 11:08
    关注

    大哥,这个问题搞定了吗

    评论

报告相同问题?

悬赏问题

  • ¥20 5037端口被adb自己占了
  • ¥15 Error in check.length("fill") : 'gpar'成分'fill'的长度不能为零
  • ¥15 python:excel数据写入多个对应word文档
  • ¥60 全一数分解素因子和素数循环节位数
  • ¥15 ffmpeg如何安装到虚拟环境
  • ¥188 寻找能做王者评分提取的
  • ¥15 matlab用simulink求解一个二阶微分方程,要求截图
  • ¥30 乘子法解约束最优化问题的matlab代码文件,最好有matlab代码文件
  • ¥15 写论文,需要数据支撑
  • ¥15 identifier of an instance of 类 was altered from xx to xx错误