在C#中使用POST请求登录网站

时间:2019-03-10 03:45:38

标签: c# post login python-requests

我正在尝试使用c#向Runescape发出登录请求。我的问题是我尚未登录。页面的源显示正常,但是我尚未登录。这是到目前为止的代码。页面的HTML显示我仍然需要登录。我认为这可能是由于Cookie或标题引起的,但是我不确定,如果有人可以帮助我,将不胜感激

`using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Threading;
using System.Net;
using System.Text.RegularExpressions;

namespace ConsoleApp1
{
 class Program
{
    static void Main(string[] args)
    {
        String username = "my username is here";
        String password = "password here";
                    ServicePointManager.Expect100Continue = false;
                    ServicePointManager.MaxServicePointIdleTime = 2000;
                    ServicePointManager.SecurityProtocol = 
                    SecurityProtocolType.Tls12;
                    byte[] buffer = Encoding.ASCII.GetBytes("username=" + 
                    username.Replace(" ", "20%") + "&password=" + 
                    password.Replace(" ", "20%") + 
                    "&mod=www&ssl=0&dest=community%2C");
                    HttpWebRequest WebReq = (HttpWebRequest)WebRequest.Create("https://secure.runescape.com/m=weblogin/login.ws");           
                    WebReq.UserAgent = "Mozilla/5.0 (Windows NT 10.0; WOW64; rv:50.0) Gecko/20100101 Firefox/50.0";
                    WebReq.Method = "POST";
                    WebReq.Referer = "https://secure.runescape.com/m=weblogin/login.ws";
                    WebReq.ContentType = "application/x-www-form-urlencoded";
                    WebReq.ContentLength = buffer.Length;
                    Stream PostData = WebReq.GetRequestStream();
                    PostData.Write(buffer, 0, buffer.Length);
                    PostData.Close();
                    HttpWebResponse WebResp = 
                    (HttpWebResponse)WebReq.GetResponse();
                    Stream Answer = WebResp.GetResponseStream();
                    StreamReader _Answer = new StreamReader(Answer);
                    string data = _Answer.ReadToEnd();
                    Console.WriteLine(data);
                    Console.ReadKey();


    }
}

}`

0 个答案:

没有答案