webRequest和webNavigation

 chrome有2套可以拦截URL的API:

webRequest和webNavigation


试验中:
http://developer.chrome.com/extensions/declarativeWebRequest.html
据说速度更快,而且可以 RedirectToEmptyDocument [但是在试验阶段]

两者关系:

There is no defined ordering between events of the webRequest API and the events of the webNavigation API. It is possible that webRequest events are still received for frames that already started a new navigation, or that a navigation only proceeds after the network resources are already fully loaded.

In general, the webNavigation events are closely related to the navigation state that is displayed in the UI, while the webRequest events correspond to the state of the network stack which is generally opaque to the user.


webNavigation只会发出事件。不能拦截。

webRequest可以拦截请求并改变内容。
http://developer.chrome.com/extensions/webRequest.html#type-RequestFilter

BlockingResponse

object )
Returns value for event handlers that have the 'blocking' extraInfoSpec applied. Allows the event handler to modify network requests.

用cancel方式拦截会导致页面无法继续
chrome.webRequest.onBeforeRequest.addListener(
  function(info) {
    console.log("onBeforeRequest:"+info.type );
   return {cancel:true};
  },
  // filters
  {
    urls: [
      "<all_urls>"
    ],
    types: ["main_frame"]
 },
  // extraInfoSpec
  ["blocking"]
  );

我写了一个有输出的content script. 从console中看到content script在这种情况下也没有被调用

If the optional  opt_extraInfoSpec  array contains the string  'blocking'  (only allowed for specific events), the callback function is handled synchronously. That means that the request is blocked until the callback function returns. In this case, the callback can return a BlockingResponse  that determines the further life cycle of the request. Depending on the context, this response allows cancelling or redirecting a request ( onBeforeRequest ), cancelling a request or modifying headers ( onBeforeSendHeaders onHeadersReceived ), or providing authentication credentials ( onAuthRequired ).



所以能够cancel请求的有2处:onBeforeRequest和onBeforeSendHeaders
上帝王牌,Web Navigation Description Standard web browsers contain features to move backward and forward among the pages recently visited. One way to implement these features is to use two stacks to keep track of the pages that can be reached by moving backward and forward. In this problem, you are asked to implement this. The following commands need to be supported: BACK: Push the current page on the top of the forward stack. Pop the page from the top of the backward stack, making it the new current page. If the backward stack is empty, the command is ignored. FORWARD: Push the current page on the top of the backward stack. Pop the page from the top of the forward stack, making it the new current page. If the forward stack is empty, the command is ignored. VISIT : Push the current page on the top of the backward stack, and make the URL specified the new current page. The forward stack is emptied. QUIT: Quit the browser. Assume that the browser initially loads the web page at the URL http://www.acm.org/ Input Input is a sequence of commands. The command keywords BACK, FORWARD, VISIT, and QUIT are all in uppercase. URLs have no whitespace and have at most 70 characters. You may assume that no problem instance requires more than 100 elements in each stack at any time. The end of input is indicated by the QUIT command. Output For each command other than QUIT, print the URL of the current page after the command is executed if the command is not ignored. Otherwise, print "Ignored". The output for each command should be printed on its own line. No output is produced for the QUIT command. Sample Input VISIT http://acm.ashland.edu/ VISIT http://acm.baylor.edu/acmicpc/ BACK BACK BACK FORWARD VISIT http://www.ibm.com/ BACK BACK FORWARD FORWARD FORWARD QUIT Sample Output http://acm.ashland.edu/ http://acm.baylor.edu/acmicpc/ http://acm.ashland.edu/ http://www.acm.org/ Ignored http://acm.ashland.edu/ http://www.ibm.com/ http://acm.ashland.edu/ http://www.acm.org/ http://acm.ashland.edu/ http://www.ibm.com/ Ignored
UnityWebRequest.Post 和 PostWWWForm 都是在 Unity 游戏引擎中用于网络请求的工具,主要用于发送 POST 请求。 1. UnityWebRequest.Post: - `UnityWebRequest` 是 Unity 的内置 API,提供了一个低级别的、线程安全的方式来发起 HTTP 请求。`UnityWebRequest.Post(url, data)` 这个方法用于发送 POST 请求,其中 `url` 是你要请求的服务器地址,`data` 是要发送的数据(通常是一个 JSON 字符串,可以用 `JsonUtility.ToJson()` 转换)。你需要设置好异步任务,并通过 `await` 关键字等待其完成,然后检查响应状态是否成功。 ```csharp using UnityEngine; using System.Collections; using UnityEngine.UI; using System.Net; public class NetworkExample : MonoBehaviour { public Button sendButton; private async void SendPOSTRequest() { UnityWebRequest www = UnityWebRequest.Post("https://api.example.com/data", textData); www.Send(); await www WaitUntilComplete(); if (www.isNetworkError || www.isHttpError) Debug.LogError(www.error); else Debug.Log(www.downloadHandler.text); } } ``` 2. PostWWWForm: - `PostWWWForm` 则来自于第三方库 `WWWForm`,这个库的功能更强大,支持上传文件和表单数据。它创建了一个 `WWWForm` 对象,你可以添加键值对到其中,然后传递给 `WWW` 类的 `Post` 方法。`WWW` 类会处理发送和解析返回结果的过程。 ```csharp using UnityEngine; using System.IO; public class NetworkExample : MonoBehaviour { public Button sendButton; public InputField usernameInput; public FileUploader fileUploader; private void SendPOSTRequest() { WWWForm form = new WWWForm(); form.AddField("username", usernameInput.text); if (fileUploader != null && fileUploader.FileToUpload != null) form.AddFileData("file", fileUploader.FileToUpload); WWW www = new WWW("https://api.example.com/upload", form); yield return www; if (!string.IsNullOrEmpty(www.error)) Debug.LogError(www.error); else Debug.Log("Response: " + www.text); } } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值