在Winform中如何向http目标地址以post的方式提交请求,并抓取页面输出内容呢?

lcy_008 2006-06-22 09:19:37
如题。100送上。
...全文
694 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
shalen520 2006-06-22
  • 打赏
  • 举报
回复
//这个新建的Cookie集合不知道有什么用??
//req.CookieContainer =new CookieContainer ();
=================================================
如果需要在请求之间保存Session状态,就有用了
固执的染色体 2006-06-22
  • 打赏
  • 举报
回复
mark
blestcc 2006-06-22
  • 打赏
  • 举报
回复
#region get数据类GetDate
/// <summary>
/// Get数据类
/// </summary>
public class GetDate
{
private string sPostUrl;
/// <summary>
/// 提交地址
/// </summary>
public string postUrl
{
get
{
return sPostUrl;
}
set
{
sPostUrl=value;
}
}
private CookieContainer sSessionId;
/// <summary>
/// Cookies标志
/// </summary>
public CookieContainer SessionId
{
get
{
return sSessionId;
}
set
{
sSessionId=value;
}
}
private string sReturnString;
/// <summary>
/// 返回字符串
/// </summary>
/// <returns></returns>
public string ReturnString
{
get
{
return sReturnString;
}
set
{
sReturnString=value;
}
}

public void Send()
{
HttpWebRequest request = (HttpWebRequest) WebRequest.Create(sPostUrl);
request.CookieContainer=sSessionId;
request.Method="get";
//request.Timeout=2240;
HttpWebResponse response = (HttpWebResponse) request.GetResponse();
byte[] buf = new byte[1024];
int count=0;
while(true)
{
//resStream.Write(byte1,0,byte1.Length);
count = response.GetResponseStream().Read(buf, 0, buf.Length);
if (count<=0)
{
break;
}
sReturnString+=Encoding.Unicode.GetString(buf,0,count);
}
}
}

#endregion
我看你有戏 2006-06-22
  • 打赏
  • 举报
回复
public static void PostDate()
{
//建立登录检查地址
string url = @"http://howaa.baoyaa.com/smslt.asp";
//建立request对象
System.Net.HttpWebRequest req=(System.Net.HttpWebRequest)System.Net.HttpWebRequest.Create(url);
//这个新建的Cookie集合不知道有什么用??
//req.CookieContainer =new CookieContainer ();

req.Method="POST";//POST方式请求
req.ContentType= "application/x-www-form-urlencoded";//内容类型


//参数经过URL编码
string paraUrlCoded;
paraUrlCoded=System.Web.HttpUtility.UrlEncode("MobilePhone") +"="+System.Web.HttpUtility.UrlEncode("13564250347");
// paraUrlCoded+="&";
// paraUrlCoded+=System.Web.HttpUtility.UrlEncode("passwd")+"="+System.Web.HttpUtility.UrlEncode("999999");



//将URL编码后的字符串转化为字节
try
{
byte[] payload;
payload=System.Text.Encoding.UTF8.GetBytes(paraUrlCoded);
req.ContentLength=payload.Length; //设置请求的ContentLength
System.IO.Stream writer=req.GetRequestStream();//获得请求流
writer.Write(payload,0,payload.Length);//将请求参数写入流
writer.Close();//关闭请求流
}
catch
{}

//获得响应流


}
Yuna_2z 2006-06-22
  • 打赏
  • 举报
回复
MARK
shalen520 2006-06-22
  • 打赏
  • 举报
回复
using System.Net;


HttpWebRequest req = (HttpWebRequest)WebRequest.Create("http://abc.com.cn/xyz.aspx");
//构造要post的数据
ASCIIEncoding encoding=new ASCIIEncoding();
string postdata="a=xxx&b=xxx";
byte[] byte1=encoding.GetBytes(postdata);
//Post
req.Method = "POST";
req.ContentType="application/x-www-form-urlencoded";
req.ContentLength=postdata.Length;
Stream s=req.GetRequestStream();
s.Write(byte1,0,byte1.Length);
s.Close();
//获取响应
HttpWebResponse rp= (HttpWebResponse)req.GetResponse();
Stream rps= rp.GetResponseStream();
StreamReader sr= new StreamReader(rps);
//获得响应字符串
string result = sr.ReadToEnd();
rp.Close();
YAOHE 2006-06-22
  • 打赏
  • 举报
回复
System.Text.Encoding encode = System.Text.Encoding.GetEncoding("GB2312");
tmpurl="请求URL信息";
System.Net.WebClient wc=new WebClient();
System.IO.StreamReader sr=new System.IO.StreamReader( wc.OpenRead(tmpurl),encode);
tmpurl="";
tmpurl=sr.ReadToEnd();
tmpurl即取得返回所有信息
lcy_008 2006-06-22
  • 打赏
  • 举报
回复
调用webclient执行postdata么?

110,545

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧