gpt4 book ai didi

c# - 如何使用 HttpWebRequest 使 C# 应用程序表现得像 fiddler

转载 作者:太空狗 更新时间:2023-10-29 21:19:25 26 4
gpt4 key购买 nike

我有一个控制台应用程序,它使用 20 个左右的线程连接到远程 Web 服务器并发送相当小的任意 http 请求,100% 通过 ssl。远程 Web 服务器实际上是一个完整的负载平衡数据中心,充满了每秒可以处理数十万个请求的高可用性系统。这不是服务器或带宽问题。话虽这么说,我不运行它,我也不会影响它的配置方式,所以即使我想更改服务器端也无法更改。

当使用 fiddler 运行应用程序时,应用程序的执行速度非常快。当不在 fiddler 中运行时,它真的慢得多,以至于对手头的任务毫无用处。它似乎也在过程的早期某个点锁定,但这可能只是一个死锁问题,我还不确定。

无论如何,fiddler 作为代理,无疑以某种方式修改我的请求/连接以确保出色的吞吐量,但我不知道它在做什么。我试图弄清楚,以便我可以强制我的 .net 应用程序模仿 fiddler 连接处理行为,而不必实际通过 fiddler 运行它

我已经在下面粘贴了连接代码。

     using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;

namespace Redacted
{
public class HiveCommunicator
{

public static IResponse SendRequest(IRequest request) {

ServicePointManager.DefaultConnectionLimit = 60;
ServicePointManager.Expect100Continue = false;


string hostUrlString = string.Empty;
if (request.SiteID <= 0)
hostUrlString = string.Format("{0}://{1}{2}", request.UseSSL ? "https" : "http", DataCenters.GetCenter(request.DataCenter), request.Path);
else
hostUrlString = string.Format("{0}://{1}{2}", request.UseSSL ? "https" : "http", DataCenters.GetCenter(request.DataCenter), string.Format(request.Path, request.SiteID));

HttpWebRequest webRequest = (HttpWebRequest)HttpWebRequest.Create(hostUrlString);

switch (request.ContentType)
{
default:
case ContentTypes.XML:
webRequest.ContentType = "application/xml";
break;
case ContentTypes.JSON:
webRequest.ContentType = "application/json";
break;
case ContentTypes.BINARY:
webRequest.ContentType = "application/octet-stream";
break;
}

if (request.RequiresAuthorizationToken)
{
AuthorizationToken tok = HiveAuthentication.GetToken(request.SiteID);
if (tok == null)
{
return null;
}
webRequest.Headers.Add(HttpRequestHeader.Authorization, tok.Token);
}

bool UsesRequestBody = true;

switch (request.HttpVerb)
{
case HttpVerbs.POST:
webRequest.Method = "POST";
break;
case HttpVerbs.DELETE:
webRequest.Method = "DELETE";
UsesRequestBody = false;
break;
case HttpVerbs.PUT:
webRequest.Method = "PUT";
break;
default:
case HttpVerbs.GET:
webRequest.Method = "GET";
UsesRequestBody = false;
break;
}

HttpWebResponse webResponse = null;
Stream webRequestStream = null;

byte[] webRequestBytes = null;
if (UsesRequestBody)
{
webRequestBytes = request.RequestBytes;
webRequest.ContentLength = webRequestBytes.Length;
webRequestStream = webRequest.GetRequestStream();
for (int i = 0; i < webRequest.ContentLength; i++)
{
webRequestStream.WriteByte(webRequestBytes[i]);
}
}

try
{
webResponse = (HttpWebResponse)webRequest.GetResponse();
}
catch (WebException ex)
{

webResponse = (HttpWebResponse)ex.Response;
}

if (UsesRequestBody)
{
webRequestStream.Close();
webRequestStream.Dispose();
}

IResponse respReturn = request.ParseResponse(webResponse);
webResponse.Close();

return respReturn;
}
}
}

最佳答案

我感谢这里试图提供帮助的人们。不幸的是,这需要调用 Microsoft 专业支持部门。

尽管我使用的是 ServicePointManager.Expect100Continue = false; 它发生在应用程序生命周期的后期。查看 System.Net.Trace 日志,我们发现 expect-100 continue header 仍在使用(使用 fiddler 时除外)。解决方案是将其放入应用程序启动中(在 Main() 中)

我还试图在关闭请求流之前读取响应流。

修复后,一切都加快了。该应用程序在没有 fiddler 的情况下运行速度比使用 fiddler 快得多,这是我所期望的。

一些人说要在 HttpWebResponse 上调用 dispose。该类没有公共(public) Dispose 方法。我假设 .Close() 在内部调用 .Dispose()。

关于c# - 如何使用 HttpWebRequest 使 C# 应用程序表现得像 fiddler,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6539034/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com