有没有什么好的方法(除了繁琐地查询每个属性)在C#中为HttpWebRequest构建诊断转储字符串?对于简单的对象,可以使用new JObject(theObject)
来逃避,但这对HttpWebRequest不起作用(当然,toString
在HttpWebRequest上没有用,因为它在任何C#对象上都是无用的。 p>
对于C#对象的任何子集,是否有任何一般方法可以执行此操作(除了我已经提到的new JObject
之外)?
更新:我发现使用JsonConvert.SerializeObject(Hans的链接中的一个建议)可以很好地转储HttpWebRequest(尽管它没有获取请求流)。我有点使System.Net.Tracing的东西工作(虽然文档,像往常一样的.NET东西,很糟糕)。
答案 0 :(得分:8)
结果很好:
string httpWebRequestDump(HttpWebRequest hwr)
{
return JsonConvert.SerializeObject(hwr,Formatting.Indented);
}
唯一的问题是它不会转储关联的请求流。我还没有找到从请求中提取的方法。
答案 1 :(得分:3)
作为快速回复,我会说通过创建自己的变体类来覆盖ToString()
函数,唯一的区别是ToString()
方法;例如:
class HttpWebRequest2 : HttpWebRequest
{
public HttpWebRequest2(System.Runtime.Serialization.SerializationInfo SerializationInfo, System.Runtime.Serialization.StreamingContext StreamingContext)
: base(SerializationInfo, StreamingContext) { }
public override string ToString()
{
// Edit ToString() code here.
// For example:
string retval = "[Accept] = " + Accept.ToString()
+ "\r\n[Address] = " + Address.ToString()
+ "\r\n[AllowAutoRedirect] = " + AllowAutoRedirect.ToString()
+ "\r\n[AllowReadStreamBuffering] = " + AllowReadStreamBuffering.ToString()
+ "\r\n[AllowWriteStreamBuffering] = " + AllowWriteStreamBuffering.ToString()
+ "\r\n[AuthenticationLevel] = " + AuthenticationLevel.ToString()
+ "\r\n[AutomaticDecompression] = " + AutomaticDecompression.ToString()
+ "\r\n[CachePolicy] = " + CachePolicy.ToString()
+ "\r\n[ClientCertificates] = " + ClientCertificates.ToString()
+ "\r\n[Connection] = " + Connection.ToString()
+ "\r\n[ConnectionGroupName] = " + ConnectionGroupName.ToString()
+ "\r\n[ContentLength] = " + ContentLength.ToString()
+ "\r\n[ContentType] = " + ContentType.ToString()
+ "\r\n[ContinueDelegate] = " + ContinueDelegate.ToString()
+ "\r\n[ContinueTimeout] = " + ContinueTimeout.ToString()
+ "\r\n[CookieContainer] = " + CookieContainer.ToString()
+ "\r\n[CreatorInstance] = " + CreatorInstance.ToString()
+ "\r\n[Credentials] = " + Credentials.ToString()
+ "\r\n[Date] = " + Date.ToString()
+ "\r\n[DefaultCachePolicy] = " + DefaultCachePolicy.ToString()
+ "\r\n[DefaultMaximumErrorResponseLength] = " + DefaultMaximumErrorResponseLength.ToString()
+ "\r\n[DefaultMaximumResponseHeadersLength] = " + DefaultMaximumResponseHeadersLength.ToString()
+ "\r\n[Expect] = " + Expect.ToString()
+ "\r\n[HaveResponse] = " + HaveResponse.ToString()
+ "\r\n[Headers] = " + Headers.ToString()
+ "\r\n[Host] = " + Host.ToString()
+ "\r\n[IfModifiedSince] = " + IfModifiedSince.ToString()
+ "\r\n[ImpersonationLevel] = " + ImpersonationLevel.ToString()
+ "\r\n[KeepAlive] = " + KeepAlive.ToString()
+ "\r\n[MaximumAutomaticRedirections] = " + MaximumAutomaticRedirections.ToString()
+ "\r\n[MaximumResponseHeadersLength] = " + MaximumResponseHeadersLength.ToString()
+ "\r\n[MediaType] = " + MediaType.ToString()
+ "\r\n[Method] = " + Method.ToString()
+ "\r\n[Pipelined] = " + Pipelined.ToString()
+ "\r\n[PreAuthenticate] = " + PreAuthenticate.ToString()
+ "\r\n[ProtocolVersion] = " + ProtocolVersion.ToString()
+ "\r\n[Proxy] = " + Proxy.ToString()
+ "\r\n[ReadWriteTimeout] = " + ReadWriteTimeout.ToString()
+ "\r\n[Referer] = " + Referer.ToString()
+ "\r\n[RequestUri] = " + RequestUri.ToString()
+ "\r\n[SendChunked] = " + SendChunked.ToString()
+ "\r\n[ServerCertificateValidationCallback] = " + ServerCertificateValidationCallback.ToString()
+ "\r\n[ServicePoint] = " + ServicePoint.ToString()
+ "\r\n[SupportsCookieContainer] = " + SupportsCookieContainer.ToString()
+ "\r\n[Timeout] = " + Timeout.ToString()
+ "\r\n[TransferEncoding] = " + TransferEncoding.ToString()
+ "\r\n[UnsafeAuthenticatedConnectionSharing] = " + UnsafeAuthenticatedConnectionSharing.ToString()
+ "\r\n[UseDefaultCredentials] = " + UseDefaultCredentials.ToString()
+ "\r\n[UserAgent] = " + UserAgent.ToString();
return retval;
}
}
对于请求流,如何将其复制到内存流?沿着:
int count = 0;
do
{
count = responseStream.Read(buffer, 0, buffer.Length);
memoryStream.Write(buffer, 0, count);
Response.BinaryWrite(buffer);
}
while (count != 0);
result = memoryStream.ToArray(); // or ToString() etc. as it takes you fancy/suits your needs :)
这样您就可以单独保留原始请求流,并根据需要使用内存流中的副本进行打印/存储等。
这将返回一个字符串,其中包含HttpWebRequest
中可以包含getter的所有值:)
你所要做的就是使用HttpWebRequest2()
而不是它应该没问题。)
如果这有帮助,或者如果您需要更多信息/不同的方式,请告诉我:)
答案 2 :(得分:2)
我比GMasucci的答案更好,因为序列化没有提取标题的值。
我修改了GMasucci的答案,成为一个扩展方法,并使用StringBuilder和AppendFormat,以便空值不会被炸毁,所以如果你对这个答案进行投票,请向他投票。我也评论了似乎被弃用的属性:
public static String ToStringReadable(this HttpWebRequest request)
{
StringBuilder sb = new StringBuilder();
sb.AppendFormat("[Accept] = {0}", request.Accept);
sb.AppendFormat("{0}[Address] = {1}", Environment.NewLine, request.Address);
sb.AppendFormat("{0}[AllowAutoRedirect] = {1}", Environment.NewLine, request.AllowAutoRedirect);
//sb.AppendFormat("{0}[AllowReadStreamBuffering] = {1}", Environment.NewLine, request.AllowReadStreamBuffering);
sb.AppendFormat("{0}[AllowWriteStreamBuffering] = {1}", Environment.NewLine, request.AllowWriteStreamBuffering);
sb.AppendFormat("{0}[AuthenticationLevel] = {1}", Environment.NewLine, request.AuthenticationLevel);
sb.AppendFormat("{0}[AutomaticDecompression] = {1}", Environment.NewLine, request.AutomaticDecompression);
sb.AppendFormat("{0}[CachePolicy] = {1}", Environment.NewLine, request.CachePolicy);
sb.AppendFormat("{0}[ClientCertificates] = {1}", Environment.NewLine, request.ClientCertificates);
sb.AppendFormat("{0}[Connection] = {1}", Environment.NewLine, request.Connection);
sb.AppendFormat("{0}[ConnectionGroupName] = {1}", Environment.NewLine, request.ConnectionGroupName);
sb.AppendFormat("{0}[ContentLength] = {1}", Environment.NewLine, request.ContentLength);
sb.AppendFormat("{0}[ContentType] = {1}", Environment.NewLine, request.ContentType);
sb.AppendFormat("{0}[ContinueDelegate] = {1}", Environment.NewLine, request.ContinueDelegate);
//sb.AppendFormat("{0}[ContinueTimeout] = {1}", Environment.NewLine, request.ContinueTimeout);
sb.AppendFormat("{0}[CookieContainer] = {1}", Environment.NewLine, request.CookieContainer);
//sb.AppendFormat("{0}[CreatorInstance] = {1}", Environment.NewLine, request.CreatorInstance);
sb.AppendFormat("{0}[Credentials] = {1}", Environment.NewLine, request.Credentials);
//sb.AppendFormat("{0}[Date] = {1}", Environment.NewLine, request.Date);
//sb.AppendFormat("{0}[DefaultCachePolicy] = {1}", Environment.NewLine, request.DefaultCachePolicy);
//sb.AppendFormat("{0}[DefaultMaximumErrorResponseLength] = {1}", Environment.NewLine, request.DefaultMaximumErrorResponseLength);
//sb.AppendFormat("{0}[DefaultMaximumResponseHeadersLength] = {1}", Environment.NewLine, request.DefaultMaximumResponseHeadersLength);
sb.AppendFormat("{0}[Expect] = {1}", Environment.NewLine, request.Expect);
sb.AppendFormat("{0}[HaveResponse] = {1}", Environment.NewLine, request.HaveResponse);
sb.AppendFormat("{0}[Headers] = {1}", Environment.NewLine, request.Headers);
//sb.AppendFormat("{0}[Host] = {1}", Environment.NewLine, request.Host);
sb.AppendFormat("{0}[IfModifiedSince] = {1}", Environment.NewLine, request.IfModifiedSince);
sb.AppendFormat("{0}[ImpersonationLevel] = {1}", Environment.NewLine, request.ImpersonationLevel);
sb.AppendFormat("{0}[KeepAlive] = {1}", Environment.NewLine, request.KeepAlive);
sb.AppendFormat("{0}[MaximumAutomaticRedirections] = {1}", Environment.NewLine, request.MaximumAutomaticRedirections);
sb.AppendFormat("{0}[MaximumResponseHeadersLength] = {1}", Environment.NewLine, request.MaximumResponseHeadersLength);
sb.AppendFormat("{0}[MediaType] = {1}", Environment.NewLine, request.MediaType);
sb.AppendFormat("{0}[Method] = {1}", Environment.NewLine, request.Method);
sb.AppendFormat("{0}[Pipelined] = {1}", Environment.NewLine, request.Pipelined);
sb.AppendFormat("{0}[PreAuthenticate] = {1}", Environment.NewLine, request.PreAuthenticate);
sb.AppendFormat("{0}[ProtocolVersion] = {1}", Environment.NewLine, request.ProtocolVersion);
sb.AppendFormat("{0}[Proxy] = {1}", Environment.NewLine, request.Proxy);
sb.AppendFormat("{0}[ReadWriteTimeout] = {1}", Environment.NewLine, request.ReadWriteTimeout);
sb.AppendFormat("{0}[Referer] = {1}", Environment.NewLine, request.Referer);
sb.AppendFormat("{0}[RequestUri] = {1}", Environment.NewLine, request.RequestUri);
sb.AppendFormat("{0}[SendChunked] = {1}", Environment.NewLine, request.SendChunked);
//sb.AppendFormat("{0}[ServerCertificateValidationCallback] = {1}", Environment.NewLine, request.ServerCertificateValidationCallback);
sb.AppendFormat("{0}[ServicePoint] = {1}", Environment.NewLine, request.ServicePoint);
//sb.AppendFormat("{0}[SupportsCookieContainer] = {1}", Environment.NewLine, request.SupportsCookieContainer);
sb.AppendFormat("{0}[Timeout] = {1}", Environment.NewLine, request.Timeout);
sb.AppendFormat("{0}[TransferEncoding] = {1}", Environment.NewLine, request.TransferEncoding);
sb.AppendFormat("{0}[UnsafeAuthenticatedConnectionSharing] = {1}", Environment.NewLine, request.UnsafeAuthenticatedConnectionSharing);
sb.AppendFormat("{0}[UseDefaultCredentials] = {1}", Environment.NewLine, request.UseDefaultCredentials);
sb.AppendFormat("{0}[UserAgent] = {1}", Environment.NewLine, request.UserAgent);
return sb.ToString();
}
答案 3 :(得分:0)
我修改了GMasucci和jaybro的答案以使用HttpRequest,以用于使用它代替HttpWebRequest的情况。
public static String ToStringReadable (HttpRequest request)
{
StringBuilder sb = new StringBuilder();
sb.AppendFormat("{0}[AcceptTypes] = {1}", Environment.NewLine, request.AcceptTypes);
sb.AppendFormat("{0}[AnonymousID] = {1}", Environment.NewLine, request.AnonymousID);
sb.AppendFormat("{0}[ApplicationPath] = {1}", Environment.NewLine, request.ApplicationPath);
sb.AppendFormat("{0}[AppRelativeCurrentExecutionFilePath] = {1}", Environment.NewLine, request.AppRelativeCurrentExecutionFilePath);
sb.AppendFormat("{0}[Browser] = {1}", Environment.NewLine, request.Browser);
sb.AppendFormat("{0}[ClientCertificate] = {1}", Environment.NewLine, request.ClientCertificate);
sb.AppendFormat("{0}[ContentEncoding] = {1}", Environment.NewLine, request.ContentEncoding);
sb.AppendFormat("{0}[ContentLength] = {1}", Environment.NewLine, request.ContentLength);
sb.AppendFormat("{0}[ContentType] = {1}", Environment.NewLine, request.ContentType);
sb.AppendFormat("{0}[Cookies] = {1}", Environment.NewLine, request.Cookies);
sb.AppendFormat("{0}[CurrentExecutionFilePath] = {1}", Environment.NewLine, request.CurrentExecutionFilePath);
sb.AppendFormat("{0}[CurrentExecutionFilePathExtension] = {1}", Environment.NewLine, request.CurrentExecutionFilePathExtension);
sb.AppendFormat("{0}[FilePath] = {1}", Environment.NewLine, request.FilePath);
sb.AppendFormat("{0}[Files] = {1}", Environment.NewLine, request.Files);
sb.AppendFormat("{0}[Filter] = {1}", Environment.NewLine, request.Filter);
sb.AppendFormat("{0}[Form] = {1}", Environment.NewLine, request.Form);
sb.AppendFormat("{0}[Headers] = {1}", Environment.NewLine, request.Headers);
sb.AppendFormat("{0}[HttpChannelBinding] = {1}", Environment.NewLine, request.HttpChannelBinding);
sb.AppendFormat("{0}[HttpMethod] = {1}", Environment.NewLine, request.HttpMethod);
sb.AppendFormat("{0}[InputStream] = {1}", Environment.NewLine, request.InputStream);
sb.AppendFormat("{0}[IsAuthenticated] = {1}", Environment.NewLine, request.IsAuthenticated);
sb.AppendFormat("{0}[IsLocal] = {1}", Environment.NewLine, request.IsLocal);
sb.AppendFormat("{0}[IsSecureConnection] = {1}", Environment.NewLine, request.IsSecureConnection);
sb.AppendFormat("{0}[Params] = {1}", Environment.NewLine, request.Params);
sb.AppendFormat("{0}[Path] = {1}", Environment.NewLine, request.Path);
sb.AppendFormat("{0}[PathInfo] = {1}", Environment.NewLine, request.PathInfo);
sb.AppendFormat("{0}[PhysicalApplicationPath] = {1}", Environment.NewLine, request.PhysicalApplicationPath);
sb.AppendFormat("{0}[PhysicalPath] = {1}", Environment.NewLine, request.PhysicalPath);
sb.AppendFormat("{0}[QueryString] = {1}", Environment.NewLine, request.QueryString);
sb.AppendFormat("{0}[RawUrl] = {1}", Environment.NewLine, request.RawUrl);
sb.AppendFormat("{0}[ReadEntityBodyMode] = {1}", Environment.NewLine, request.ReadEntityBodyMode);
sb.AppendFormat("{0}[RequestContext] = {1}", Environment.NewLine, request.RequestContext);
sb.AppendFormat("{0}[RequestType] = {1}", Environment.NewLine, request.RequestType);
sb.AppendFormat("{0}[ServerVariables] = {1}", Environment.NewLine, request.ServerVariables);
sb.AppendFormat("{0}[TimedOutToken] = {1}", Environment.NewLine, request.TimedOutToken);
sb.AppendFormat("{0}[TlsTokenBindingInfo] = {1}", Environment.NewLine, request.TlsTokenBindingInfo);
sb.AppendFormat("{0}[TotalBytes] = {1}", Environment.NewLine, request.TotalBytes);
sb.AppendFormat("{0}[Unvalidated] = {1}", Environment.NewLine, request.Unvalidated);
sb.AppendFormat("{0}[Url] = {1}", Environment.NewLine, request.Url);
sb.AppendFormat("{0}[UrlReferrer] = {1}", Environment.NewLine, request.UrlReferrer);
sb.AppendFormat("{0}[UserAgent] = {1}", Environment.NewLine, request.UserAgent);
sb.AppendFormat("{0}[UserHostAddress] = {1}", Environment.NewLine, request.UserHostAddress);
sb.AppendFormat("{0}[UserHostName] = {1}", Environment.NewLine, request.UserHostName);
sb.AppendFormat("{0}[UserLanguages] = {1}", Environment.NewLine, request.UserLanguages);
return sb.ToString();
}
答案 4 :(得分:0)
以上解决方案是很多代码,此外,您还必须做很多事情
以使该代码在生产环境中长期运行。
您可能还需要解决许多其他问题
仅使用Newtonsoft.Json.Linq.JObject
解决方案:
另外,您始终想记录更多信息,例如:
有最好的解决方案,可以解决所有这些问题,甚至更多。
使用以下Nuget软件包:Desharp。
它用于网络和桌面应用程序。非常上瘾。
看到它是 Desharp Github documentation 。它具有许多配置选项。
随时随地打电话:
Desharp.Debug.Log(yourRequestInstance);
更多:
Desharp.Debug.Log(anyException);
Desharp.Debug.Log(anyCustomValueObject);
Desharp.Debug.Log(anyNonserializableObject);
Desharp.Debug.Log(anyFunc);
Desharp.Debug.Log(anything, Desharp.Level.EMERGENCY); // you can store into different files
Desharp.Debug.Dump(anything); // you can dump into browser floating bar
有一些网络演示:
尝试检出它们中的任何一个以及可以转储或登录HDD的可能性:
我相信这会有所帮助。