简体   繁体   English

如何使用Fiddler在请求中设置cookie?

[英]How can I set a cookie in a request using Fiddler?

I need to set a cookie before I issue a request to a Web site using Fiddler. 在使用Fiddler向网站发出请求之前,我需要设置一个cookie。 How do I do this? 我该怎么做呢?

简单...您需要设置一个标头值,与您的请求一样,如下所示:

Cookie: YourCookieName=YourCookieValue

To do this using the FiddlerScript engine , add the following code into the onBeforeRequest method: 要使用FiddlerScript引擎执行此操作,请将以下代码添加到onBeforeRequest方法中:

oSession.oRequest["Cookie"] = (oSession.oRequest["Cookie"] + ";YourCookieName=YourCookieValue");

This will preserve any other cookies that have been set. 这将保留已设置的任何其他cookie。

You need to be more specific about what you're trying to do. 您需要更具体地了解您要做的事情。

You can edit (or add) an outbound Cookie header to send a cookie to the website. 您可以编辑(或添加)出站Cookie标头以将cookie发送到网站。 You can do this either manually or via the FiddlerScript engine . 您可以手动或通过FiddlerScript引擎执行此操作。 But that doesn't "set" the cookie on the client-- it simply sends it to the server. 但这不会在客户端“设置”cookie - 它只是将其发送到服务器。 If you want to set a cookie on the client, you either have to use another means, or you can inject a Set-Cookie response header on a previous response from the server, with the value you want to set on the client. 如果要在客户端上设置cookie,则必须使用其他方法,或者可以在服务器的先前响应上注入Set-Cookie响应头,并在客户端上设置要设置的值。

You can also use the Fiddler Composer. 您也可以使用Fiddler Composer。

  1. Run Fiddler 运行Fiddler
  2. Open the Composer Tab on the top. 打开顶部的Composer选项卡。

It's easiest if you can start with another request from your web site. 如果您可以从您的网站发出另一个请求,这是最简单的。 To do this capture a the request you want to modify, then drag it from the UI to the composer tab. 要执行此操作,请捕获要修改的请求,然后将其从UI拖动到composer选项卡。

A good explanation is here: http://www.debugtheweb.com/Fiddler/help/composer.asp 这里有一个很好的解释: http//www.debugtheweb.com/Fiddler/help/composer.asp

Fiddler allows your to resend/rebuild an existing request. Fiddler允许您重新发送/重建现有请求。 There is a Request Builder . 有一个Request Builder While rebuilding in the RAW form, modify your cookies. 在RAW表单中重建时,请修改您的cookie。

This solution is valid for Cookie based authentication: 此解决方案对基于Cookie的身份验证有效:

If you want to test the API/url which have authentication enabled, please try following, i am showing for MVC web API on IIS server. 如果你想测试启用了身份验证的API / url,请尝试以下,我在IIS服务器上显示MVC web API。 usually there are more than 1 cookie responsible for authorization, so you may need to send more than 1 cookie in header as follows: 通常有超过1个cookie负责授权,因此您可能需要在标头中发送多个cookie,如下所示:

User-Agent: Fiddler Host: localhost:51000 content-Type: application/json Cookie : .ASPXAUTH=xxxxx;ASP.NET_SessionId=yyyy;__RequestVerificationToken=zzzz

When running Fiddler as a reverse Proxy you can modify the response headers via FiddlerScript by adding a line in the OnBeforeResponse method: Fiddler作为反向代理运行时您可以通过在OnBeforeResponse方法中添加一行来通过FiddlerScript修改响应头:

static function OnBeforeResponse(oSession: Session) {
    // ...
    oSession.oResponse["Set-Cookie"] = "sessionToken=abc123; Expires=Wed, 09 Jun 2021 10:18:14 GMT";
}

Also check Fiddler docs about Modifying a Request or Response for more info. 还可以查看有关修改请求或响应的 Fiddler文档获取更多信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM