c# HttpWebResponse判断返回请求的问题
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了c# HttpWebResponse判断返回请求的问题相关的知识,希望对你有一定的参考价值。
目前已经从listbox1中读取数据,以HttpWebRequest提交到网页,现在以HttpWebResponse方式获取返回信息。
for (int i = 0; i < listBox1.Items.Count; i++)
req = (HttpWebRequest)WebRequest.Create(listBox1.Items[i].ToString() + "?action=login&usr=admin&pwd=admin");
res = (HttpWebResponse)req.GetResponse();
res.Close();
req.Abort();
System.Threading.Thread.Sleep(500);
这是现在的代码。希望判断如果返回admin_default.asp则把listBox1.Items[i].ToString() + "?action=login&usr=admin&pwd=admin"中的内容写到listbox2中。希望高手给段代码。。吼吼,就30分了全部拿出来。今晚在线等答案。谢谢各位了。。。
HttpReceiveData.aspx
///
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="HttpReceiveData.aspx.cs" Inherits="HttpReceiveData" %>
HttpReceiveData.aspx.cs
///////
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.htmlControls;
public partial class HttpReceiveData : System.Web.UI.Page
protected void Page_Load(object sender, EventArgs e)
Response.Clear();
Response.Write("nihao" + Request.Form["Desc"] + Request.Form["Title"]);
Response.End();
////string c = Request.Form["nihao"].ToString();
//String a = Request.Form["Title"].ToString();
//String b = Request.Form["Desc"].ToString();
//Common d = new Common();
//d.ShowMessage(this, a + b);
HttpSendData.aspx
//
<%@ Page language="c#" %>
<%@ Import Namespace = "System" %>
<%@ Import Namespace = "System.Collections" %>
<%@ Import Namespace = "System.Web" %>
<%@ Import Namespace = "System.Web.UI" %>
<%@ Import Namespace = "System.Web.UI.WebControls" %>
<%@ Import Namespace = "System.Net" %>
<%@ Import Namespace = "System.IO" %>
<%@ Import Namespace = "System.Text" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<script type="text/C#" runat="server">
void Button1_Click(object sender, System.EventArgs e)
string strTitle = TextBox1.Text;
string strDesc = TextBox2.Text;
//Encoding encoding = Encoding.GetEncoding("GB2312");
//string postData = "Title=" + strTitle;
//string strUrl = "http://localhost/ht/HttpReceiveData.aspx";
//postData += ("&Desc=" + strDesc);
//byte[] data = encoding.GetBytes(postData);
//// 准备请求...
//HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(strUrl);
//myRequest.Method = "POST";
//myRequest.ContentType="application/x-www-form-urlencoded";
//myRequest.ContentLength = data.Length;
//Stream newStream=myRequest.GetRequestStream();
//// 发送数据
//newStream.Write(data,0,data.Length);
//newStream.Close();
CookieContainer cc = new CookieContainer();
string postData = "Title=" + strTitle;
//string strUrl = "http://localhost/ht/HttpReceiveData.aspx";
postData += ("&Desc=" + strDesc);
byte[] byteArray = Encoding.UTF8.GetBytes(postData); // 转化
HttpWebRequest webRequest2 = (HttpWebRequest)WebRequest.Create("http://localhost/ht/HttpReceiveData.aspx");
webRequest2.CookieContainer = cc;
webRequest2.Method = "POST";
webRequest2.ContentType = "application/x-www-form-urlencoded";
webRequest2.ContentLength = byteArray.Length;
Stream newStream = webRequest2.GetRequestStream();
// Send the data.
newStream.Write(byteArray, 0, byteArray.Length); //写入参数
newStream.Close();
HttpWebResponse response2 = (HttpWebResponse)webRequest2.GetResponse();
long t = response2.ContentLength;
StreamReader sr2 = new StreamReader(response2.GetResponseStream(), Encoding.Default);
string text2 = sr2.ReadToEnd();
Response.Redirect("HttpSendData.aspx");
</script>
</HEAD>
<body>
<form id="HTTPPost" method="post" runat="server">
标题:<asp:TextBox id="TextBox1" runat="server"></asp:TextBox>
<br>
内容:
<br>
<asp:TextBox id="TextBox2" runat="server" TextMode="MultiLine" Rows="10" Columns="100"></asp:TextBox>
<br>
<asp:Button id="Button1" runat="server" Text=" 发 送 " onclick="Button1_Click"></asp:Button>
</form>
</body>
</HTML>
HttpSendData.aspx文件
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class HttpSendData : System.Web.UI.Page
protected void Page_Load(object sender, EventArgs e)
参考技术A 晕倒,搞个功能贴那么长,其实很简单。
for (int i = 0; i < listBox1.Items.Count; i++)
req = (HttpWebRequest)WebRequest.Create(listBox1.Items[i].ToString() + "?action=login&usr=admin&pwd=admin");
res = (HttpWebResponse)req.GetResponse();
if(res.StatusCode == HttpStatusCode.Found)
//这里就跳转了,登录成功
else
//否则就读取内容
System.IO.StreamReader sr=new System.IO.StreamReader(res.GetResponseStream());
string content=sr.ReadToEnd(); //这里的content就是网页内容了
sr.Close();
//判断错误信息
if(content.IndexOf("用户名或密码错误")!=-1)
//用户名密码错误?
else
//其它信息
res.Close();
req.Abort();
System.Threading.Thread.Sleep(500);
注意一个问题,就是网页的编码问题,可能你需要调整new System.IO.StreamReader(res.GetResponseStream());这句话使用指定的编码来读取,否则可能会乱码。本回答被提问者采纳 参考技术B 想实现啥功能?
C# HttpWebReqeust和HttpWebResponse发送请求
1 var request = (HttpWebRequest)WebRequest.Create("URL"); 2 3 var data = Encoding.UTF8.GetBytes("username=zhenjinggu&md5Pwd=643d1820b9ce49bbf283b4875848596c&otpCode="); 4 5 request.Method = WebRequestMethods.Http.Post; 6 request.Timeout = 60 * 1000; 7 request.AllowAutoRedirect = true; 8 request.ContentLength = data.Length; 9 request.ContentType = "application/x-www-form-urlencoded"; 10 request.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8"; 11 request.UserAgent = "Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2490.86 Safari/537.36"; 12 var stream = request.GetRequestStream(); 13 stream.Write(data, 0, data.Length); 14 stream.Close(); 15 16 var response = (HttpWebResponse)request.GetResponse(); 17 var sr = new StreamReader(response.GetResponseStream()); 18 var content = sr.ReadToEnd();
以上是关于c# HttpWebResponse判断返回请求的问题的主要内容,如果未能解决你的问题,请参考以下文章
(58)C#里怎么判断HttpWebResponse的返回码
C# HttpWebRequest GET HTTP HTTPS 请求
C# HttpWebReqeust和HttpWebResponse发送请求
C# HttpWebResponse, WebException