我正在提交一个带有我的C#代码的aspx,它运行良好,但是我将html直接返回,因为我没有在浏览器中显示它。
这是我的C#代码:
string getUrl = "https://www.facebook.com/login.php?login_attempt=1";
string email = "email@email.com";
string pw = "pwd";
string postData = String.Format("email={0}&pass={1}", email, pw);
HttpWebRequest getRequest = (HttpWebRequest)WebRequest.Create(getUrl);
getRequest.CookieContainer = new CookieContainer();
getRequest.CookieContainer.Add(cookies); //recover cookies First request
getRequest.Method = WebRequestMethods.Http.Post;
getRequest.UserAgent = "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/535.2 (KHTML, like Gecko) Chrome/15.0.874.121 Safari/535.2";
getRequest.AllowWriteStreamBuffering = true;
getRequest.ProtocolVersion = HttpVersion.Version11;
getRequest.AllowAutoRedirect = true;
getRequest.ContentType = "application/x-www-form-urlencoded";
byte[] byteArray = Encoding.ASCII.GetBytes(postData);
getRequest.ContentLength = byteArray.Length;
Stream newStream = getRequest.GetRequestStream(); //open connection
newStream.Write(byteArray, 0, byteArray.Length); // Send the data.
newStream.Close();
HttpWebResponse getResponse = (HttpWebResponse)getRequest.GetResponse();
string sourceCode = "";
using (StreamReader sr = new StreamReader(getResponse.GetResponseStream()))
{
sourceCode = sr.ReadToEnd();
}
这为我提供了我需要的响应,但是页面应首先加载javascript而不是以html的形式发送回来,不幸的是它不会这样做。
我一直在寻找:
当然我更喜欢这个:
HttpWebResponse getResponse = (HttpWebResponse)getRequest.GetResponse();
我试过: - 使用WebBrowser wb = new WebBrowser();但是这会产生单个线程错误,似乎无法将页面发布到url。 - while(wb.ReadyState!= WebBrowserReadyState.Complete),这不能用,因为我没有加载实际页面但只得到响应
任何人都有一个好的和聪明的想法,用POST在浏览器中加载页面,等待javascript被执行,然后我将这个html加载到我的C#代码中?