我有一个旧的VB6应用程序-我需要通过Web浏览器将其外壳到ASP.net站点。我打开了浏览器并成功调用了ASP网站。我需要VB6应用程序才能知道何时关闭Web浏览器会话。 Web浏览器会话打开时,需要禁用VB应用程序表单(或“保存”按钮)。 (我不想使用Windows进程的进程ID进行检查。)
我的想法是:
一些建议会很好。非常感谢。
答案 0 :(得分:0)
公司内部申请
台式机应用(VB6)
使用参数(计划ID等)输入Asp.Net网站时,我们将为“会话ID”发送一个额外的参数。
在VB6应用中随机生成一个会话ID [随机:24个字符长,小写,数字介于0-5之间],这复制了Asp.Net框架将如何为其生成会话ID的方式HTML和C#之间的通信。然后,我们将使用我们自己随机生成的会话ID覆盖Asp.Net给我们的Asp.Net会话ID。
检查是否在VB6中设置了ASP.Net会话变量:
Private Sub Command1_Click()
Dim objHTTP As Object
Dim Json As String
Dim result As String
' === Check if Browser Session is open ===
Set objHTTP = CreateObject("MSXML2.ServerXMLHTTP")
url = "http://dub-iisdev/SessionTest/test.aspx/IsWindowOpen"
objHTTP.open "POST", url, False
objHTTP.setRequestHeader("cookie") = "ASP.NET_SessionId=" + txtCookie.Text ' Required twice
objHTTP.setRequestHeader("cookie") = "ASP.NET_SessionId=" + txtCookie.Text ' Required twice
objHTTP.setRequestHeader "Content-type", "application/json"
objHTTP.send (Json)
result = objHTTP.responseText
txtOutput.Text = result
Set objHTTP = Nothing
End Sub
ASP.Net
我们在这里需要一些管道: 设置ASP.Net会话ID的一种小方法 一个小的aspx关闭页面,当我们离开浏览器时将运行一些代码,并且 现有页面中还有一些额外的C#方法和JavaScript。
1。设置会话ID:
将变量从旧会话复制到新位置
protected void ReGenerateSessionId(string newsessionID)
{
SessionIDManager manager = new SessionIDManager();
string oldId = manager.GetSessionID(Context);
string newId = manager.CreateSessionID(Context);
bool isAdd = false, isRedir = false;
manager.RemoveSessionID(Context);
manager.SaveSessionID(Context, newsessionID, out isRedir, out isAdd);
HttpApplication ctx = (HttpApplication)HttpContext.Current.ApplicationInstance;
HttpModuleCollection mods = ctx.Modules;
System.Web.SessionState.SessionStateModule ssm = (SessionStateModule)mods.Get("Session");
System.Reflection.FieldInfo[] fields = ssm.GetType().GetFields(BindingFlags.NonPublic | BindingFlags.Instance);
SessionStateStoreProviderBase store = null;
System.Reflection.FieldInfo rqIdField = null, rqLockIdField = null, rqStateNotFoundField = null;
SessionStateStoreData rqItem = null;
foreach (System.Reflection.FieldInfo field in fields)
{
if (field.Name.Equals("_store")) store = (SessionStateStoreProviderBase)field.GetValue(ssm);
if (field.Name.Equals("_rqId")) rqIdField = field;
if (field.Name.Equals("_rqLockId")) rqLockIdField = field;
if (field.Name.Equals("_rqSessionStateNotFound")) rqStateNotFoundField = field;
if ((field.Name.Equals("_rqItem")))
{
rqItem = (SessionStateStoreData)field.GetValue(ssm);
}
}
object lockId = rqLockIdField.GetValue(ssm);
if ((lockId != null) && (oldId != null))
{
store.RemoveItem(Context, oldId, lockId, rqItem);
}
rqStateNotFoundField.SetValue(ssm, true);
rqIdField.SetValue(ssm, newsessionID);
}
您的第一个登录页面将使用上面的ReGenerateSessionId类设置发送到服务器的新会话ID(来自VB6)参数。
此代码执行完Asp.Net的实例后,我们的VB6实例将具有用于HTTP通信的相同会话ID
protected void Page_Load(object sender, EventArgs e)
{
// Simulate Session ID coming in from VB6...
string sessionId;
Random rnd = new Random();
sessionId = "asdfghjklqwertyuiop12345" [24 char - a to z (small) & 0-5]
// Set new session variable and copy variable from old session to new location
ReGenerateSessionId(sessionId);
// Put something into the session
HttpContext.Current.Session["SOME_SESSION_VARIABLE_NAME"] = "Consider it done!";
}
2。打开\关闭浏览器
其中一个ASP.Net页面需要具有三个新的WebMethod:SetOpenWindow,SetClosingWindow和IsWindowOpen
打开浏览器:
C#。 SetOpenWindow: 这将通过.ready JavaScript在您的第一个(或任何必需的)HTML页面中调用。页面加载后,JavaScript会简单地向SetOpwnWindow web方法触发Ajax调用。该方法会将会话变量WINDOW_STATUS设置为OPEN。
[WebMethod()]
[ScriptMethod(UseHttpGet = false)]
public static string SetOpenWindow()
{
HttpContext.Current.Session["WINDOW_STATUS"] = "OPEN";
return "Status:" + HttpContext.Current.Session["WINDOW_STATUS"];
}
ASPX。页面加载后,从Ajax调用SetOpenWindow。这会将WINDOW_STATUS设置为OPEN
<script src=”Scripts/jquery-1.4.1.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function () {
jQuery.ajax({
url: 'test.aspx/SetOpenWindow',
type: "POST",
dataType: "json",
data: "",
contentType: "application/json; charset=utf-8",
success: function (data) {}//alert(JSON.stringify(data));
});
$("#form1").submit(function () {
submitted = true;
});
});
</script>
关闭浏览器:
在可以关闭浏览器窗口的页面上,调用JavaScript可以在浏览器窗口关闭时进行捕获(方便将其添加到母版页而不是每个页面上!)。这将调用ClosingSessionPage aspx页面来运行SetClosingWndow Web方法:
<script type=”text/javascript”>
var submitted = false;
function wireUpWindowUnloadEvents() {
$(document).on('keypress', function (e) { if (e.keyCode == 116) { callServerForBrowserCloseEvent(); } }); // Attach the event keypress to exclude the F5 refresh
$(document).on("click", "a", function () { callServerForBrowserCloseEvent(); }); // Attach the event click for all links in the page
}
$(window).bind("onunload", function () { if (!submitted) { callServerForBrowserCloseEvent(); event.preventDefault(); } });
$(window).bind("beforeunload", function () { if (!submitted) { callServerForBrowserCloseEvent(); event.preventDefault(); } });
window.onbeforeunload = function () { if (!submitted) { callServerForBrowserCloseEvent(); } };
window.onunload = function () { if (!submitted) { callServerForBrowserCloseEvent(); } };
$(window).bind("onunload", function () { if (!submitted) { callServerForBrowserCloseEvent();event.preventDefault();} });
function callServerForBrowserCloseEvent() {
window.open("ClosingSessionPage.aspx", "Closing Page", "location=0,menubar=0,statusbar=1,width=1,height=1"); }
function btn_onclick() { open(location, '_self').close(); }
</script>
上面的close JavaScript方法重定向到一个关闭的aspx页面,以运行一些ajax,然后自行关闭– Ajax将SetClosingWindow的会话WINDOW_STATUS变量调用为CLOSED。
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server" title="Closing Session">
<script src="Scripts/jquery-1.4.1.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function () {
jQuery.ajax({
url: 'test.aspx/SetClosingWndow',
type: "POST",
dataType: "json",
data: "",
contentType: "application/json; charset=utf-8",
success: function (data) {
window.close();
} }); });
</script>
</head>
<body>
<form id="form1" runat="server">
<p>Closing browser session, please wait.</p>
</form>
</body>
</html>
C#SetClosingWindow 当浏览器窗口关闭并将WINDOW_STATUS设置为CLOSED时,从Ajax JavaScript调用:
[WebMethod()]
[ScriptMethod(UseHttpGet = false)]
public static string SetClosingWndow()
{
HttpContext.Current.Session["WINDOW_STATUS"] = "CLOSED";
ScriptManager.RegisterClientScriptBlock((Page)(HttpContext.Current.Handler), typeof(Page), "closePage", "window.close();", true);
return "Status:" + HttpContext.Current.Session["WINDOW_STATUS"];
}
3。浏览器是否打开?
VB6在需要知道浏览器窗口是打开还是关闭时由VB6调用的ASP.Net WebMethod。
[WebMethod()]
[ScriptMethod(UseHttpGet = false)]
public static string IsWindowOpen()
{
string isWindowOpen = HttpContext.Current.Session["WINDOW_STATUS"] != null ? HttpContext.Current.Session["WINDOW_STATUS"].ToString() : "CLOSED";
return "IsWindowOpen:" + isWindowOpen;
}