1. 程式人生 > >C# net request payload形式傳送post請求

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字串傳送。