我现在是通过登录信息访问网页。(附原码)
我想直接通过帐号和密码访问,请指教。
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Net;namespace GetUrl
{
public class GetHtmlUrl
{
private static string CookVlaue = FileOpenCookies(); /// <summary>
/// 根据网址返回对应HTML代码
/// </summary>
/// <param name="UriAddress"></param>
/// <returns></returns>
public static string GetUrlTxt(string UriAddress)
{
HttpWebRequest HttpWreq = (HttpWebRequest)WebRequest.Create(UriAddress); //初始化打开网页实例
//HttpWreq.ContentType = "application/x-www-form-urlencoded";
//HttpWreq.ContentType = "application/x-shockwave-flash";
Cookie mycookie = new Cookie();
mycookie.Name = "T3E";
if (CookVlaue == " ")
{
return "没有读取到用户登陆信息";
}
mycookie.Value = CookVlaue;
mycookie.Domain = "s2.travian.cn";
//给mycookie值
CookieContainer mycookiecontainer = new CookieContainer();
mycookiecontainer.Add(mycookie);
HttpWreq.CookieContainer = mycookiecontainer; HttpWreq.Method = "GET";
HttpWreq.AllowAutoRedirect = true;
HttpWreq.KeepAlive = true;
HttpWebResponse HttpWresp = (HttpWebResponse)HttpWreq.GetResponse(); //Console.WriteLine(HttpWresp.ProtocolVersion.ToString());
//返回版本号
Encoding gb2312 = Encoding.GetEncoding("GB2312");
Encoding Utf8 = Encoding.GetEncoding("UTF-8");
Stream myStream = HttpWresp.GetResponseStream();
StreamReader myReader = new StreamReader(myStream, Utf8);
string var = myReader.ReadToEnd(); myReader.Close(); //关闭
HttpWresp.Close();
return var; //返回取回的值
} /// <summary>
/// 读取用户登陆信息,如果成功返回登陆信息,否则返回 "没有读取到用户登陆信息"
/// </summary>
/// <returns></returns>
private static string FileOpenCookies()
{
string RetCookValue = ""; //存放读出来的COOKIE值
try
{
string CookiePath = Environment.GetFolderPath(Environment.SpecialFolder.Cookies);
string username = Environment.UserName;
CookiePath += "\\" + username + "@s2.travian[2].txt";
if (!File.Exists(CookiePath))
{
CookiePath = CookiePath.Replace("[2]", "[1]");
}
string[] cookieValue = File.ReadAllLines(CookiePath, Encoding.ASCII);
if (cookieValue.Length == 9)
{
RetCookValue = cookieValue[1];
RetCookValue = Uri.UnescapeDataString(RetCookValue);
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
return " ";
}
return RetCookValue;
}
}
}
我想直接通过帐号和密码访问,请指教。
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Net;namespace GetUrl
{
public class GetHtmlUrl
{
private static string CookVlaue = FileOpenCookies(); /// <summary>
/// 根据网址返回对应HTML代码
/// </summary>
/// <param name="UriAddress"></param>
/// <returns></returns>
public static string GetUrlTxt(string UriAddress)
{
HttpWebRequest HttpWreq = (HttpWebRequest)WebRequest.Create(UriAddress); //初始化打开网页实例
//HttpWreq.ContentType = "application/x-www-form-urlencoded";
//HttpWreq.ContentType = "application/x-shockwave-flash";
Cookie mycookie = new Cookie();
mycookie.Name = "T3E";
if (CookVlaue == " ")
{
return "没有读取到用户登陆信息";
}
mycookie.Value = CookVlaue;
mycookie.Domain = "s2.travian.cn";
//给mycookie值
CookieContainer mycookiecontainer = new CookieContainer();
mycookiecontainer.Add(mycookie);
HttpWreq.CookieContainer = mycookiecontainer; HttpWreq.Method = "GET";
HttpWreq.AllowAutoRedirect = true;
HttpWreq.KeepAlive = true;
HttpWebResponse HttpWresp = (HttpWebResponse)HttpWreq.GetResponse(); //Console.WriteLine(HttpWresp.ProtocolVersion.ToString());
//返回版本号
Encoding gb2312 = Encoding.GetEncoding("GB2312");
Encoding Utf8 = Encoding.GetEncoding("UTF-8");
Stream myStream = HttpWresp.GetResponseStream();
StreamReader myReader = new StreamReader(myStream, Utf8);
string var = myReader.ReadToEnd(); myReader.Close(); //关闭
HttpWresp.Close();
return var; //返回取回的值
} /// <summary>
/// 读取用户登陆信息,如果成功返回登陆信息,否则返回 "没有读取到用户登陆信息"
/// </summary>
/// <returns></returns>
private static string FileOpenCookies()
{
string RetCookValue = ""; //存放读出来的COOKIE值
try
{
string CookiePath = Environment.GetFolderPath(Environment.SpecialFolder.Cookies);
string username = Environment.UserName;
CookiePath += "\\" + username + "@s2.travian[2].txt";
if (!File.Exists(CookiePath))
{
CookiePath = CookiePath.Replace("[2]", "[1]");
}
string[] cookieValue = File.ReadAllLines(CookiePath, Encoding.ASCII);
if (cookieValue.Length == 9)
{
RetCookValue = cookieValue[1];
RetCookValue = Uri.UnescapeDataString(RetCookValue);
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
return " ";
}
return RetCookValue;
}
}
}
*****************************************************************************
欢迎使用CSDN论坛专用阅读器 : CSDN Reader(附全部源代码) http://feiyun0112.cnblogs.com/
我现在的程序是可以访问相关的登录信息,但是都是ie的登录信息(ie自动生成的信息文件),我想直接用帐号和密码登录。谁知道怎么登录?
string username = Environment.UserName;
这儿能得到预期值吗?
如果不能,请参考:http://blog.csdn.net/longren629/archive/2008/11/28/3400351.aspx
这个关键,看能否得到本地用户名
否则像我一样用activex顺便帮忙看看,我遇到的问题。
http://topic.csdn.net/u/20081231/12/3d0afdc3-8920-4347-80de-24d290f347eb.html
可以获取到预定的登录信息。我现在是通过这些登录信息来访问网站。
我想改为帐号和密码访问,请指教。
你应该用post方式请求 即:
HttpWreq.Method = "POST";
然后加上post参数:
至于代码我就不贴了
具体方法你可以在百度里搜一下 httpwebrequest post 会出来一堆代码的。
你可以去看一下这个网站里关于ASP.NET资料学习
http://www.singletowm.com
最好能够提供原码..谢谢
我访问的量太大了。
最高会到40万次左右,如果使用fiddler的话,速度能跟上吗?