如何将请求 headers 输出到 C# 中的字符串
How to output request headers to a string in C#
我想为字符串的删除请求添加两个自定义 header。
我有以下代码
Dictionary<string, string> customHeaders = new Dictionary<string, string>();
customHeaders.Add("If-Match", passedEtag);
customHeaders.Add("context_study_site", "edc42643-27b5-428e-b8ae-2fa19bfb457a");
我试过以下方法
StringBuilder sb = new StringBuilder();
sb.Append(Environment.NewLine);
sb.Append(Environment.NewLine + "Context headers passed in the request" + Environment.NewLine);
Dictionary<string, string> customHeaders = new Dictionary<string, string>();
customHeaders.Add("If-Match", passedEtag);
customHeaders.Add("context_study_site", "edc42643-27b5-428e-b8ae-2fa19bfb457a");
string test = customHeaders.ToString();
File.WriteAllText("C:\Testing", "headersoutput.txt");
但我得到以下信息
System.Collections.Generic.Dictionary`2[System.String,System.String]
System.Collections.Generic.Dictionary`2[System.String,System.String]
而不是两个 header 键值对。
非常感谢任何想法。
提前致谢。
您需要使用 JsonConvert.SerializeObjec()
Serialize
而不是 .ToString()
var jsonDict = JsonConvert.SerializeObject( myDictionary );
然后写入文件,
File.WriteAllText(@"C:\Testing\headersoutput.txt", jsonDict);
为什么您的文件中出现 System.Collections.Generic.Dictionary2[System.String,System.String]
?
- 您正在尝试将
Dictionary
转换为字符串,使用
.ToString()
方法。你没有覆盖它的基本功能,
所以它调用了 .ToString()
方法的基本实现
在 Object
class.
Returns a string that represents the current object.
MSDN: Basic implementation of Object.ToString()
我们是怎么解决的?
- 我们使用了
JsonConvert.SerializeObject()
方法
Newtonsoft.Json
库,它转换 object
(在你的情况下
它是一本字典),到 json 字符串
Serializes the specified object to a JSON string.
我想为字符串的删除请求添加两个自定义 header。
我有以下代码
Dictionary<string, string> customHeaders = new Dictionary<string, string>();
customHeaders.Add("If-Match", passedEtag);
customHeaders.Add("context_study_site", "edc42643-27b5-428e-b8ae-2fa19bfb457a");
我试过以下方法
StringBuilder sb = new StringBuilder();
sb.Append(Environment.NewLine);
sb.Append(Environment.NewLine + "Context headers passed in the request" + Environment.NewLine);
Dictionary<string, string> customHeaders = new Dictionary<string, string>();
customHeaders.Add("If-Match", passedEtag);
customHeaders.Add("context_study_site", "edc42643-27b5-428e-b8ae-2fa19bfb457a");
string test = customHeaders.ToString();
File.WriteAllText("C:\Testing", "headersoutput.txt");
但我得到以下信息
System.Collections.Generic.Dictionary`2[System.String,System.String]
System.Collections.Generic.Dictionary`2[System.String,System.String]
而不是两个 header 键值对。
非常感谢任何想法。 提前致谢。
您需要使用 JsonConvert.SerializeObjec()
Serialize
而不是 .ToString()
var jsonDict = JsonConvert.SerializeObject( myDictionary );
然后写入文件,
File.WriteAllText(@"C:\Testing\headersoutput.txt", jsonDict);
为什么您的文件中出现 System.Collections.Generic.Dictionary2[System.String,System.String]
?
- 您正在尝试将
Dictionary
转换为字符串,使用.ToString()
方法。你没有覆盖它的基本功能, 所以它调用了.ToString()
方法的基本实现 在Object
class.
Returns a string that represents the current object.
MSDN: Basic implementation of Object.ToString()
我们是怎么解决的?
- 我们使用了
JsonConvert.SerializeObject()
方法Newtonsoft.Json
库,它转换object
(在你的情况下 它是一本字典),到 json 字符串
Serializes the specified object to a JSON string.