我正在尝试构建c#应用程序,它在网站中存在“更新”时通知我。 网站登录表单包含3个文本框,它是login.aspx。从c#应用程序连接到.aspx网站
我的问题是,如何将3个细节“发送”到站点并从我想在c#中构建的应用程序进行连接(身份验证),如果可能的话,我该怎么做? 我寻找任何指南或东西阅读关于这个,但没有找到。
我正在尝试构建c#应用程序,它在网站中存在“更新”时通知我。 网站登录表单包含3个文本框,它是login.aspx。从c#应用程序连接到.aspx网站
我的问题是,如何将3个细节“发送”到站点并从我想在c#中构建的应用程序进行连接(身份验证),如果可能的话,我该怎么做? 我寻找任何指南或东西阅读关于这个,但没有找到。
您需要使用WebClient
类。这个类的更多信息,可以在http://msdn.microsoft.com/en-us/library/system.net.webclient(v=vs.80).aspx
谢谢,但有一个问题。我知道我需要知道页面发送的参数,但是当我使用嗅探器尝试时,我发现内容被加密为很多字母。如何做到这一点? – user1840196
First you need post a form using c#
HttpWebRequest request = (HttpWebRequest)WebRequest.Create (args[0]);
// Set some reasonable limits on resources used by this request
request.MaximumAutomaticRedirections = 4;
request.MaximumResponseHeadersLength = 4;
// Set credentials to use for this request.
request.Credentials = CredentialCache.DefaultCredentials;
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Console.WriteLine ("Content length is {0}", response.ContentLength);
Console.WriteLine ("Content type is {0}", response.ContentType);
// Get the stream associated with the response.
Stream receiveStream = response.GetResponseStream();
// Pipes the stream to a higher level stream reader with the required encoding format.
StreamReader readStream = new StreamReader (receiveStream, Encoding.UTF8);
Console.WriteLine ("Response stream received.");
Console.WriteLine (readStream.ReadToEnd());
response.Close();
readStream.Close();
then try to save cookie, its required to store aspnet_session_id into client for future requests
private class CookieAwareWebClient : WebClient
{
public CookieAwareWebClient()
: this(new CookieContainer())
{ }
public CookieAwareWebClient(CookieContainer c)
{
this.CookieContainer = c;
}
public CookieContainer CookieContainer { get; set; }
protected override WebRequest GetWebRequest(Uri address)
{
WebRequest request = base.GetWebRequest(address);
if (request is HttpWebRequest)
{
(request as HttpWebRequest).CookieContainer = this.CookieContainer;
}
return request;
}
}
一个很好的例子可以找到确保您发送的和每个请求恢复aspnet_session_id 。 和宾果!
我建议您阅读this。
您需要模拟Web浏览器的行为,例如:身份验证,cookie和重定向。所以WebClient类可能会帮助你 – pylover