ASP.NET 如何在一个类中用C#代码POST提交表单 (高分求解)!!!
我要做的 功能是 在类库中自定义了一个类,在类的一个方法里面 post提交表单 根据用户名,密码,路径实现登录别人的网站,
通过查看目标网站的源文件 已经获得 用户名和密码的input标签ID,name
用js 可以实现 document.getElementById("FormID").submit();
但是我要在类中的方法里面实现,,,求解
------解决方案--------------------http://blog.csdn.net/a497785609/article/details/6437154
看看这篇文章
------解决方案--------------------http://www.cnblogs.com/changyou/archive/2010/01/09/1643166.html
------解决方案--------------------ASP.NET怎么发到asp版了,这里回答的人少!
------解决方案--------------------
public string GetPage(string posturl, string postData)
{
Stream outstream = null;
Stream instream = null;
StreamReader sr = null;
HttpWebResponse response = null;
HttpWebRequest request = null;
Encoding encoding = System.Text.Encoding.GetEncoding("utf-8");
byte[] data = encoding.GetBytes(postData);
// 准备请求...
try
{
// 设置参数
request = WebRequest.Create(posturl) as HttpWebRequest;
CookieContainer cookieContainer = new CookieContainer();
request.CookieContainer = cookieContainer;
request.AllowAutoRedirect = true;
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = data.Length;
outstream = request.GetRequestStream();
outstream.Write(data, 0, data.Length);
// outstream.Close();
//发送请求并获取相应回应数据
response = request.GetResponse() as HttpWebResponse;
//直到request.GetResponse()程序才开始向目标网页发送Post请求
instream = response.GetResponseStream();
sr = new StreamReader(instream, encoding);
//返回结果网页(html)代码
string content = sr.ReadToEnd();
string err = string.Empty;
return content;
}
catch (Exception ex)
{
string err = ex.Message;
return string.Empty;
}
}