如果不想止步不前,那就加油吧

.NET程序-配置使用Fidder代理,抓取后台HTTP请求

一般我们用Fidder查看浏览器的Http请求来分析页面的性能问题,但是如果是后台.Net发起Http请求怎么看?下面介绍一种方法,通过配置.NET程序,使用Fidder代理可以查看后台的HTTP请求。

首先开启Fiddler2代理:

 

下面有三种方式设置System.Net使用Fidder作为。

方式一:代码方式

var defaultProxy = new WebProxy();
defaultProxy.Address = new Uri("http://localhost:8888");
defaultProxy.Credentials = new NetworkCredential("xxxxx", "********");
System.Net.WebRequest.DefaultWebProxy = defaultProxy;

方式二:配置文件

  <system.net>
    <defaultProxy useDefaultCredentials="True">
      <proxy proxyaddress="http://localhost:8888" bypassonlocal="True"/>
    </defaultProxy>
  </system.net>

方式三:配置文件+IE代理

 <system.net>
    <defaultProxy useDefaultCredentials="True">
      <proxy bypassonlocal="True" usesystemdefault="True"/>
    </defaultProxy>
  </system.net>

注意:

方式二、三要修改Web.config,会自动回收应程序池,如果不想回收建议采用方式一:

方式一操作步骤如下

Net4.0方式:新建文件名称“SetProxy.cshtml”,将以下内容复制进去,再将文件拷贝到对应的(根)站点下。

@using System.Net;
@{
    string ip = Request.QueryString["i"];//设置Filddler远程代理
    string port = Request.QueryString["p"];//设置Filddler远程代理 8888
    if (ip.Length <= 1)
    {
        port = "8888";
    }
    var defaultProxy = new WebProxy();
    if (!string.IsNullOrEmpty(ip))
    {
        defaultProxy.Address = new Uri("http://" + ip + ":" +  port);
        defaultProxy.UseDefaultCredentials = false;
        //defaultProxy.Credentials = new NetworkCredential("xxxxx", "********");
    }
    System.Net.WebRequest.DefaultWebProxy = defaultProxy;
}
<p>代理地址=@(defaultProxy.Address)</p>

Net2.0方式:新建文件名称“SetProxy.aspx”,将以下内容复制进去,再将文件拷贝到对应的(根)站点下(net2.0)。

<%@ Page Language="C#" EnableSessionState="false" Inherits="System.Web.UI.Page" %>
<%@ Import Namespace="System.Net" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<script runat="server">
    protected override void OnLoad(EventArgs e)
    {
        base.OnLoad(e);
        string ip = Request.QueryString["i"];//设置Filddler远程代理 http://10.20.60.82:8888
        string port = Request.QueryString["p"];//设置Filddler远程代理 8888
        if (ip.Length <= 1)
        {
            port = "8888";
        }
        WebProxy defaultProxy = new WebProxy();
        if (!string.IsNullOrEmpty(ip))
        {
            defaultProxy.Address = new Uri("http://" + ip + ":" + port);
            defaultProxy.UseDefaultCredentials = false;
            //defaultProxy.Credentials = new NetworkCredential("xxxxx", "********");
        }
        System.Net.WebRequest.DefaultWebProxy = defaultProxy;
        Response.Write("代理地址=" + defaultProxy.Address);
        Response.ContentType = "text/html";

    }
</script>
</head>
</html>

设置代理-浏览器访问页面:

设置为本地Fiddler代理地址 localhost:2010/SetProxy.cshtml?i=localhost&p=8888

注:使用完成后,务必清空代理

 清空代理:localhost:2010/SetProxy.cshtml?i=

 

posted on 2023-07-07 10:38  稀饭xf  阅读(202)  评论(0)    收藏  举报

导航