C#如何打开一个JSP登陆页面

zhang_cr 2010-03-26 02:43:16
这个jsp页面是另一个网站的登陆接口。通过post传值并打开这个页面。
这个post传值要由后台代码发送。先要判断他是否已经登陆成功了。在asp.net网站要有Session记录。方便下次直接打开jsp网站的用户中心
...全文
233 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
zhang_cr 2010-03-26
  • 打赏
  • 举报
回复
谢谢 你
zhang_cr 2010-03-26
  • 打赏
  • 举报
回复
行了 刚直接复制你的 你最后写错了 应该是 </script>
cpp2017 2010-03-26
  • 打赏
  • 举报
回复
form的id对么?
zhang_cr 2010-03-26
  • 打赏
  • 举报
回复
到我这怎么不自动提交了呢?
cpp2017 2010-03-26
  • 打赏
  • 举报
回复
向前台
生成一段html
<form id='form2' action='...jsp' methos='post.............</form><script>form2.submit()<script>"
zhang_cr 2010-03-26
  • 打赏
  • 举报
回复
我也是这么想的 但是要怎么做?
cpp2017 2010-03-26
  • 打赏
  • 举报
回复
关键是验证是jsp做的,你可以在.asp中验证么?如果是你本地先验证过后,再构造一个form到客户端,然后自动提交到jsp
zhang_cr 2010-03-26
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 zhang_cr 的回复:]
form提交表单 提交给asp.net页面 由这个页面选判断用户名是否正确如果正确再打开欢迎页面。不能直接提交给jsp页面
[/Quote]
这个登陆状态是要控制的,下次好不用再登陆直接打开对方的欢迎页面
zhang_cr 2010-03-26
  • 打赏
  • 举报
回复
form提交表单 提交给asp.net页面 由这个页面选判断用户名是否正确如果正确再打开欢迎页面。不能直接提交给jsp页面
cpp2017 2010-03-26
  • 打赏
  • 举报
回复
浏览者再打开对方的欢迎页面还是提示登陆
==》浏览者在哪里打开?客户端?那肯定是不行的。因为登录是服务端进行的。

如果你要在客户端实现登录的话,构造一个form将那个jsp中的元素填进去,然后提交。
zhang_cr 2010-03-26
  • 打赏
  • 举报
回复
但是你弄的这个我看不懂!我也用过WebRequest 但是这后台代码运行在服务器,浏览者再打开对方的欢迎页面还是提示登陆
chen_lis 2010-03-26
  • 打赏
  • 举报
回复
1楼 正解
cpp2017 2010-03-26
  • 打赏
  • 举报
回复
1、以前做过n次。
2、代码是copy的,我不可能现在为你写这么长的代码。

zhang_cr 2010-03-26
  • 打赏
  • 举报
回复
这位大哥你是亲自试过还是以前做过?

不会又是从哪copy的代码吧!
cpp2017 2010-03-26
  • 打赏
  • 举报
回复
首先参考以下代码实现模拟登录。
然后将那个cookiecontainer保存到你的session中。


HttpWebRequset
1、通过附加一个cookiecontainer到httprequest对象中,可以得到登录后返回的代表SESSION ID的COOKIE。

2 、将此COOKIE包含在一个cookiecontainer中并附加到另一个HTTPREQUEST请求中,则可以实现SESSION的还原。

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
//using System.Data.OleDb;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Net;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
using Microsoft.Data.Odbc;
namespace PdfTest
{
/// <summary>
/// Summary description for WebForm1.
/// </summary>
public class getHttpInfo : System.Web.UI.Page
{
protected static string cookieheader;


private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here

string strResult;

if (HttpContext.Current.Application["cookieheader"] != null)
{
cookieheader = (string)HttpContext.Current.Application["cookieheader"];
}
else
{
//Login into the website and keep the cookie for the session in the application variable
string strLogin = Login("http://www.thesiteyouwanttovisit/theloginpage.asp", "Action=&USERID=&Password=") ;
}


strResult = getPage("http://www.thesiteyouwanttovisit/theloginpage.asp", "Action=&data=") ;


//Write the result to htm file
FileStream htmFile = new FileStream("c:\save.htm", FileMode.OpenOrCreate);
StreamWriter sw = new StreamWriter(htmFile);
sw.Write(strResult);
sw.Close();
htmFile.Close();

// output the result
Response.Write(strResult);
}


public static string Login(String url, String paramList)
{
HttpWebResponse res = null;
string strResult="";

try
{

HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
req.Method = "POST";
req.ContentType = "application/x-www-form-urlencoded";
req.AllowAutoRedirect = false;
CookieContainer cookieCon = new CookieContainer();
req.CookieContainer = cookieCon;

StringBuilder UrlEncoded = new StringBuilder();
Char[] reserved = {'?', '=', '&'};
byte[] SomeBytes = null;

if (paramList != null)
{
int i=0, j;
while(i<paramList.Length)
{
j=paramList.IndexOfAny(reserved, i);
if (j==-1)
{
UrlEncoded.Append(HttpUtility.UrlEncode(paramList.Substring(i, paramList.Length-i)));
break;
}
UrlEncoded.Append(HttpUtility.UrlEncode(paramList.Substring(i, j-i)));
UrlEncoded.Append(paramList.Substring(j,1));
i = j+1;
}
SomeBytes = Encoding.UTF8.GetBytes(UrlEncoded.ToString());
req.ContentLength = SomeBytes.Length;
Stream newStream = req.GetRequestStream();
newStream.Write(SomeBytes, 0, SomeBytes.Length);
newStream.Close();
}
else
{
req.ContentLength = 0;
}


res = (HttpWebResponse)req.GetResponse();
cookieheader = req.CookieContainer.GetCookieHeader(new Uri(url));
HttpContext.Current.Application.Lock();
HttpContext.Current.Application["cookieheader"] = cookieheader;
HttpContext.Current.Application.UnLock();

Stream ReceiveStream = res.GetResponseStream();
Encoding encode = System.Text.Encoding.GetEncoding("utf-8");
StreamReader sr = new StreamReader( ReceiveStream, encode );
Char[] read = new Char[256];
int count = sr.Read( read, 0, 256 );
while (count > 0)
{
String str = new String(read, 0, count);
strResult += str;
count = sr.Read(read, 0, 256);
}
}
catch(Exception e)
{
strResult = e.ToString();
}
finally
{
if ( res != null )
{
res.Close();
}
}

return strResult;
}


public static string getPage(String url, String paramList)
{
HttpWebResponse res = null;
string strResult = "";

try
{

HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
req.Method = "POST";
req.KeepAlive = true;
req.ContentType = "application/x-www-form-urlencoded";
CookieContainer cookieCon = new CookieContainer();
req.CookieContainer = cookieCon;
req.CookieContainer.SetCookies(new Uri(url),cookieheader);
StringBuilder UrlEncoded = new StringBuilder();
Char[] reserved = {'?', '=', '&'};
byte[] SomeBytes = null;

if (paramList != null)
{
int i=0, j;
while(i<paramList.Length)
{
j=paramList.IndexOfAny(reserved, i);
if (j==-1)
{
UrlEncoded.Append(HttpUtility.UrlEncode(paramList.Substring(i, paramList.Length-i)));
break;
}
UrlEncoded.Append(HttpUtility.UrlEncode(paramList.Substring(i, j-i)));
UrlEncoded.Append(paramList.Substring(j,1));
i = j+1;
}
SomeBytes = Encoding.UTF8.GetBytes(UrlEncoded.ToString());
req.ContentLength = SomeBytes.Length;
Stream newStream = req.GetRequestStream();
newStream.Write(SomeBytes, 0, SomeBytes.Length);
newStream.Close();
}
else
{
req.ContentLength = 0;
}


res = (HttpWebResponse)req.GetResponse();
Stream ReceiveStream = res.GetResponseStream();
Encoding encode = System.Text.Encoding.GetEncoding("utf-8");
StreamReader sr = new StreamReader( ReceiveStream, encode );
Char[] read = new Char[256];
int count = sr.Read( read, 0, 256 );
while (count > 0)
{
String str = new String(read, 0, count);
strResult += str;
count = sr.Read(read, 0, 256);
}
}
catch(Exception e)
{
strResult = e.ToString();
}
finally
{
if ( res != null )
{
res.Close();
}
}

return strResult;
}


#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);

}
#endregion



}
}




62,041

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧