【求助】关于HttpWebRequest相关的一段代码,麻烦帮忙看下
string url = "http://my.b2b.hc360.com/my/turbine/template/corcenter,company,main.html&renew=true";
string param="LoginID=stxszp&password=1qazxsw2&logincode=";
public virtual string HttpPost(string url, string param)
{
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.Accept = "*/*";
request.Timeout = 15000;
request.AllowAutoRedirect = true;
StreamWriter requestStream = null;
WebResponse response = null;
string responseStr = null;
try
{
requestStream = new StreamWriter(request.GetRequestStream());
requestStream.Write(param);
requestStream.Close();
response = request.GetResponse();
if (response != null)
{
StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.Default);
responseStr = reader.ReadToEnd();
reader.Close();
}
}
catch (Exception)
{
throw;
}
finally
{
request = null;
requestStream = null;
response = null;
}
return responseStr;
}
这段代码是登陆一个无验证页面的,可是每次返回的结果都还是这个登陆页面的源码。为什么
我想在WINFORM上实现登陆这个网站,保留cookie然后进入另一个信息重发页面提交一个表单
我们公司是做这个网站的服务的,有N多个会员,每天都要挨个登陆重发一下信息,工作量很大。
非常感谢! HttpWebRequest C# --------------------编程问答-------------------- 你没保存登陆的cookies CookieContainer
http://blog.csdn.net/smeller/article/details/6895065 --------------------编程问答--------------------
public string Login(string _u,string _p)
{
try
{
string postData = "LoginID=stxszp&password=1qazxsw2&logincode=";
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://my.b2b.hc360.com/my/turbine/template/corcenter,company,main.html&renew=true");
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.Timeout = 10000;
request.ContentLength = Encoding.UTF8.GetByteCount(postData);
request.AllowAutoRedirect = false;
Stream myRequestStream = request.GetRequestStream();
StreamWriter myStreamWriter = new StreamWriter(myRequestStream, Encoding.GetEncoding("gb2312"));
myStreamWriter.Write(postData);
myStreamWriter.Close();
myRequestStream.Close();
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
if (response.StatusCode == HttpStatusCode.Found)
{
return response.Headers["Set-Cookie"];
}else{
return "";
}
}
catch {
return "";
}
}
在没有验证码情况下登录,登录如果返回不为空,那么你获取JSESSIONID=******; 这段返回的Cookie,以后操作时候在request.Headers.Add("Cookie","JSESSIONID=****;")就可以正常操作了 --------------------编程问答-------------------- 我自己模仿他的登陆表单写了个表单在静态页面上 是可以正常登陆的 没有验证码 可是这段代码返回的页面源码显示出来却还是这个登陆页面 而且还多了验证码 这是为什么呢 在返回的数据里我也找不到JSESSIONID --------------------编程问答--------------------
第一,限制了你的IP,会出现验证码。如果不出现验证码,你还返回了登录页源码,把request.AllowAutoRedirect = true;改成false 不要自动跳转,你因你没有处理Cookie问题,自动跳转的话就清空Cookie,又返回首页了 --------------------编程问答--------------------
你这个代码我试了,最终返回的还是登陆页面,也就是没有登陆成功,获取不到Cookie 访问站内其他页面返回的还是登陆页面 而且在浏览器上打开登陆页面是没有验证码的 返回的这个数据在VS里用HTML可视化工具显示出来的时候还多了验证码这一块 --------------------编程问答-------------------- 帮帮忙撒大哥们 我这个真的不会用啊 急死了 --------------------编程问答-------------------- 用webbrowser简单些 --------------------编程问答-------------------- 你是没懂我的意思吧
而且就算是用webbrowser 执行点击操作 进入下个页面的时候会弹出新的网页 然后就无法控制了啊。
不知道是不是我不知道 至少我知道的事这样的 --------------------编程问答--------------------
public string Login(string postData, string url)
{
try
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.Timeout = 10000;
request.ContentLength = Encoding.UTF8.GetByteCount(postData);
request.AllowAutoRedirect = false;
Stream myRequestStream = request.GetRequestStream();
StreamWriter myStreamWriter = new StreamWriter(myRequestStream, Encoding.GetEncoding("gb2312"));
myStreamWriter.Write(postData);
myStreamWriter.Close();
myRequestStream.Close();
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
if (response.StatusCode == HttpStatusCode.Found)
{
return response.Headers["Set-Cookie"];
}
else
{
return "";
}
}
catch
{
return "";
}
}
private void btnOK_Click(object sender, EventArgs e)
{
string postData = "LoginID=stxszp&password=1qazxsw2&logincode=";
string url = "http://my.b2b.hc360.com/my/turbine/template/corcenter,company,main.html&renew=true";
string cookie=Login(postData,url);
url = "http://my.b2b.hc360.com/my/turbine/template/corcenter,business,businpas.html?querysorttag=0";
postData = "";
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Headers.Add("Cookie", cookie);
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.Timeout = 10000;
request.ContentLength = Encoding.UTF8.GetByteCount(postData);
request.AllowAutoRedirect = false;
Stream myRequestStream = request.GetRequestStream();
StreamWriter myStreamWriter = new StreamWriter(myRequestStream, Encoding.GetEncoding("gb2312"));
myStreamWriter.Write(postData);
myStreamWriter.Close();
myRequestStream.Close();
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.Default);
string endStr = reader.ReadToEnd();
reader.Close();
}
按你说的做了 是得到JSESSIONID了 可是访问内页的时候还是不成功 AllowAutoRedirect = false的话就返回空值了
麻烦你了 再帮我看下吧 这块真的不懂
非常感谢
补充:.NET技术 , C#