zoukankan      html  css  js  c++  java
  • WebBrowser 跨域之间访问的方法

        项目需要,本地静态页访问远程页面,然后远程页面反过来调用本地页面。这样在浏览器中是不可以实现的,只有 ie 5 调低了安全级别才可以。在一个 winform 程序中,可以用 webbrowser来实现。

        代码

    using System;
    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.Configuration;
    using System.Web;

    namespace SetLocalPath
    {
        
    public partial class Form1 : Form
        {
            
    public Form1()
            {
                InitializeComponent();
            }

            
    //string href = string.Empty;

            
    private void button1_Click(object sender, EventArgs e)
            {
                
    //HtmlElement middlecenter = webBrowser1.Document.All["smile"];
                
    //middlecenter.SetAttribute("src", "e:\\pdm.jpg");

                
                
    //webBrowser1.Document.Links[0].SetAttribute("href", "F:\\TestWebSite\\index.html");
            }

            
    void link_Click(object sender, HtmlElementEventArgs e)
            {
                HtmlElement archor 
    = (HtmlElement)sender;
                
    string url = archor.GetAttribute("href");

                
    string fileType = url.Substring(url.LastIndexOf('.'+ 1);

                
    if (fileType == "html" || fileType == "htm")
                {
                   url 
    = url.Replace("//""\\");
                }
                
                webBrowser1.Navigate(url);
            }

            
    private void webBrowser1_Navigating(object sender, WebBrowserNavigatingEventArgs e)
            {
                
    string url = e.Url.ToString();

                
    if (lastNavigate != url && !isFirstTime && url.IndexOf("javascript"== -1)
                {
                    lastNavigate 
    = url;
                    url 
    = System.Web.HttpUtility.UrlDecode(url, Encoding.GetEncoding("utf-8")); // 转一次编码,将 %3 转为 ?
                    string fileType = url.Substring(url.LastIndexOf('.'+ 1);

                    
    if (fileType.IndexOf('?'> -1)
                    {
                        fileType 
    = fileType.Substring(0, fileType.IndexOf('?'));
                    }

                    
    if (fileType == "html" || fileType == "htm")
                    {

                        
    //url = ConfigurationManager.AppSettings["localfile"].ToString() + "\\" + url.Substring(url.LastIndexOf('\\') + 1);
                        if (url.StartsWith("http"))
                        {
                            url 
    = ConfigurationManager.AppSettings["localfile"].ToString().TrimEnd(new char[] { '\\''/' }) + "\\" +
                                url.Replace(ConfigurationManager.AppSettings[
    "remote"].ToString().TrimEnd(new char[] { '\\''/' }) + "/""");
                        }
                        
    else
                        {
                            url 
    = url.Replace("file:///""").Replace("/""\\");
                        }
                        
    //url = url.Replace("/", "\\");
                    }
                    
    else if (fileType == "aspx")
                    {
                        
    if (url.StartsWith("file"))
                        {
                            url 
    = ConfigurationManager.AppSettings["remote"].ToString().TrimEnd(new char[] { '\\''/' }) + "/" +
                                url.Replace(
    "file:///""").Replace(ConfigurationManager.AppSettings["localfile"].ToString().Replace('\\''/').TrimEnd(new char[] { '\\''/' }) + "/""");
                        }
                        
    else
                        {
                            url 
    = System.Text.RegularExpressions.Regex.Replace(url,
                                
    "http://([^.]+)/", ConfigurationManager.AppSettings["remote"].ToString().TrimEnd(new char[] { '\\''/' }) + "/");

                        }
                        lastNavigate 
    = url;
                        
    //url = ConfigurationManager.AppSettings["remote"].ToString().TrimEnd(new char[] { '\\', '/' }) + "/" + currPage.Replace(ConfigurationManager.AppSettings["remote"].ToString().TrimEnd(new char[] { '\\', '/' }), "");
                    }

                    
    //lastNavigate = url;
                    this.webBrowser1.Navigate(url);

                }

                
    // 尝试
                currPage = e.Url.ToString();

                
    if (isFirstTime)
                {
                    lastNavigate 
    = url;
                    isFirstTime 
    = false;
                }
            }

            
    private void webBrowser1_Navigated(object sender, WebBrowserNavigatedEventArgs e)
            {
                
    //this.webBrowser1.Navigate("F:\\TestWebSite\\index.html");
                
    //string url = e.Url.ToString();
                
    //string fileType = url.Substring(url.LastIndexOf('.') + 1);


                
    //if (fileType == "html" || fileType == "htm")
                
    //{
                
    //    this.webBrowser1.Navigate(url);
                
    //}
            }

            
    private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
            {


            }

            
    private void webBrowser1_DocumentCompleted_1(object sender, WebBrowserDocumentCompletedEventArgs e)
            {
                
    foreach (HtmlElement archor in this.webBrowser1.Document.Links)
                {
                    archor.SetAttribute(
    "target""_self");
                    
    //archor.Click += new HtmlElementEventHandler(link_Click);
                }

                
    foreach (HtmlElement form in this.webBrowser1.Document.Forms)
                {
                    form.SetAttribute(
    "target""_self");
                }

                
    //HtmlElement link = webBrowser1.Document.All["linktest"];
                
    //href = link.GetAttribute("href");
                
    //link.Click += new HtmlElementEventHandler(link_Click);
            }

            
    string lastNavigate = string.Empty;
            
    string prepre = string.Empty;
            
    string currPage = string.Empty;
            
    bool isFirstTime = true;


            
    private void webBrowser1_Navigated_1(object sender, WebBrowserNavigatedEventArgs e)
            {
                
    string url = e.Url.ToString();
                
                
    if (currPage != url && !isFirstTime)
                {
                    url 
    = System.Web.HttpUtility.UrlDecode(url, Encoding.GetEncoding("utf-8")); // 转一次编码,将 %3 转为 ?
                    string fileType = url.Substring(url.LastIndexOf('.'+ 1);

                    
    if (fileType.IndexOf('?'> -1)
                    {
                        fileType 
    = fileType.Substring(0, fileType.IndexOf('?'));
                    }

                    
    if (fileType == "html" || fileType == "htm")
                    {

                        
    if (url.StartsWith("http"))
                        {
                            url 
    = ConfigurationManager.AppSettings["localfile"].ToString().TrimEnd(new char[] { '\\''/' }) + "\\" +
                                url.Replace(ConfigurationManager.AppSettings[
    "remote"].ToString().TrimEnd(new char[] { '\\''/' }), "");
                        }
                        
    else
                        {
                            url 
    = url.Replace("file:///""").Replace("/""\\");
                        }
                    }
                    
    else if (fileType == "aspx")
                    {
                        
    if (url.StartsWith("file"))
                        {
                            url 
    = ConfigurationManager.AppSettings["remote"].ToString().TrimEnd(new char[] { '\\''/' }) + "/" +
                                url.Replace(
    "file:///""").Replace(ConfigurationManager.AppSettings["localfile"].ToString().Replace('\\''/').TrimEnd(new char[] { '\\''/' }) + "/""");
                        }
                        
    else
                        {
                            url 
    = System.Text.RegularExpressions.Regex.Replace(url,
                                
    "http://([^.]+)/", ConfigurationManager.AppSettings["remote"].ToString().TrimEnd(new char[] { '\\''/' }) + "/");

                        }

                    }
                    lastNavigate 
    = url;
                    
    this.webBrowser1.Navigate(url);

                }


                
    if (isFirstTime)
                {
                    lastNavigate 
    = url;
                    isFirstTime 
    = false;
                }

                 
    //应该是跳转的页面,而不是 当前的
            }

        }
    }

    走向地狱的途中,不小心走了程序员这条路,路上一个个黑心的老板,和暗无天日的加班,我才发现,通往地狱的路径中,我们这行是最短的。

  • 相关阅读:
    Spring事务原理分析-部分二
    Spring事务原理分析-部分一
    Spring 通读官方文档
    Spring IOC 源码解析
    SpringAOP原理分析
    Docker学习笔记
    TCP、UDP和HTTP关系
    洛谷P3312
    洛谷P3327
    BZOJ3073
  • 原文地址:https://www.cnblogs.com/zlfucku/p/1702539.html
Copyright © 2011-2022 走看看