登录连接: http://www.metin2.org/user/login/index.php用软件侦测 浏览器提交 username=Ncapoemchl&password=cdk890901&SubmitLoginForm=Gönder请问WebClient 代码应该如何写?
解决方案 »
- Convert.ToBase64String(Encoding.Unicode.GetBytes(userName))
- 窗体gridview里数据怎么放dataset里
- c# SerialPort问题,请教,急!!急!!急 !!!
- excel 日期格式导入datagridview 问题
- 有关反射struct方法的一点问题!
- c# winform 支持透明度的自定义panel,当拖动改变大小的时候怎样取消闪烁?控件代码如下:
- 到底应该怎么样可视化地对web应用的datagrid进行数据绑定
- 加急!!!!string型怎么转换为int型呀???
- 急急急 C# 启动外部EXE,指定工作路径,调试可以用.打包软件后,EXE无法启动
- 请教一个P/Invoke的问题
- winform状态栏statusStrip分栏问题
- 二维数组很基础的问题,不解
System.Net.HttpWebRequest req=(System.Net.HttpWebRequest)System.Net.HttpWebRequest.Create(url);
req.Method= "POST ";//POST方式请求
req.ContentType= "application/x-www-form-urlencoded ";//内容类型
string para;
para=System.Web.HttpUtility.UrlEncode( "userid ") + "= "+System.Web.HttpUtility.UrlEncode("");
para+= "& ";
para+=System.Web.HttpUtility.UrlEncode( "passwd ")+ "= "+System.Web.HttpUtility.UrlEncode( "");
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Web;namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
string url = "http://www.metin2.org/user/login/index.php";
HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(url);
req.Method = "POST ";//POST方式请求
req.ContentType = "application/x-www-form-urlencoded ";//内容类型
string para;
para = HttpUtility.UrlEncode("userid ") + "= " + HttpUtility.UrlEncode("");
//这里为什么会报上下文不存在名称 HttpUtility ?
para += "& ";
para += HttpUtility.UrlEncode("passwd ") + "= " + HttpUtility.UrlEncode("");
} }
}
using System.Web;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Web;
我已经加了啊,我也很奇怪为什么这里会报错。
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.IO;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} private void button1_Click(object sender, EventArgs e)
{
string Txt = "";
string UserName = "Ncapoemchl";
string UserPwd = "cdk890901";
String LoginUrl = "http://www.metin2.org/user/login/index.php";
Login(UserName, UserPwd, LoginUrl, out Txt); //获取登录后的页面数据
textBox2.Text = Txt; //判断该页面数据是否是登陆后的数据就可以判断是否登录成功了
//由于都是字符串操作就没有必要写了呵呵
} public void Login(string UserName, string UserPwd, string LoginUrl, out string Txt)
{
try
{
//定义Cookie容器
CookieContainer CookieArray = new CookieContainer(); //创建Http请求
HttpWebRequest LoginHttpWebRequest = (HttpWebRequest)WebRequest.Create(LoginUrl); //登录数据
string LoginData = "username=" + UserName + "&password=" + UserPwd + "&SubmitLoginForm=Gönder";
//数据被传输类型
LoginHttpWebRequest.ContentType = "application/x-www-form-urlencoded";
//数据长度
LoginHttpWebRequest.ContentLength = LoginData.Length;
//数据传输方法 get或post
LoginHttpWebRequest.Method = "POST";
//设置HttpWebRequest的CookieContainer为刚才建立的那个CookieArray
LoginHttpWebRequest.CookieContainer = CookieArray;
//获取登录数据流
Stream myRequestStream = LoginHttpWebRequest.GetRequestStream();
//StreamWriter
StreamWriter myStreamWriter = new StreamWriter(myRequestStream, Encoding.Default);
//把数据写入HttpWebRequest的Request流
myStreamWriter.Write(LoginData);
//关闭打开对象
myStreamWriter.Close(); myRequestStream.Close(); //新建一个HttpWebResponse
HttpWebResponse myHttpWebResponse = (HttpWebResponse)LoginHttpWebRequest.GetResponse(); //获取一个包含url的Cookie集合的CookieCollection
myHttpWebResponse.Cookies = CookieArray.GetCookies(LoginHttpWebRequest.RequestUri); WebHeaderCollection a = myHttpWebResponse.Headers; Stream myResponseStream = myHttpWebResponse.GetResponseStream(); StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.Default); Txt = myStreamReader.ReadToEnd();
//把数据从HttpWebResponse的Response流中读出
myStreamReader.Close(); myResponseStream.Close();
}
catch (Exception ex)
{
throw ex;
}
} }
}