这是使用System.Net的正确方法吗?

时间:2018-06-14 00:44:24

标签: c# system.net

我的System.Net的帖子方法是

public string Post(string url, string postdata, string referrer = "https://accounts.epicgames.com/login?productName=epic-games&redirectUrl=https%3A%2F%2Fwww.epicgames.com%2Fsite%2Fen-US%2Fhome&noHostRedirect=true", string accept = "*/*")
{
    var req = (HttpWebRequest)WebRequest.Create(url);
    byte[] postData = Encoding.UTF8.GetBytes(Uri.EscapeUriString(postdata));
    req.Method = "POST";
    req.Accept = accept;
    req.UserAgent = useragent;
    req.Timeout = 10000;
    req.ReadWriteTimeout = 15000;
    if (Useproxy) req.Proxy = HProxy;
    req.Headers.Add("Origin", "https://accounts.epicgames.com");
    req.Headers.Add("Accept-Language", "en-GB,en-US;q=0.9,en;q=0.8");
    req.Headers.Add("Accept-Encoding", "gzip, deflate, br");
    req.Referer = referrer;
    req.ContentType = "application/x-www-form-urlencoded";
    req.AutomaticDecompression = DecompressionMethods.Deflate | DecompressionMethods.GZip;
    req.KeepAlive = true;
    req.CookieContainer = cookies;
    using (var s = req.GetRequestStream())
    {
        s.Write(postData, 0, postData.Length);
    }
    var res = (HttpWebResponse)req.GetResponse();

    using (var sr = new StreamReader(res.GetResponseStream()))
    {
        return sr.ReadToEnd();
    }
}

在我的主要方法中,我有这个

public void DoWork()
{
    string source = string.Empty;

    try
    {
        string account = "account here";
        string[] combo = account.Split(':', ';', '|');
        var req = new HttpReq();
        var x = req.Get("https://accounts.epicgames.com/login/doLogin?redirectUrl=https%3A%2F%2Fwww.epicgames.com%2Fsite%2Fen-US%2Fhome&productName=epic-games");
        string xsrf = Regex.Match(x, "value=\"([a-z0-9-]{36})\"").Groups[1].Value;
        string pData = $"X-XSRF-TOKEN={xsrf}&X-XSRF-URI=%2Flogin%2FdoLogin&fromForm=yes&authType=&linkExtAuth=&redirectUrl=https%3A%2F%2Fwww.epicgames.com%2Fsite%2Fen-US%2Fhome&epic_username={WebUtility.UrlEncode(combo[0])}&password={combo[1]}&productName=epic-games";
        var y = req.Post("https://accounts.epicgames.com/login/doLogin", pData);
        MessageBox.Show(y);
        //do normal login and see get url and post url again. It wa still same issuemeanslets see

    }
    catch (WebException wex)
    {
        MessageBox.Show(wex.Message);
        //if (wex.Response != null)
        //{
        //    using (var errorResponse = (HttpWebResponse)wex.Response)
        //    {
        //        using (var reader = new StreamReader(errorResponse.GetResponseStream()))
        //        {
        //            string error = reader.ReadToEnd();
        //            //TODO: use JSON.net to parse this string and look at the error message
        //            Clipboard.SetText(error);
        //        }
        //    }
        //}
    }
} 

我已经在本网站上关注了一些问题,而且在语法上似乎是正确的。但是,当发布到网站时,我收到400 Bad Request 当我读到错误的内容时,它说明了这一点:

  

抱歉,您似乎已尝试两次提交表单。请   只需点击一次。

我确定一切正常,因为我在xNet中使用相同的系统。我正在切换,因为xNet无法满足我的需求。

0 个答案:

没有答案