1
我試圖通過C#登錄網站,然後我想在beeing登錄後讀出該網站提供的HTML代碼。Httpwebrequest登錄到頁面但始終與答案相同的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;
}
}
}
@SLaks我已經添加了cookiecontainer,但問題仍然存在 – Mika