2015-11-03 108 views
0

當前正在開發顯示Web瀏覽器的.net C#應用程序。但由於Visual Studio網絡瀏覽器仍在使用IE7,並且不支持很多事情,因此我打算放入Chromium的CefSharp。那麼,你們有沒有嘗試使用CefSharp從本地主機服務器獲取一些json數據?我嘗試了兩種方式來獲得它,但失敗了。使用CefSharp從本地主機端口獲取數據的方法

對於C#在Visual Studio中,我解僱了Chromium瀏覽器這樣的:

var test = new CefSharp.WinForms.ChromiumWebBrowser(AppDomain.CurrentDomain.BaseDirectory + "html\\index.html") 
     { 
      Dock = DockStyle.Fill, 
     }; 
     this.Controls.Add(test); 

那麼對於index.html的,它是需要從本地主機端口1000得到的數據加載它之後。我已經嘗試了JavaScript的方式有兩種:

首先使用XMLHttpRequest的:

var xmlhttp = new XMLHttpRequest(); 
    var url = "http://localhost:1000/api/data1"; 
    var services; 
    xmlhttp.onreadystatechange = function() { 
     if (xmlhttp.readyState == 4 && xmlhttp.status == 200) { 
      services = jQuery.parseJSON(xmlhttp.responseText); 
     } 
    } 
    xmlhttp.open("GET", url, true); 
    xmlhttp.send(); 

其次使用jQuery的獲得():

 $.get("http://localhost:1000/api/data1", function (data) { 
     var services = data; 
    }); 

但是這兩種方式不能返回的數據。如果我將index.html放入Chrome或Firefox這樣的普通瀏覽器中,我就可以獲取數據。

在我的編碼中是否缺少某些東西?任何想法什麼是錯的傢伙?

+0

這是你在找什麼?請檢查我的答案。 – null1941

回答

0

我正在使用Chromium Web瀏覽器並向本地主機發送GET請求以獲取JSON。隨着這一點,我正在運行一個Web服務器,它不斷監聽並返回JSON。

網絡服務器:

public class WebServer 
{ 
    public WebServer() 
    { 
    } 
    void Process(object o) 
    { 
     Thread thread = new Thread(() => new WebServer().Start()); 
     thread.Start(); 

     HttpListenerContext context = o as HttpListenerContext; 
     HttpListenerResponse response = context.Response; 
     try 
     { 
      string json; 
      string url = context.Request.Url.ToString(); 
      if (url.Contains("http://localhost:8888/json")) 
      { 
       List<SampleObject> list = new List<SampleObject>(); 
       json = JsonConvert.SerializeObject(new 
       { 
        results = list 
       }); 
       byte[] decryptedbytes = new byte[0]; 
       decryptedbytes = System.Text.Encoding.UTF8.GetBytes(json); 
       response.AddHeader("Content-type", "text/json"); 
       response.ContentLength64 = decryptedbytes.Length; 
       System.IO.Stream output = response.OutputStream; 
       try 
       { 
        output.Write(decryptedbytes, 0, decryptedbytes.Length); 
        output.Close(); 
       } 
       catch (Exception e) 
       { 
        response.StatusCode = 500; 
        response.StatusDescription = "Server Internal Error"; 
        response.Close(); 
        Console.WriteLine(e.Message); 
       } 
      } 
     } 
     catch (Exception ex) 
     { 
      response.StatusCode = 500; 
      response.StatusDescription = "Server Internal Error"; 
      response.Close(); 
      Console.WriteLine(ex); 
     } 
    } 
    static byte[] GetBytes(string str) 
    { 
     byte[] bytes = new byte[str.Length * sizeof(char)]; 
     System.Buffer.BlockCopy(str.ToCharArray(), 0, bytes, 0, bytes.Length); 
     return bytes; 
    } 
    public void Start() 
    { 
     HttpListener server = new HttpListener(); 
     server.Prefixes.Add("http://localhost:8888/json"); 
     server.Start(); 
     while (true) 
     { 
      ThreadPool.QueueUserWorkItem(Process, server.GetContext()); 
     } 

    } 
} 
public class SampleObject 
{ 
    string param1 { get; set; } 
    string param2 { get; set; } 
    string param3 { get; set; } 
} 

要啓動Web服務器:

Thread thread = new Thread(() => new WebServer().Start()); 
     thread.Start(); 

的Index.html

<!DOCTYPE html> 
 
<html> 
 
<head> 
 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script> 
 
<script> 
 
$(document).ready(function(){ 
 
    $.get("http://localhost:8888/json", function (data) { 
 
     var jsonData= data; 
 
    }); 
 
}); 
 
</script> 
 
</head> 
 
<body> 
 

 
<p>Example JSON Request.</p> 
 

 
</body> 
 
</html>

之前在Chromium網絡瀏覽器中啓動Index.html,開始運行網絡服務器來偵聽請求。在文檔加載事件之後,它會進行ajax調用,然後它會點擊Webserver,然後Webserver將返回JSON。你也可以使用Chrome來測試它。啓動網絡服務器並在地址欄中輸入網址(http://localhost:8888/json),您將在開發人員工具中看到返回的JSON。

注意:代碼未經測試。希望它能起作用。

相關問題