winform下登陆web服务器(C#)
我想在winform中登陆web页面假如我有个登陆页面接受用户名和密码参数实现登陆到系统.我想在winform下直接登陆到web不用打开浏览器.这样可以吗希望高手指教下用...
我想在winform中登陆web页面
假如我有个登陆页面接受用户名和密码参数实现登陆到系统.我想在winform下直接登陆到web 不用打开浏览器.
这样可以吗 希望高手指教下
用的是c#语言开发工具vs205 希望最好能贴上代码.谢谢 展开
假如我有个登陆页面接受用户名和密码参数实现登陆到系统.我想在winform下直接登陆到web 不用打开浏览器.
这样可以吗 希望高手指教下
用的是c#语言开发工具vs205 希望最好能贴上代码.谢谢 展开
展开全部
using System;
using System.Net;
using System.IO;
using System.Text;
class Test
{
[STAThread]
static void Main(string[] args)
{
//
// TODO: 在此处添加代码以启动应用程序
//
string url = "http://localhost/csdn2/1.asp";
// <%
// if request("aa")="zhuye" then session("ok")="ok"
// if session("ok")="ok" then
// response.write("登录")
// else
// response.write("没有登录")
// end if
// %>
string indata = "aa=zhuye";
string outdata = "";
CookieContainer myCookieContainer = new CookieContainer();
//新建一个CookieContainer来存放Cookie集合
HttpWebRequest myHttpWebRequest = (HttpWebRequest) WebRequest.Create(url);
//新建一个HttpWebRequest
myHttpWebRequest.ContentType = "application/x-www-form-urlencoded";
myHttpWebRequest.ContentLength = indata.Length;
myHttpWebRequest.Method = "POST";
myHttpWebRequest.CookieContainer = myCookieContainer;
//设置HttpWebRequest的CookieContainer为刚才建立的那个myCookieContainer
Stream myRequestStream = myHttpWebRequest.GetRequestStream();
StreamWriter myStreamWriter = new StreamWriter(myRequestStream, Encoding.GetEncoding("gb2312"));
myStreamWriter.Write(indata);
//把数据写入HttpWebRequest的Request流
myStreamWriter.Close();
myRequestStream.Close();
//关闭打开对象
HttpWebResponse myHttpWebResponse = (HttpWebResponse) myHttpWebRequest.GetResponse();
//新建一个HttpWebResponse
myHttpWebResponse.Cookies = myCookieContainer.GetCookies(myHttpWebRequest.RequestUri);
//获取一个包含url的Cookie集合的CookieCollection
Stream myResponseStream = myHttpWebResponse.GetResponseStream();
StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("gb2312"));
outdata = myStreamReader.ReadToEnd();
//把数据从HttpWebResponse的Response流中读出
myStreamReader.Close();
myResponseStream.Close();
Console.WriteLine(outdata);
//显示"登录"
//拿到了Cookie,再进行请求就能直接读取到登录后的内容了
myHttpWebRequest = (HttpWebRequest) WebRequest.Create(url);
myHttpWebRequest.CookieContainer = myCookieContainer;//*
//刚才那个CookieContainer已经存有了Cookie,把它附加到HttpWebRequest中则能直接通过验证
myHttpWebResponse = (HttpWebResponse) myHttpWebRequest.GetResponse();
myHttpWebResponse.Cookies = myCookieContainer.GetCookies(myHttpWebRequest.RequestUri);
myResponseStream = myHttpWebResponse.GetResponseStream();
myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("gb2312"));
outdata = myStreamReader.ReadToEnd();
myStreamReader.Close();
myResponseStream.Close();
Console.WriteLine(outdata);
//再次显示"登录"
//如果把*行注释调,就显示"没有登录"
}
}
注释写得很明确了,不多罗嗦了。
补充:如果是以get方式登录的话,直接修改url就可以了,indate可以不写任何东西。(不要去修改myHttpWebRequest.Method为GET),比如把asp文件修改为if request.querystring("aa")="zhuye" then session("ok")="ok",只要修改url为string url="http://localhost/csdn2/1.asp?aa=zhuye";即可。
using System.Net;
using System.IO;
using System.Text;
class Test
{
[STAThread]
static void Main(string[] args)
{
//
// TODO: 在此处添加代码以启动应用程序
//
string url = "http://localhost/csdn2/1.asp";
// <%
// if request("aa")="zhuye" then session("ok")="ok"
// if session("ok")="ok" then
// response.write("登录")
// else
// response.write("没有登录")
// end if
// %>
string indata = "aa=zhuye";
string outdata = "";
CookieContainer myCookieContainer = new CookieContainer();
//新建一个CookieContainer来存放Cookie集合
HttpWebRequest myHttpWebRequest = (HttpWebRequest) WebRequest.Create(url);
//新建一个HttpWebRequest
myHttpWebRequest.ContentType = "application/x-www-form-urlencoded";
myHttpWebRequest.ContentLength = indata.Length;
myHttpWebRequest.Method = "POST";
myHttpWebRequest.CookieContainer = myCookieContainer;
//设置HttpWebRequest的CookieContainer为刚才建立的那个myCookieContainer
Stream myRequestStream = myHttpWebRequest.GetRequestStream();
StreamWriter myStreamWriter = new StreamWriter(myRequestStream, Encoding.GetEncoding("gb2312"));
myStreamWriter.Write(indata);
//把数据写入HttpWebRequest的Request流
myStreamWriter.Close();
myRequestStream.Close();
//关闭打开对象
HttpWebResponse myHttpWebResponse = (HttpWebResponse) myHttpWebRequest.GetResponse();
//新建一个HttpWebResponse
myHttpWebResponse.Cookies = myCookieContainer.GetCookies(myHttpWebRequest.RequestUri);
//获取一个包含url的Cookie集合的CookieCollection
Stream myResponseStream = myHttpWebResponse.GetResponseStream();
StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("gb2312"));
outdata = myStreamReader.ReadToEnd();
//把数据从HttpWebResponse的Response流中读出
myStreamReader.Close();
myResponseStream.Close();
Console.WriteLine(outdata);
//显示"登录"
//拿到了Cookie,再进行请求就能直接读取到登录后的内容了
myHttpWebRequest = (HttpWebRequest) WebRequest.Create(url);
myHttpWebRequest.CookieContainer = myCookieContainer;//*
//刚才那个CookieContainer已经存有了Cookie,把它附加到HttpWebRequest中则能直接通过验证
myHttpWebResponse = (HttpWebResponse) myHttpWebRequest.GetResponse();
myHttpWebResponse.Cookies = myCookieContainer.GetCookies(myHttpWebRequest.RequestUri);
myResponseStream = myHttpWebResponse.GetResponseStream();
myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("gb2312"));
outdata = myStreamReader.ReadToEnd();
myStreamReader.Close();
myResponseStream.Close();
Console.WriteLine(outdata);
//再次显示"登录"
//如果把*行注释调,就显示"没有登录"
}
}
注释写得很明确了,不多罗嗦了。
补充:如果是以get方式登录的话,直接修改url就可以了,indate可以不写任何东西。(不要去修改myHttpWebRequest.Method为GET),比如把asp文件修改为if request.querystring("aa")="zhuye" then session("ok")="ok",只要修改url为string url="http://localhost/csdn2/1.asp?aa=zhuye";即可。
参考资料: http://www.chenjiliang.com/Article/View.aspx?ArticleID=1291&TypeID=79
展开全部
如果web服务代码也是你写的..那就很好办.
写一个web service
如果不是你的..那...
写一个web service
如果不是你的..那...
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
可以使用WebBrowser
看下面这个例子
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
namespace AdaEniac
{
[System.Runtime.InteropServices.ComVisibleAttribute(true)]
public partial class frmNavigation : Form
{
public frmNavigation()
{
InitializeComponent();
}
private void frmNavigation_Load(object sender, EventArgs e)
{
try
{
this.Cursor = Cursors.WaitCursor;
//定义网页地址
FileInfo fileNavigation = new FileInfo("Web/Notice.htm");
// WebBrowser控件显示的网页路径
webNavigation.Url = new Uri(fileNavigation.FullName);
// 将当前类设置为可由脚本访问
webNavigation.ObjectForScripting = this;
//webNavigation 是WebBrowser控件,在窗体设计工具里有该控件。
WriteNoticeWeb();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
this.Cursor = Cursors.Default;
}
}
private void WriteNoticeWeb()
{
string strPath = @"Web/Notice.htm";
string strTitle = "";
try
{
this.Cursor = Cursors.WaitCursor;
int i = 5;
if (i > 0)
{
strTitle = "测试信息";
}
FileStream myStream = new FileStream(strPath, FileMode.Create, FileAccess.Write);
StreamWriter myWriter = new StreamWriter(myStream, Encoding.Unicode);
myWriter.WriteLine("<html>");
myWriter.WriteLine("<head>");
myWriter.WriteLine("<title>公司最新公告</title>");
myWriter.WriteLine("<meta http-equiv=\"Content-Language\" content=\"zh-cn\">");
myWriter.WriteLine("<link rel=\"stylesheet\" href=\"default.css\" type=\"text/css\" />");
myWriter.WriteLine("<script language=\"javascript\">");
myWriter.WriteLine("function ShowMenuForm(eFormText)");
myWriter.WriteLine("{");
myWriter.WriteLine(" window.external.ShowMenuForm(eFormText);");
myWriter.WriteLine("}");
myWriter.WriteLine("</script>");
myWriter.WriteLine("</head>");
myWriter.WriteLine("<body oncontextmenu=\"return false\">");
myWriter.WriteLine("<marquee>");
if (i <=0 )
{
myWriter.WriteLine(""); //没有新公告
}
else
{
myWriter.WriteLine("<a href=\"#\" onClick=\"ShowMenuForm('公告栏')\">您有"+i.ToString()+"条未读新公告,新公告标题:" + strTitle + "</a>");
}
myWriter.WriteLine("</marquee>");
myWriter.WriteLine("</body>");
myWriter.WriteLine("</html>");
myWriter.Close();
myStream.Close();
//公告页面
FileInfo fileNotice = new FileInfo("Web/Notice.htm");
webNotice.Url = new Uri(fileNotice.FullName);
webNotice.ObjectForScripting = this;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
this.Cursor = Cursors.Default;
}
}
}
}
看下面这个例子
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
namespace AdaEniac
{
[System.Runtime.InteropServices.ComVisibleAttribute(true)]
public partial class frmNavigation : Form
{
public frmNavigation()
{
InitializeComponent();
}
private void frmNavigation_Load(object sender, EventArgs e)
{
try
{
this.Cursor = Cursors.WaitCursor;
//定义网页地址
FileInfo fileNavigation = new FileInfo("Web/Notice.htm");
// WebBrowser控件显示的网页路径
webNavigation.Url = new Uri(fileNavigation.FullName);
// 将当前类设置为可由脚本访问
webNavigation.ObjectForScripting = this;
//webNavigation 是WebBrowser控件,在窗体设计工具里有该控件。
WriteNoticeWeb();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
this.Cursor = Cursors.Default;
}
}
private void WriteNoticeWeb()
{
string strPath = @"Web/Notice.htm";
string strTitle = "";
try
{
this.Cursor = Cursors.WaitCursor;
int i = 5;
if (i > 0)
{
strTitle = "测试信息";
}
FileStream myStream = new FileStream(strPath, FileMode.Create, FileAccess.Write);
StreamWriter myWriter = new StreamWriter(myStream, Encoding.Unicode);
myWriter.WriteLine("<html>");
myWriter.WriteLine("<head>");
myWriter.WriteLine("<title>公司最新公告</title>");
myWriter.WriteLine("<meta http-equiv=\"Content-Language\" content=\"zh-cn\">");
myWriter.WriteLine("<link rel=\"stylesheet\" href=\"default.css\" type=\"text/css\" />");
myWriter.WriteLine("<script language=\"javascript\">");
myWriter.WriteLine("function ShowMenuForm(eFormText)");
myWriter.WriteLine("{");
myWriter.WriteLine(" window.external.ShowMenuForm(eFormText);");
myWriter.WriteLine("}");
myWriter.WriteLine("</script>");
myWriter.WriteLine("</head>");
myWriter.WriteLine("<body oncontextmenu=\"return false\">");
myWriter.WriteLine("<marquee>");
if (i <=0 )
{
myWriter.WriteLine(""); //没有新公告
}
else
{
myWriter.WriteLine("<a href=\"#\" onClick=\"ShowMenuForm('公告栏')\">您有"+i.ToString()+"条未读新公告,新公告标题:" + strTitle + "</a>");
}
myWriter.WriteLine("</marquee>");
myWriter.WriteLine("</body>");
myWriter.WriteLine("</html>");
myWriter.Close();
myStream.Close();
//公告页面
FileInfo fileNotice = new FileInfo("Web/Notice.htm");
webNotice.Url = new Uri(fileNotice.FullName);
webNotice.ObjectForScripting = this;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
this.Cursor = Cursors.Default;
}
}
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
可以在一个winfrom窗体加一个webBrowser空间,
应以浏览网页,
后台代码写
private void GoToItem(string address)
{
if (address == null) return;
if (currentAddress != address)
{
webBrowser1.Navigate(address);
}
}
void InitHomePage()
{
GoToItem("网址");
}
其他有关登陆判断都在web页面写,
我的意思说我们这个winfrom就是个浏览器---自己作的浏览器!
应以浏览网页,
后台代码写
private void GoToItem(string address)
{
if (address == null) return;
if (currentAddress != address)
{
webBrowser1.Navigate(address);
}
}
void InitHomePage()
{
GoToItem("网址");
}
其他有关登陆判断都在web页面写,
我的意思说我们这个winfrom就是个浏览器---自己作的浏览器!
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
using System;
using System.Net;
using System.IO;
using System.Text;
class Test
{
[STAThread]
static void Main(string[] args)
{
//
// TODO: 在此处添加代码以启动应用程序
//
string url = "http://localhost/csdn2/1.asp";
// <%
// if request("aa")="zhuye" then session("ok")="ok"
// if session("ok")="ok" then
// response.write("登录")
// else
// response.write("没有登录")
// end if
// %>
string indata = "aa=zhuye";
string outdata = "";
CookieContainer myCookieContainer = new CookieContainer();
//新建一个CookieContainer来存放Cookie集合
HttpWebRequest myHttpWebRequest = (HttpWebRequest) WebRequest.Create(url);
//新建一个HttpWebRequest
myHttpWebRequest.ContentType = "application/x-www-form-urlencoded";
myHttpWebRequest.ContentLength = indata.Length;
myHttpWebRequest.Method = "POST";
myHttpWebRequest.CookieContainer = myCookieContainer;
//设置HttpWebRequest的CookieContainer为刚才建立的那个myCookieContainer
Stream myRequestStream = myHttpWebRequest.GetRequestStream();
StreamWriter myStreamWriter = new StreamWriter(myRequestStream, Encoding.GetEncoding("gb2312"));
myStreamWriter.Write(indata);
//把数据写入HttpWebRequest的Request流
myStreamWriter.Close();
myRequestStream.Close();
//关闭打开对象
HttpWebResponse myHttpWebResponse = (HttpWebResponse) myHttpWebRequest.GetResponse();
//新建一个HttpWebResponse
myHttpWebResponse.Cookies = myCookieContainer.GetCookies(myHttpWebRequest.RequestUri);
//获取一个包含url的Cookie集合的CookieCollection
Stream myResponseStream = myHttpWebResponse.GetResponseStream();
StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("gb2312"));
outdata = myStreamReader.ReadToEnd();
//把数据从HttpWebResponse的Response流中读出
myStreamReader.Close();
myResponseStream.Close();
Console.WriteLine(outdata);
//显示"登录"
//拿到了Cookie,再进行请求就能直接读取到登录后的内容了
myHttpWebRequest = (HttpWebRequest) WebRequest.Create(url);
myHttpWebRequest.CookieContainer = myCookieContainer;//*
//刚才那个CookieContainer已经存有了Cookie,把它附加到HttpWebRequest中则能直接通过验证
myHttpWebResponse = (HttpWebResponse) myHttpWebRequest.GetResponse();
myHttpWebResponse.Cookies = myCookieContainer.GetCookies(myHttpWebRequest.RequestUri);
myResponseStream = myHttpWebResponse.GetResponseStream();
myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("gb2312"));
outdata = myStreamReader.ReadToEnd();
myStreamReader.Close();
myResponseStream.Close();
Console.WriteLine(outdata);
//再次显示"登录"
//如果把*行注释调,就显示"没有登录"
}
}
注释写得很明确了,不多罗嗦了。
补充:如果是以get方式登录的话,直接修改url就可以了,indate可以不写任何东西。(不要去修改myHttpWebRequest.Method为GET),比如把asp文件修改为if request.querystring("aa")="zhuye" then session("ok")="ok",只要修改url为string url="http://localhost/csdn2/1.asp?aa=zhuye";即可。
参考资料:http://www.chenjiliang.com/Article/View.aspx?ArticleID=1291&TypeID=79
using System.Net;
using System.IO;
using System.Text;
class Test
{
[STAThread]
static void Main(string[] args)
{
//
// TODO: 在此处添加代码以启动应用程序
//
string url = "http://localhost/csdn2/1.asp";
// <%
// if request("aa")="zhuye" then session("ok")="ok"
// if session("ok")="ok" then
// response.write("登录")
// else
// response.write("没有登录")
// end if
// %>
string indata = "aa=zhuye";
string outdata = "";
CookieContainer myCookieContainer = new CookieContainer();
//新建一个CookieContainer来存放Cookie集合
HttpWebRequest myHttpWebRequest = (HttpWebRequest) WebRequest.Create(url);
//新建一个HttpWebRequest
myHttpWebRequest.ContentType = "application/x-www-form-urlencoded";
myHttpWebRequest.ContentLength = indata.Length;
myHttpWebRequest.Method = "POST";
myHttpWebRequest.CookieContainer = myCookieContainer;
//设置HttpWebRequest的CookieContainer为刚才建立的那个myCookieContainer
Stream myRequestStream = myHttpWebRequest.GetRequestStream();
StreamWriter myStreamWriter = new StreamWriter(myRequestStream, Encoding.GetEncoding("gb2312"));
myStreamWriter.Write(indata);
//把数据写入HttpWebRequest的Request流
myStreamWriter.Close();
myRequestStream.Close();
//关闭打开对象
HttpWebResponse myHttpWebResponse = (HttpWebResponse) myHttpWebRequest.GetResponse();
//新建一个HttpWebResponse
myHttpWebResponse.Cookies = myCookieContainer.GetCookies(myHttpWebRequest.RequestUri);
//获取一个包含url的Cookie集合的CookieCollection
Stream myResponseStream = myHttpWebResponse.GetResponseStream();
StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("gb2312"));
outdata = myStreamReader.ReadToEnd();
//把数据从HttpWebResponse的Response流中读出
myStreamReader.Close();
myResponseStream.Close();
Console.WriteLine(outdata);
//显示"登录"
//拿到了Cookie,再进行请求就能直接读取到登录后的内容了
myHttpWebRequest = (HttpWebRequest) WebRequest.Create(url);
myHttpWebRequest.CookieContainer = myCookieContainer;//*
//刚才那个CookieContainer已经存有了Cookie,把它附加到HttpWebRequest中则能直接通过验证
myHttpWebResponse = (HttpWebResponse) myHttpWebRequest.GetResponse();
myHttpWebResponse.Cookies = myCookieContainer.GetCookies(myHttpWebRequest.RequestUri);
myResponseStream = myHttpWebResponse.GetResponseStream();
myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("gb2312"));
outdata = myStreamReader.ReadToEnd();
myStreamReader.Close();
myResponseStream.Close();
Console.WriteLine(outdata);
//再次显示"登录"
//如果把*行注释调,就显示"没有登录"
}
}
注释写得很明确了,不多罗嗦了。
补充:如果是以get方式登录的话,直接修改url就可以了,indate可以不写任何东西。(不要去修改myHttpWebRequest.Method为GET),比如把asp文件修改为if request.querystring("aa")="zhuye" then session("ok")="ok",只要修改url为string url="http://localhost/csdn2/1.asp?aa=zhuye";即可。
参考资料:http://www.chenjiliang.com/Article/View.aspx?ArticleID=1291&TypeID=79
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
可以哦,不过不知道你想达到什么目的~
最简单的方法就是,将用户名和密码提交到网站的数据库于验证!
当然也可以用模仿网页post数据
最简单的方法就是,将用户名和密码提交到网站的数据库于验证!
当然也可以用模仿网页post数据
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询