ASP.Net/Network/HttpWebRequest — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
Admin (обсуждение | вклад) м (1 версия) |
(нет различий)
|
Текущая версия на 11:54, 26 мая 2010
Содержание
Header of HttpWebRequest
<%@ Page %>
<%@ Import Namespace="System.IO" %>
<%@ Import Namespace="System.Net" %>
<script language="C#" runat="server">
private void SubmitButton_Click(System.Object sender, System.EventArgs e){
String RequestUrl = "http://www.ebay.ru";
HttpWebRequest webRequestObject = null;
StreamReader sr = null;
HttpWebResponse webResponseObject = null;
try{
webRequestObject = (HttpWebRequest) WebRequest.Create(RequestUrl);
webRequestObject.Method = "GET";
webResponseObject = (HttpWebResponse) webRequestObject.GetResponse();
int i;
HeadersText.Text = "";
for(i=0; i < webResponseObject.Headers.Count; i++){
HeadersText.Text += Environment.NewLine + "Header Name: " + webResponseObject.Headers.Keys[i] + " Value :" + webResponseObject.Headers[i];
}
CookieCollection myCookieCollection = webResponseObject.Cookies;
for(i=0; i < myCookieCollection.Count; i++){
HeadersText.Text += Environment.NewLine + "Cookie Name: " + myCookieCollection[i].Name + " Value= " + myCookieCollection[i].Value;
}
sr = new StreamReader(webResponseObject.GetResponseStream());
String Results = sr.ReadToEnd();
WebResponseLabel.Text = Results;
WebResponseText.Text = Results;
}
finally{
try{
sr.Close();
webResponseObject.Close();
webRequestObject.Abort();
}
catch{}
}
}
</script>
<html>
<head>
<title></title>
</head>
<body>
<form runat="server" ID="Form1">
<asp:Button Runat="server" ID="SubmitButton" Text="Get Headers and Cookies" OnClick="SubmitButton_Click" /><br>
<asp:TextBox Runat="server" id="HeadersText" Width="780" Height="300" TextMode="MultiLine" /><br>
<asp:TextBox Runat="server" id="WebResponseText" Width="780" Height="300" TextMode="MultiLine" /><br>
<asp:Label Runat="server" ID="WebResponseLabel" />
</form>
</body>
</html>
HttpWebRequest AllowAutoRedirect
<%@ Page %>
<%@ Import Namespace="System.IO" %>
<%@ Import Namespace="System.Net" %>
<script language="C#" runat="server">
private void SubmitButton_Click(System.Object sender, System.EventArgs e){
String RequestUrl = Request.Url.GetLeftPart(System.UriPartial.Authority) + Request.ApplicationPath + "/NextPage.aspx";
HttpWebRequest webRequestObject = null;
StreamReader sr = null;
HttpWebResponse webResponseObject = null;
try{
webRequestObject = (HttpWebRequest) WebRequest.Create(RequestUrl);
webRequestObject.Method = "GET";
webRequestObject.AllowAutoRedirect = false;
webResponseObject = (HttpWebResponse) webRequestObject.GetResponse();
sr = new StreamReader(webResponseObject.GetResponseStream());
String Results = sr.ReadToEnd();
WebResponseLabel.Text = Results;
WebResponseText.Text = Results;
}
finally{
try{
sr.Close();
webResponseObject.Close();
webRequestObject.Abort();
}
catch{}
}
}
</script>
<html>
<head>
<title></title>
</head>
<body>
<form runat="server" ID="Form1">
<asp:Button Runat="server" ID="SubmitButton" Text="Get Redirecting Page" OnClick="SubmitButton_Click" /><br>
<asp:TextBox Runat="server" id="WebResponseText" Width="780" Height="300" TextMode="MultiLine" /><br>
<asp:Label Runat="server" ID="WebResponseLabel" />
</form>
</body>
</html>
File: NextPage.aspx
<%@ Page EnableViewstate="False" %>
<script language="VB" runat="server">
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs)
Response.Redirect("http://nfex.ru")
End Sub
</script>
Read Cookie value from HttpWebRequest
<%@ Page %>
<%@ Import Namespace="System.IO" %>
<%@ Import Namespace="System.Net" %>
<script language="C#" runat="server">
private void SubmitButton_Click(System.Object sender, System.EventArgs e){
String RequestUrl = Request.Url.GetLeftPart(System.UriPartial.Authority) + Request.ApplicationPath + "/NextPage.aspx";
HttpWebRequest webRequestObject = null;
StreamReader sr = null;
HttpWebResponse webResponseObject = null;
try{
webRequestObject = (HttpWebRequest) WebRequest.Create(RequestUrl);
webRequestObject.Method = "GET";
System.Net.CookieContainer CookieContainerObject = new System.Net.CookieContainer();
System.Net.Cookie Cookie = new System.Net.Cookie();
Cookie.Name = "userid";
Cookie.Value = "1234567890";
Cookie.Domain = Request.ServerVariables["HTTP_HOST"];
Cookie.Secure = true;
CookieContainerObject.Add(Cookie);
webRequestObject.CookieContainer = CookieContainerObject;
webResponseObject = (HttpWebResponse) webRequestObject.GetResponse();
sr = new StreamReader(webResponseObject.GetResponseStream());
String Results = sr.ReadToEnd();
WebResponseLabel.Text = Results;
WebResponseText.Text = Results;
}
finally{
try{
sr.Close();
webResponseObject.Close();
webRequestObject.Abort();
}
catch{}
}
}
</script>
<html>
<head>
<title></title>
</head>
<body>
<form runat="server" ID="Form1">
<asp:Button Runat="server" ID="SubmitButton" Text="Get Page Requiring Cookies" OnClick="SubmitButton_Click" />
<a href="NextPage.aspx">View Page without Cookie</a><br>
<asp:TextBox Runat="server" id="WebResponseText" Width="780" Height="300" TextMode="MultiLine" /><br>
<asp:Label Runat="server" ID="WebResponseLabel" />
</form>
</body>
</html>
File: NextPage.aspx
<%@ Page EnableViewstate="False" %>
<%@ Import Namespace="System.Net" %>
<script language="VB" runat="server">
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs)
Dim MyCookie as HttpCookie = Request.Cookies("userid")
If MyCookie is nothing then
Label.Text = "You need cookies turned on to view this page."
Else
Label.Text = "Thank you for turning on cookies. Your userid is: " & MyCookie.Value
End If
End Sub
</script>
<html>
<body>
<form runat="server">
<asp:Label Runat="server" ID="Label" />
</form>
</body>
</html>
Read Stream from HttpWebRequest
<%@ Page %>
<%@ Import Namespace="System.IO" %>
<%@ Import Namespace="System.Net" %>
<script language="C#" runat="server">
private void SubmitButton_Click(System.Object sender, System.EventArgs e){
String RequestUrl = UrlTextbox.Text;
HttpWebRequest webRequestObject = null;
StreamReader sr = null;
HttpWebResponse webResponseObject = null;
try{
webRequestObject = (HttpWebRequest) WebRequest.Create(RequestUrl);
webRequestObject.Method = "GET";
webResponseObject = (HttpWebResponse) webRequestObject.GetResponse();
sr = new StreamReader(webResponseObject.GetResponseStream());
String Results = sr.ReadToEnd();
WebResponseLabel.Text = Results;
WebResponseText.Text = Results;
}
finally{
try{
sr.Close();
webResponseObject.Close();
webRequestObject.Abort();
}
catch{}
}
}
</script>
<html>
<head>
<title></title>
</head>
<body>
<form runat="server" ID="Form1">
<asp:TextBox Runat="server" id="UrlTextbox" Text="http://www.nfex.ru" />
<asp:Button Runat="server" ID="SubmitButton" Text="Get Page" OnClick="SubmitButton_Click" /><br>
<asp:TextBox Runat="server" id="WebResponseText" Width="780" Height="300" TextMode="MultiLine" />
<asp:Label Runat="server" ID="WebResponseLabel" />
</form>
</body>
</html>
Search Amazon
<%@ Page %>
<%@ Import Namespace="System.IO" %>
<%@ Import Namespace="System.Net" %>
<script language="C#" runat="server">
private void SubmitButton_Click(System.Object sender, System.EventArgs e){
String RequestUrl = "http://www.amazon.ru/exec/obidos/search-handle-form";
String Post = "field-keywords=" + SearchText.Text;
StreamWriter Writer = null;
HttpWebRequest webRequestObject = null;
StreamReader sr = null;
HttpWebResponse webResponseObject = null;
try{
webRequestObject = (HttpWebRequest) WebRequest.Create(RequestUrl);
webRequestObject.Method = "POST";
webRequestObject.ContentType = "application/x-www-form-urlencoded";
webRequestObject.ContentLength = Post.Length;
Writer = new StreamWriter(webRequestObject.GetRequestStream());
Writer.Write(Post);
Writer.Close();
webResponseObject = (HttpWebResponse) webRequestObject.GetResponse();
sr = new StreamReader(webResponseObject.GetResponseStream());
String Results = sr.ReadToEnd();
WebResponseLabel.Text = Results;
WebResponseText.Text = Results;
}
finally{
try{
sr.Close();
webResponseObject.Close();
webRequestObject.Abort();
}
catch{}
}
}
</script>
<html>
<head>
<title></title>
</head>
<body>
<form runat="server" ID="Form1">
Search Amazon for:
<asp:TextBox Runat="server" ID="SearchText" Text="Java" />
<asp:Button Runat="server" ID="SubmitButton" Text="Get Amazon.ru Search Results" OnClick="SubmitButton_Click" /><br>
<asp:TextBox Runat="server" id="WebResponseText" Width="780" Height="300" TextMode="MultiLine" />
<asp:Label Runat="server" ID="WebResponseLabel" />
</form>
</body>
</html>
Set UserAgent for HttpWebRequest
<%@ Page %>
<%@ Import Namespace="System.IO" %>
<%@ Import Namespace="System.Net" %>
<script language="C#" runat="server">
private void SubmitButton_Click(System.Object sender, System.EventArgs e){
String RequestUrl = Request.Url.GetLeftPart(System.UriPartial.Authority) + Request.ApplicationPath + "/Default.aspx";
HttpWebRequest webRequestObject = null;
StreamReader sr = null;
HttpWebResponse webResponseObject = null;
try{
webRequestObject = (HttpWebRequest) WebRequest.Create(RequestUrl);
webRequestObject.Method = "GET";
webRequestObject.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; .NET CLR 1.0.3705)";
webResponseObject = (HttpWebResponse) webRequestObject.GetResponse();
sr = new StreamReader(webResponseObject.GetResponseStream());
String Results = sr.ReadToEnd();
WebResponseLabel.Text = Results;
WebResponseText.Text = Results;
}
finally{
try{
sr.Close();
webResponseObject.Close();
webRequestObject.Abort();
}
catch{}
}
}
</script>
<html>
<head>
<title></title>
</head>
<body>
<form runat="server" ID="Form1">
<asp:Button Runat="server" ID="SubmitButton" Text="Get Page Requiring Specific Browser Settings" OnClick="SubmitButton_Click" /><br>
<asp:TextBox Runat="server" id="WebResponseText" Width="780" Height="300" TextMode="MultiLine" /><br>
<asp:Label Runat="server" ID="WebResponseLabel" />
</form>
</body>
</html>