我的目标是使用我的应用程序在http://uk.advfn.com上登录http://uk.advfn.com,我是C#中的新手,我从另一个链接中了解了以下代码,但我无法管理解决我的。当我调试时,response
显示不良的登录页面,而不是登录成功页面。谁能为我调查我在哪里做的错误?
我使用篡改数据firefox插件来获取那些需要的值,但是我不确定是否正确使用它们。
您的帮助非常感谢!谢谢。:)
第1部分:
public class CookieAwareWebClient : WebClient
{
public string Method;
public CookieContainer CookieContainer { get; set; }
public Uri Uri { get; set; }
public CookieAwareWebClient()
: this(new CookieContainer())
{
}
public CookieAwareWebClient(CookieContainer cookies)
{
this.CookieContainer = cookies;
}
protected override WebRequest GetWebRequest(Uri address)
{
WebRequest request = base.GetWebRequest(address);
if (request is HttpWebRequest)
{
(request as HttpWebRequest).CookieContainer = this.CookieContainer;
(request as HttpWebRequest).ServicePoint.Expect100Continue = false;
(request as HttpWebRequest).UserAgent = "Mozilla/5.0 (Windows NT 6.3; WOW64; rv:26.0) Gecko/20100101 Firefox/26.0";
(request as HttpWebRequest).Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
(request as HttpWebRequest).Headers.Add(HttpRequestHeader.AcceptLanguage, "en-US,en;q=0.5");
(request as HttpWebRequest).Referer = "http://uk.advfn.com/";
(request as HttpWebRequest).KeepAlive = true;
(request as HttpWebRequest).AutomaticDecompression = DecompressionMethods.Deflate | DecompressionMethods.GZip;
if (Method == "POST")
{
(request as HttpWebRequest).ContentType = "application/x-www-form-urlencoded";
}
}
HttpWebRequest httpRequest = (HttpWebRequest)request;
httpRequest.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;
return httpRequest;
}
protected override WebResponse GetWebResponse(WebRequest request)
{
WebResponse response = base.GetWebResponse(request);
String setCookieHeader = response.Headers[HttpResponseHeader.SetCookie];
if (setCookieHeader != null)
{
//do something if needed to parse out the cookie.
try
{
if (setCookieHeader != null)
{
Cookie cookie = new Cookie(); //create cookie
this.CookieContainer.Add(cookie);
}
}
catch (Exception)
{
}
}
return response;
}
}
第2部分:
private void btn_login_Click(object sender, EventArgs e)
{
var cookieJar = new CookieContainer();
CookieAwareWebClient client = new CookieAwareWebClient(cookieJar);
string response = client.DownloadString("http://uk.advfn.com/common/account/login");
string postData = string.Format("redirect_url=aHR0cDovL3VrLmFkdmZuLmNvbQ%3D%3D&site=uk&login_username=demouser&login_password=demopassword");
client.Method = "POST";
response = client.UploadString("https://secure.advfn.com/login/secure", postData);
}
我将使用Fiddler捕获通过浏览器完成的成功登录的HTTP请求,然后将其与应用程序生成的请求进行比较。两者中的任何差异都可以掌握为什么该应用程序无法成功登录的线索。
几年前,我对网站进行了一些远程登录,我保存了对我有帮助的stackoverflow问题。
登录网站,通过C#
我找不到我的代码,因为我没有在过去的同一家公司工作,但希望这会有所帮助...
我已经尝试了您的代码,并且可以正常工作。我唯一要做的就是更改密码并登录以纠正一个。
我猜你有"不良登录"消息的原因是因为您实际上输入了不良登录和密码。
我想我只会对自己的问题进行更新,以便它可以帮助他人。
我最终跳过了所有复杂的代码,我使用硒来登录网站,然后下载所需的文件。Selenium确实是一种功能强大的工具,我只使用硒和Firefox,在每个下载中使用一定间隔下载了900多个文件。特别感谢@vdohnal向我发表评论,不幸的是,我只能对他的评论进行投票。
没有"答案"。代码是从硒IDE导出的,我对代码进行了一些更改,并添加了需要下载的部分。
using System;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using NUnit.Framework;
using OpenQA.Selenium;
using OpenQA.Selenium.Firefox;
using OpenQA.Selenium.Support.UI;
namespace SeleniumTests
{
private IWebDriver driver;
private StringBuilder verificationErrors;
private string baseURL;
private bool acceptNextAlert = true;
private bool IsElementPresent(By by)
{
try
{
driver.FindElement(by);
return true;
}
catch (NoSuchElementException)
{
return false;
}
}
private bool IsAlertPresent()
{
try
{
driver.SwitchTo().Alert();
return true;
}
catch (NoAlertPresentException)
{
return false;
}
}
private string CloseAlertAndGetItsText()
{
try
{
IAlert alert = driver.SwitchTo().Alert();
string alertText = alert.Text;
if (acceptNextAlert)
{
alert.Accept();
}
else
{
alert.Dismiss();
}
return alertText;
}
finally
{
acceptNextAlert = true;
}
}
public void TheCTest()
{
try
{
driver = new FirefoxDriver();
baseURL = "http://uk.advfn.com";
verificationErrors = new StringBuilder();
driver.Navigate().GoToUrl(baseURL + "/common/account/login");
driver.FindElement(By.Id("login_username")).Clear();
driver.FindElement(By.Id("login_username")).SendKeys("demouser");
driver.FindElement(By.Id("login_password")).Clear();
driver.FindElement(By.Id("login_password")).SendKeys("demopass");
driver.FindElement(By.Id("login_submit")).Click();
Thread.Sleep(30000);
//driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(60));
foreach (DataGridViewRow row in dataGridView_FetchTickers.Rows)
{
if (row.Cells[1].Value != null)
{
try
{
driver.Navigate().GoToUrl(baseURL + "/p.php?pid=data&daily=0&symbol=L%5E" + row.Cells[1].Value.ToString());
//driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(60));
Thread.Sleep(30000);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
else
{
//Do nothing
}
}
}
catch (Exception)
{
//
}
}
private void btn_fetchSharePrices_Click(object sender, EventArgs e)
{
TheCTest();
}
}