我正在尝试构建 c# 应用程序,它会在站点中有“更新”时通知我。站点登录表单包含 3 个文本框,它是 login.aspx。
我的问题是,如何将 3 个详细信息“发送”到站点并从我想用 c# 构建的应用程序连接(身份验证),如果可能的话,我该怎么做?我寻找了任何指南或有关此内容的内容,但没有找到。
我正在尝试构建 c# 应用程序,它会在站点中有“更新”时通知我。站点登录表单包含 3 个文本框,它是 login.aspx。
我的问题是,如何将 3 个详细信息“发送”到站点并从我想用 c# 构建的应用程序连接(身份验证),如果可能的话,我该怎么做?我寻找了任何指南或有关此内容的内容,但没有找到。
你需要使用这个WebClient
类。有关此类的更多信息,请访问http://msdn.microsoft.com/en-us/library/system.net.webclient(v=vs.80).aspx
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 ();
然后尝试保存 cookie,它需要将 aspnet_session_id 存储到客户端以供将来请求
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。还有宾果!!
我建议您阅读此内容。