c# HttpWebResponse判断返回请求的问题

目前已经从listbox1中读取数据,以HttpWebRequest提交到网页,现在以HttpWebResponse方式获取返回信息。for(inti=0;i<listB... 目前已经从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分了全部拿出来。今晚在线等答案。谢谢各位了。。。
展开
 我来答
百度网友6f75ac8fa
2009-05-09 · TA获得超过2513个赞
知道大有可为答主
回答量:1.3万
采纳率:0%
帮助的人:4022万
展开全部
没明白你什么意思给你一个我测试的代码吧
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)
{

}
}
FantasyChump
2009-05-09 · TA获得超过3288个赞
知道大有可为答主
回答量:2127
采纳率:0%
帮助的人:2251万
展开全部
晕倒,搞个功能贴那么长,其实很简单。

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());这句话使用指定的编码来读取,否则可能会乱码。
本回答被提问者采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
谦卑又顽强丶画眉鸟b25
2009-05-09 · TA获得超过114个赞
知道答主
回答量:211
采纳率:0%
帮助的人:99.6万
展开全部
想实现啥功能?
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 1条折叠回答
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式