我把代码写在web页面里,用个按钮提交可以获取到目标页面的源代码,但是我把代码写在winform里,用按钮提交,为什么获取不到目标页面的源代码呢?高手们,帮帮小弟吧!谢谢呀。。。急啊。
代码:
/// <summary>
/// 提供通过POST方法获取页面的方法
/// </summary>
/// <param name="urlString">请求的URL</param>
/// <param name="encoding">页面使用的编码</param>
/// <param name="postDataString">POST数据</param>
/// <returns>获取的页面</returns>
public static string GetHtmlFromPost(string urlString, Encoding encoding, string postDataString)
{
//定义局部变量
System.Net.ServicePointManager.Expect100Continue = false;
CookieContainer cookieContainer = new CookieContainer();
HttpWebRequest httpWebRequest = null;
HttpWebResponse httpWebResponse = null;
Stream inputStream = null;
Stream outputStream = null;
StreamReader streamReader = null;
string htmlString = string.Empty;
//转换POST数据
byte[] postDataByte = encoding.GetBytes(postDataString);
//建立页面请求
try
{
httpWebRequest = WebRequest.Create(urlString) as HttpWebRequest;
}
//处理异常
catch (Exception ex)
{
throw new Exception("建立页面请求时发生错误!", ex);
}
//指定请求处理方式
httpWebRequest.Method = "POST";
httpWebRequest.KeepAlive = false;
httpWebRequest.ContentType = "application/x-www-form-urlencoded";
httpWebRequest.CookieContainer = cookieContainer;
httpWebRequest.ContentLength = postDataByte.Length;
//向服务器传送数据
try
{
inputStream = httpWebRequest.GetRequestStream();
inputStream.Write(postDataByte, 0, postDataByte.Length);
}
//处理异常
catch (Exception ex)
{
throw new Exception("发送POST数据时发生错误!", ex);
}
finally
{
inputStream.Close();
}
//接受服务器返回信息
try
{
httpWebResponse = httpWebRequest.GetResponse() as HttpWebResponse;
outputStream = httpWebResponse.GetResponseStream();
streamReader = new StreamReader(outputStream, encoding);
htmlString = streamReader.ReadToEnd();
}
//处理异常
catch (Exception ex)
{
throw new Exception("接受服务器返回页面时发生错误!", ex);
}
finally
{
streamReader.Close();
}
foreach (Cookie cookie in httpWebResponse.Cookies)
{
cookieContainer.Add(cookie);
}
return htmlString;
}
抓包看你发的HTTP请求到底是什么
.........................好累..代码看不下去!! 自己好好debug调试吧,像楼上的说的..抓包看看!!!!!然后可以搜索一下 什么开心网外挂的资料看看...我都用控制台程序写过 偷菜程序,自己纯属无聊.....