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
    关注

    大哥,这个问题搞定了吗

    评论

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!