通过C#将登录表单提交到外部网站时出错

时间:2019-04-03 09:56:38

标签: c#

我目前正在尝试登录网站,并在登录网站后提交表单。

我尝试浏览所有输入字段并将其添加到代码中,但还是没有运气。

我要登录的网站是http://sythe.org/login/

这是我目前拥有的:

$IE = preg_match('/Trident\//i',$_SERVER['HTTP_USER_AGENT']);

谢谢大家。

2 个答案:

答案 0 :(得分:0)

您可以尝试使用Fiddler的C#插件生成的以下代码:

private void MakeRequests()
{
    HttpWebResponse response;

    if (Request_www_sythe_org(out response))
    {
        response.Close();
    }
}

private bool Request_www_sythe_org(out HttpWebResponse response)
{
    response = null;

    try
    {
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://www.sythe.org/login/login");

        request.KeepAlive = true;
        request.Headers.Set(HttpRequestHeader.CacheControl, "max-age=0");
        request.Headers.Add("Origin", @"https://www.sythe.org");
        request.Headers.Add("Upgrade-Insecure-Requests", @"1");
        request.ContentType = "application/x-www-form-urlencoded";
        request.UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.86 Safari/537.36";
        request.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3";
        request.Referer = "https://www.sythe.org/";
        request.Headers.Set(HttpRequestHeader.AcceptEncoding, "gzip, deflate, br");
        request.Headers.Set(HttpRequestHeader.AcceptLanguage, "en-GB,en-US;q=0.9,en;q=0.8");
        request.Headers.Set(HttpRequestHeader.Cookie, @"__cfduid=de8826b5a5df064d6f2ccf2d0952ce52d1554285418; _ga=GA1.2.1744286790.1554285420; _gid=GA1.2.1835584013.1554285420; G_ENABLED_IDPS=google; sythe_bug_token=c0FFSCNAJio4MzJnZWEjWXlaV1NCempSdWFrUzRnUHVZUXJKOENicnkwSmZFTXROR1M3MDhUR1ovWTFIVmlvRFZaaVN2MjFHRWtaM2JhT0N1bTJHbEdGSG5VY1dUNjdmVGNZd1RoekNmTmgzaHB4T29OTTZkNytJQWh3cDdjeWlndWpidkJFY0NiRXF4b1ljSVJhN2VGVVd2eEsvK2hrak1pclhvYjRFeWRhT09UUU5seXZDUVlETDlYWUUyazZGajRub3VWV0k5aVhkVVJiWUtxcFRuaTljRElnWEVKd2o4T3ZSZlRheFEzRWRzekgxSXN4d0doMS9YRFR6L1d0OGRONUVLcDZHUEN3eVFRWEQ%3D; _gat=1; xf_session=63943f38ebba0827a4208d363cae5dcb");
        request.Headers.Add("AlexaToolbar-ALX_NS_PH", @"AlexaToolbar/alx-4.0.3");

        request.Method = "POST";
        request.ServicePoint.Expect100Continue = false;

        string body = @"login=testtest%40testtest.testtest&register=0&password=testtesttesttest&remember=1&cookie_check=1&redirect=%2F&_xfToken=";
        byte[] postBytes = System.Text.Encoding.UTF8.GetBytes(body);
        request.ContentLength = postBytes.Length;
        Stream stream = request.GetRequestStream();
        stream.Write(postBytes, 0, postBytes.Length);
        stream.Close();

        response = (HttpWebResponse)request.GetResponse();
    }
    catch (WebException e)
    {
        if (e.Status == WebExceptionStatus.ProtocolError) response = (HttpWebResponse)e.Response;
        else return false;
    }
    catch (Exception)
    {
        if(response != null) response.Close();
        return false;
    }

    return true;
}

答案 1 :(得分:0)

尝试将string formUrl = "http://www.sythe.org/login/";替换为string formUrl = "http://www.sythe.org/login/login";