服务端仿客户端访问网页

来源:互联网 发布:linux删除大于1g文件 编辑:程序博客网 时间:2024/05/17 11:07

 asp访问asp.net session, 实时访问

getAspSession.aspx
 public string AspMakeCookies()
  {
   string result = "";
   int limit = Request.Cookies.Count-1;
   for(int i=0; i<limit; i++)
   {
    HttpCookie acookie = Request.Cookies[i];
    if(acookie.HasKeys == false)
    {
     result += acookie.Name + "=" + acookie.Value + "; ";
    }
    else
    {
     int limit2 = acookie.Values.Count - 1;
     for(int j=0; j<limit2; j++)
     {
      result += acookie.Values.AllKeys[j] + "=" + acookie.Values[j] + "; ";
     }
    }
   }
   result = result.Substring(0, result.Length - 2);
   return result;
  }

  public string GetAspSession(string sessionName)
  {
   string strCookie = this.AspMakeCookies();
   string sessionUrl = System.Configuration.ConfigurationSettings.AppSettings["sessionUrl"];
   string reqUrl=sessionUrl+sessionName;
   System.Net.WebRequest myWebRq=System.Net.WebRequest.Create(reqUrl);
   myWebRq.ContentType = "application/x-www-form-urlencoded";
   myWebRq.Headers.Set("Cookie","any non-empty string here");
   myWebRq.Headers.Set("Cookie",strCookie);
   System.Net.WebResponse myWebRponse =myWebRq.GetResponse();  
   System.IO.StreamReader sr= new System.IO.StreamReader(myWebRponse.GetResponseStream(), System.Text.Encoding.GetEncoding("GB2312"));
   string strHtml=sr.ReadToEnd();
   myWebRponse.Close();
   myWebRq.Abort();
   return strHtml; 
  }

AspSessionService.asp

<%
response.Buffer=false
sessionName=Request("sessionName")
if sessionName <> "" then
Response.Write(Session(sessionName))
end if
%>

----------------------------------------------------------------------------------------

asp访问aspx加密的cookies

getAspxCookies.asp

<%
'==== 字符转换
Function bytes2BSTR(vIn)
    strReturn = ""
    For i = 1 To LenB(vIn)
        ThisCharCode = AscB(MidB(vIn,i,1))
        If ThisCharCode < &H80 Then
            strReturn = strReturn & Chr(ThisCharCode)
        Else
            NextCharCode = AscB(MidB(vIn,i+1,1))
            strReturn = strReturn & Chr(CLng(ThisCharCode) * &H100 + CInt(NextCharCode))
            i = i + 1
        End If
    Next
    bytes2BSTR = strReturn
End Function
function getCookie(cookie_value)
 Set objHTTP = Server.CreateObject("MSXML2.XMLHTTP")
 Set xmlDOC =Server.CreateObject("Microsoft.XMLDOM")
 strWebserviceURL = "http://" & getAppPath() &  "/cookieService.aspx"
 '设置参数及其值//我觉得这里也可以用Server.Execute
 strRequest = "cookie_value=" & cookie_value
 objHTTP.Open "POST", strWebserviceURL, False
 '设置这个Content-Type很重要
 objHTTP.SetRequestHeader "Content-Type", "application/x-www-form-urlencoded"
 objHTTP.Send(strRequest)

 '看看状态值
 if objHTTP.Status=200 then
 getCookie = bytes2BSTR(objHTTP.responseBody)
 end if
end function
function getAppPath()
 getAppPath = request.ServerVariables("HTTP_HOST")
 path = request.ServerVariables("APPL_MD_PATH")
 if (len(path)-InStrRev(path,"/Root")-5)>0 then
  getAppPath = getAppPath & "/" & right(Path,len(path)-InStrRev(path,"/Root")-5)
 end if
end function

%>

aspxCookiesService.aspx

<%@ Page language="c#" %>
<%@ Import Namespace="System"%>
<%@ Import Namespace="System.Web"%>
<%@ Import Namespace="System.Web.Services"%>
<%@ Import Namespace="System.Web.Security"%>
<%@ Import Namespace="System.Security.Principal"%>
<script runat=server>
  public string GetMemberNameByValue(string cookie_value)
  {
   if(null == cookie_value || "" == cookie_value)
   {
    return "";
   }
   FormsAuthenticationTicket authTicket = null;
   try
   {
    authTicket = FormsAuthentication.Decrypt(cookie_value);//解密
   }
   catch(Exception ex)
   {
    return "";
   }
   if(null == authTicket)
   {
    return "";
   }
   string[] roles = authTicket.UserData.Split(new char[]{','});  //根据存入时的格式分解,;或|....
   FormsIdentity id = new FormsIdentity(authTicket);
   GenericPrincipal principal = new GenericPrincipal(id, roles);
   return principal.Identity.Name;
  }

  private void Page_Load(object sender, System.EventArgs e)
  {
   // 在此处放置用户代码以初始化页面
   Response.Write(GetMemberNameByValue(Request["cookie_value"]));
  }
</script>

原创粉丝点击