C# net request payload形式发送post请求

因为开发微信群发电脑版需要模拟微信POST请求,微信发送消息使用request payload发送,实际发送的是json字符串。我们只需要生成的json字符串和请求的一致,header头完全模拟即可。

下面是正常请求的谷歌请求信息:

无标题.png

下面是我的c#代码

public static bool SendTextMessage(string Content,string ToUserName)
{
    SendMessageUrl +=passticket;
    File.WriteAllText(@"D:\URL.txt", SendMessageUrl);
    HttpHelper http = new HttpHelper();
    Deviceid = DeviceID();
    string LocalId = LocalID();
    var data = new { BaseRequest = new { Uin = wxuin, Sid = wxsid, Skey = skey, DeviceID = Deviceid }, Msg = new { ClientMsgId = LocalId, Content = @Content, LocalID = LocalId, FromUserName = wxusername, ToUserName = ToUserName, Type = 1 } ,Scene =0};
    string json = JsonConvert.SerializeObject(data);
    HttpItem item = new HttpItem()
    {
        URL = SendMessageUrl,
        Method = "post",
        Accept = "application/json, text/plain, */*",
        Postdata = json,
        Cookie = HttpCookie,
        ContentType = "application/json;charset=UTF-8"
    };
    item.Header.Add("Accept-Encoding", " gzip");
    HttpResult result = http.GetHtml(item);
    string html = result.Html;
    return true;
}

我先创建一个与json格式结构一致的对象,然后序列化为json,php或者java道理一样。请求的header必须保持一致即可。

说白了就是将请求的信息通过.net实例为1个对象,再将对象转为json字符串发送。

 

posted @ 2018-07-03 17:39  .NET快速开发框架  阅读(5034)  评论(0编辑  收藏  举报