如何获得本地某网站的所有Cookie ?
WebClient类可访问远程Web页面如: http://bbs.aspsky.com/index.asp
我在论坛(bbs.aspsky.net)上已登录并保存Cookie 一年
想用WebClient类访问"bbs.aspsky.com/index.asp"时 Cookie信息没有被传递(提示没有登录,用IE直接打开就不用登录)。
现请问两个问题: 1. 如何获得某网站在本地的所有Cookie ?
2. 如何把本地的Cookie信息发送到远程服务器上?
最好能有具体代码。
下面的代码可拷贝做调试使用。
<!------------- C#版 --------------->
<%@ Import Namespace="System.IO" %>
<%@ Import Namespace="System.Net" %>
<script language="C#" runat="server">
void btnSubmit_OnClick(Object sender, EventArgs e)
{
// Create a WebClient instance
WebClient objWebClient = new WebClient();
lblHTML.Text = "<b>Request Header Information:</b><br>";
foreach (String strHeader in objWebClient.Headers)
lblHTML.Text += strHeader + " - " + objWebClient.Headers[strHeader] + "<br>";
// Read the Response into an array of bytes, but use the UTF8Encoding
// class to convert the byte array into a string
UTF8Encoding objUTF8 = new UTF8Encoding();
String strRequestedHTML = objUTF8.GetString(objWebClient.DownloadData(txtURL.Text));
lblHTML.Text += "<p><b>Response Header Information:</b><br>";
foreach (String strHeader in objWebClient.ResponseHeaders)
lblHTML.Text += strHeader + " - " + objWebClient.ResponseHeaders[strHeader] + "<br>";
// Output the contents of the Web request
lblHTML.Text += strRequestedHTML;
}
</script>
<html>
<body>
<form runat="server">
<font size=+1><b>Poor Man's Internet Explorer</b></font>
<br>Browse the Web:
<asp:textbox id="txtURL" runat="server" /><br>
<i>Enter a URL starting with <code>http://</code></i><br>
<asp:button id="btnSubmit" runat="server" Text=" Go! "
OnClick="btnSubmit_OnClick" />
<p><hr><p>
<asp:label id="lblHTML" runat="server" />
</form>
</body>
</html>
<!--====================================-->
<!--------------VB版---------------------->
<%@ Import Namespace="System.Net" %>
<script language="VB" runat="server">
Sub btnSubmit_OnClick(sender as Object, e as EventArgs)
'Create a WebClient instance
Dim objWebClient as New WebClient()
Dim strHeader as String
lblHTML.Text = "<b>Request Header Information:</b><br>"
For Each strHeader in objWebClient.Headers
lblHTML.Text &= strHeader & " - " & _
objWebClient.Headers(strHeader) & "<br>"
Next
'Read the Response into an array of bytes, but use the UTF8Encoding
'class to convert the byte array into a string
Dim objUTF8 as New UTF8Encoding()
Dim strRequestedHTML as String
strRequestedHTML = objUTF8.GetString(objWebClient.DownloadData(txtURL.Text))
lblHTML.Text &= "<p><b>Response Header Information:</b><br>"
For Each strHeader in objWebClient.ResponseHeaders
lblHTML.Text &= strHeader & " - " & _
objWebClient.ResponseHeaders(strHeader) & "<br>"
Next
'Output the contents of the Web request
lblHTML.Text &= strRequestedHTML
End Sub
</script>
<html>
<body>
<form runat="server">
<font size=+1><b>Poor Man's Internet Explorer</b></font>
<br>Browse the Web:
<asp:textbox id="txtURL" runat="server" /><br>
<i>Enter a URL starting with <code>http://</code></i><br>
<asp:button id="btnSubmit" runat="server" Text=" Go! "
OnClick="btnSubmit_OnClick" />
<p><hr><p>
<asp:label id="lblHTML" runat="server" />
</form>
</body>
</html>