2012-11-12 7 views
1

현재 C# 환경에서 Awsomnium 1.7로 작업하고 있습니다. 난 그냥 코어를 사용하고 사용자 정의 게시 매개 변수를 정의하려고합니다. 자, 내가 많이 봤 거든 심지어 awsomnium 포럼에 게시했지만 아무 대답도 없었다. 나는 그 개념을 이해하지만, 최근의 변화는 제안 된 기계공과 예를 그냥 떨어 뜨렸다.Awesomnium Post 매개 변수

내가 무엇을 발견 : 웹보기 클래스가 더 이상 "OnResourceRequest"이벤트를 포함하지 않는 것을 http://support.awesomium.com/kb/general-use/how-do-i-send-form-values-post-data

이의 문제입니다. 지금까지 IResourceInterceptor를 구현했으며 "OnRequest"기능을 덮어 썼습니다. 공개 ResourceResponse OnRequest (ResourceRequest 요청) 은 서명이지만 요청 머리글을 추가 할 수있는 기회가 없습니다. 누구든지 여기에 어떤 아이디어가 있습니까? 설명서를 살펴 보았으나 그 중 아무 것도 찾지 못했습니다 .....

답변

5

WebView가 아닌 ​​WebCore에 IResourceInterceptor를 연결해야합니다. 다음은 작업 예입니다

자원 인터셉터 :

public class CustomResourceInterceptor : ResourceInterceptor 
{ 
    protected override ResourceResponse OnRequest(ResourceRequest request) 
    { 
     request.Method = "POST"; 
     var bytes = "Appending some text to the request"; 
     request.AppendUploadBytes(bytes, (uint) bytes.Length); 
     request.AppendExtraHeader("custom-header", "this is a custom header"); 

     return null; 
    } 
} 

주요 응용 프로그램 :

public MainWindow() 
{ 
    WebCore.Started += WebCoreOnStarted; 
    InitializeComponent(); 
} 

private void WebCoreOnStarted(object sender, CoreStartEventArgs coreStartEventArgs) 
{ 
    var interceptor = new CustomResourceInterceptor(); 

    WebCore.ResourceInterceptor = interceptor; 
    //webView is a WebControl on my UI, but you should be able to create your own WebView off WebCore 
    webView.Source = new Uri("http://www.google.com"); 
} 
+0

.net dll에서 ResourceInterceptor를 찾지 못했습니다. 버전 1.7 사용 모든 단서? – Ubaid

+0

Awesomium.Core.dll을 찾고 있는지 확인하십시오. 확실히 거기에 있습니다. 문서에도 나와 있습니다. http://docs.awesomium.net/ – HotN

+0

아마도 Awesomium dll에는 ResourceInterceptor 클래스가 없기 때문에 구현해야하는 IResourceInterceptor 인터페이스 만 존재합니까? – AlexP11223

4

HotN의 대답은 위의 좋은; 사실, 그것은 내 대답을 기반으로합니다. 그러나, 나는 일주일 동안이 정보를 찾고 일할 무언가를 모으는 데 보냈습니다. (위의 대답은 최소한 Awesomium의 v1.7과 함께 작동하지 않는 몇 가지 문제를 가지고 있습니다.) 내가 찾고 있던 것은 곧바로 사용할 수있는 무언가였습니다.

여기 해결책이 있습니다. 그것은 개선이 필요하지만, 지금은 내 필요에 어울립니다. 나는 이것이 다른 누군가를 돕기를 바랍니다.

// CRI.CustomResourceInterceptor 
// 
// Author: Garison E Piatt 
// Contact: {removed} 
// Created: 11/17/14 
// Version: 1.0.0 
// 
// Apparently, when Awesomium was first created, the programmers did not understand that someone would 
// eventually want to post data from the application. So they made it incredibly difficult to upload 
// POST parameters to the remote web site. We have to jump through hoops to get that done. 
// 
// This module provides that hoop-jumping in a simple-to-understand fashion. We hope. It overrides 
// the current resource interceptor (if any), replacing both the OnRequest and OnFilterNavigation 
// methods (we aren't using the latter yet). 
// 
// It also provides settable parameters. Once this module is attached to the WebCore, it is *always* 
// attached; therefore, we can simply change the parameters before posting to the web site. 
// 
// File uploads are currently unhandled, and, once handled, will probably only upload one file. We 
// will deal with that issue later. 
// 
// To incoroprate this into your application, follow these steps: 
// 1. Add this file to your project. You know how to do that. 
// 2. Edit your MainWindow.cs file. 
// a. At the top, add: 
//  using CRI; 
// b. inside the main class declaration, near the top, add: 
//  private CustomResourceInterceptor cri; 
// c. In the MainWindow method, add: 
//  WebCore.Started += OnWebCoreOnStarted; 
//  cri = new CustomResourceInterceptor(); 
//  and (set *before* you set the Source value for the Web Control): 
//  cri.Enabled = true; 
//  cri.Parameters = String.Format("login={0}&password={1}", login, pw); 
//  (Choose your own parameters, but format them like a GET query.) 
// d. Add the following method: 
//  private void OnWebCoreOnStarted(object sender, CoreStartEventArgs coreStartEventArgs) { 
//   WebCore.ResourceInterceptor = cri; 
//  } 
// 3. Compile your application. It should work. 

using System; 
using System.Runtime.InteropServices; 
using System.Text; 
using Awesomium.Core; 
using Awesomium.Windows.Controls; 


namespace CRI { 
    //* CustomResourceInterceptor 
    // This object replaces the standard Resource Interceptor (if any; we still don't know) with something 
    // that allows posting data to the remote web site. It overrides both the OnRequest and OnFilterNavigation 
    // methods. Public variables allow for run-time configuration. 
    public class CustomResourceInterceptor : IResourceInterceptor { 
    // Since the default interceptor remains overridden for the remainder of the session, we need to disable 
    // the methods herein unless we are actually using them. Note that both methods are disabled by default. 
    public bool RequestEnabled = false; 
    public bool FilterEnabled = false; 

    // These are the parameters we send to the remote site. They are empty by default; another safeguard 
    // against sending POST data unnecessarily. Currently, both values allow for only one string. POST 
    // variables can be combined (by the caller) into one string, but this limits us to only one file 
    // upload at a time. Someday, we will have to fix that. And make it backward-compatible. 
    public String Parameters = null; 
    public String FilePath = null; 

    /** OnRequest 
    ** This ovverrides the default OnRequest method of the standard resource interceptor. It receives 
    ** the resource request object as a parameter. 
    ** 
    ** It first checks whether or not it is enabled, and returns NULL if not. Next it sees if any 
    ** parameters are defined. If so, it converst them to a byte stream and appends them to the request. 
    ** Currently, files are not handled, but we hope to add that someday. 
    */ 
    public ResourceResponse OnRequest(ResourceRequest request) { 
     // We do nothing at all if we aren't enabled. This is a stopgap that prevents us from sending 
     // POST data with every request. 
     if (RequestEnabled == false) return null; 

     // If the Parameters are defined, convert them to a byte stream and append them to the request. 
     if (Parameters != null) { 
     var str = Encoding.Default.GetBytes(Parameters); 
     var bytes = Encoding.UTF8.GetString(str); 

     request.AppendUploadBytes(bytes, (uint)bytes.Length); 
     } 

     // If either the parameters or file path are defined, this is a POST request. Someday, we'll 
     // figure out how to get Awesomium to understand Multipart Form data. 
     if (Parameters != null || FilePath != null) { 
     request.Method = "POST"; 
     request.AppendExtraHeader("Content-Type", "application/x-www-form-urlencoded"); //"multipart/form-data"); 
     } 

     // Once the data has been appended to the page request, we need to disable this process. Otherwise, 
     // it will keep adding the data to every request, including those that come from the web site. 
     RequestEnabled = false; 
     Parameters = null; 
     FilePath = null; 

     return null; 
    } 


    /** OnFilterNavigation 
    ** Not currently used, but needed to keep VisualStudio happy. 
    */ 
    public bool OnFilterNavigation(NavigationRequest request) { 
     return false; 
    } 
    } 
} 
+0

지시 사항에'cri.RequestEnabled = true;가 아닌'cri.Enabled = true;가 있어야합니다. – ThePersonWithoutC

관련 문제