[英]Httpwebrequest login to page but always same html code as answer
我试图通过C#在网站上登录,然后我想在登录后读出网站提供的HTML代码。
问题是我正在发送登录信息,但是却收到了与请求网站主页相同的HTML代码。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using System.Net;
using System.Media;
using System.Threading;
namespace OgameBot
{
public partial class Form1 : Form
{
string responsePath = @"C:\Users\mightymate\Desktop\response.txt";
CookieContainer OurCookies = new CookieContainer();
string page;
public Form1()
{
InitializeComponent();
//string[] currentFile = File.ReadAllLines(responsePath);
//foreach (string s in currentFile)
//{
// richTextBox1.AppendText(s+"\n");
//}
GetPage("http://de.ogame.gameforge.com/main/login", true);
Thread.Sleep(1000);
PostPage("http://de.ogame.gameforge.com/main/login", "kid=&uni=s123-de.ogame.gameforge.com&login=Test&pass=1234", true);
//PostPage("http://de.ogame.gameforge.com/main/login", "kid=&uni=s123-de.ogame.gameforge.com&login=Test&pass=1234", true);
}
private void GetPage(string URL, bool write)
{
HttpWebRequest WR = (HttpWebRequest)WebRequest.Create(URL);
WebHeaderCollection WHC = WR.Headers;
int temp = WHC.Count;
WR.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:26.0) Gecko/20100101 Firefox/26.0";
WR.CookieContainer = OurCookies;
WR.Method = "GET";
HttpWebResponse HWR = (HttpWebResponse)WR.GetResponse();
Stream dataStream = HWR.GetResponseStream();
if(write)
{
string response;
using (StreamReader SR = new StreamReader(dataStream))
{
response = SR.ReadToEnd();
}
richTextBox1.AppendText(GetStringFromCookieContainer(OurCookies, URL));
richTextBox1.AppendText(response);
page = response;
}
}
private void PostPage(string URL, string Request, bool write)
{
byte[] DataToSend = StringToByteArray(Request);
HttpWebRequest WebReq = (HttpWebRequest)WebRequest.Create(URL);
WebReq.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:26.0) Gecko/20100101 Firefox/26.0";
WebReq.CookieContainer = OurCookies;
WebReq.Method = "POST";
WebReq.ContentLength = DataToSend.Length;
WebReq.ContentType = "application/x-wwww-form-urlencoded";
using (Stream SendingStream = WebReq.GetRequestStream())
{
SendingStream.Write(DataToSend, 0, DataToSend.Length);
}
if(write)
{
using (HttpWebResponse WebResp = (HttpWebResponse)WebReq.GetResponse())
{
using (StreamReader SR = new StreamReader(WebResp.GetResponseStream()))
{
string serverReponse = SR.ReadToEnd();
richTextBox2.AppendText(GetStringFromCookieContainer(OurCookies, URL)+"\n\n");
richTextBox2.AppendText(serverReponse);
}
}
}
}
private byte[] StringToByteArray(string s)
{
return new ASCIIEncoding().GetBytes(s);
}
private string GetStringFromCookieContainer(CookieContainer CC, string fromUrl)
{
string cookieString=string.Empty;
CookieCollection currentCookies = OurCookies.GetCookies(new System.Uri(fromUrl));
foreach (Cookie C in currentCookies)
{
cookieString += "Comment: " + C.Comment + "\n" +
"CommentUri: " + C.CommentUri + "\n" +
"Domain: " + C.Domain + "\n" +
"Expires: " + C.Expires + "\n" +
"Name: " + C.Name + "\n" +
"Path: " + C.Path + "\n" +
"Port: " + C.Port + "\n" +
"Secure: " + C.Secure + "\n" +
"Timestamp: " + C.TimeStamp + "\n" +
"Value: " + C.Value + "\n" +
"Version: " + C.Version + "\n";
}
return cookieString;
}
}
}
您需要保留cookie,以便网站看到您已登录。
创建一个CookieContainer
并在两个请求上都进行设置。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.