本文介绍了见HttpWebRequest的作为字符串的GetResponse之前没有使用招的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我怎么能看到HttpWebRequest对象的字符串调用GetResponse方法之前?我希望看到的要求是这样的RAW格式作为小提琴手:

 内容类型:多部分/格式数据;边界= --------------------------- 2600251021003
内容长度:338
----------------------------- 2600251021003内容处置:表格数据; NAME =UPLOAD_FILEName;文件名=Searchlight062210W¯¯price.csv内容类型:应用程序/ vnd.ms-Excel中
,,,,,
----------------------------- 2600251021003
内容处置:表格数据; NAME =提交
提交
----------------------------- 2600251021003--
 

我试着以下code,但没有奏效,因为流是无法读取。

 字符串GetRequestString(HttpWebRequest的REQ)
        {
            流流2 = req.GetRequestStream();
            StreamReader的reader2 =新的StreamReader(流2);
            返回reader2.ReadToEnd();

        }
 

解决方案

如果是为了记录的目的,您可以通过激活把这个在你的应用程序/ web.config中跟踪:

 < System.Diagnostics程序>
    <源>
      <信源名=的System.Net.SocketsTRACEMODE =protocolonly>
        <听众>
          <添加名称=的System.Net.SocketsTYPE =System.Diagnostics.TextWriterTraceListenerinitializeData =network.log/>
        < /听众>
      < /源>
    < /来源>

    <开关>
      <添加名称=的System.Net.Sockets值=松散/&G​​T;
    < /开关>

    <跟踪自动冲洗=真/>
  < /system.diagnostics>
 

运行您的code和查看生成的日志文件。

How can i see HttpWebRequest object as string before calling GetResponse method? I want to see raw format of request something like this as in fiddler:

Content-Type: multipart/form-data; boundary=---------------------------2600251021003 
Content-Length: 338 
-----------------------------2600251021003 Content-Disposition: form-data; name="UPLOAD_FILEName"; filename="Searchlight062210 w price.csv" Content-Type: application/vnd.ms-excel 
,,,,, 
-----------------------------2600251021003 
Content-Disposition: form-data; name="submit" 
submit 
-----------------------------2600251021003-- 

I tried following code, but not worked because stream is not readable.

 string GetRequestString(HttpWebRequest req)
        {
            Stream stream2 = req.GetRequestStream(); 
            StreamReader reader2 = new StreamReader(stream2);
            return reader2.ReadToEnd();  

        }
解决方案

If it is for logging purposes you could activate tracing by putting this in your app/web.config:

  <system.diagnostics>
    <sources>
      <source name="System.Net.Sockets" tracemode="protocolonly">
        <listeners>
          <add name="System.Net.Sockets" type="System.Diagnostics.TextWriterTraceListener" initializeData="network.log" />
        </listeners>
      </source>
    </sources>

    <switches>
      <add name="System.Net.Sockets" value="Verbose"/>
    </switches>

    <trace autoflush="true" />
  </system.diagnostics>

Run your code and look at the generated log file.

这篇关于见HttpWebRequest的作为字符串的GetResponse之前没有使用招的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-22 14:47