Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/security/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Json c“httpcontent”;400-错误请求“;如果字符串包含换行符_Json_Wpf_String_Rest_Special Characters - Fatal编程技术网

Json c“httpcontent”;400-错误请求“;如果字符串包含换行符

Json c“httpcontent”;400-错误请求“;如果字符串包含换行符,json,wpf,string,rest,special-characters,Json,Wpf,String,Rest,Special Characters,我在与.Net WPF应用程序(.Net 4.5)中的restful服务通信时遇到了一些问题,尤其是在发送带有一些json数据的“PUT”请求时 仅供参考:restful服务正在Python Flask下运行 我使用以下方法向restful服务发送请求: HttpClient http = new HttpClient(); http.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", enco

我在与.Net WPF应用程序(.Net 4.5)中的restful服务通信时遇到了一些问题,尤其是在发送带有一些json数据的“PUT”请求时

仅供参考:restful服务正在Python Flask下运行

我使用以下方法向restful服务发送请求:

HttpClient http = new HttpClient();
http.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", encodedCredentials);
http.Timeout = TimeSpan.FromSeconds(1);
// Add an Accept header for JSON format.
http.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
HttpContent content = new StringContent(jDataString, Encoding.UTF8, "application/json");
当我提交通常的字符串时,一切正常。但是一旦字符串包含换行符,我就会遇到问题

使用:

mytring.Replace("\r", "").Replace("\n", "") 
如果成功,我的字符串将被restful服务接受

不幸的是,这是不可接受的,因为我希望能够检索换行符

因此,我尝试了以下方法:

mytring.Replace("\n", "\\n").Replace("\r", "\\r")
或者甚至用一个内部字符来确保我能识别图案:

mytring.Replace("\n", "\\+n").Replace("\r", "\\+r")
在这两种情况下,我解析的字符串看起来都不错,但restful服务不接受

下面两个例子-接受第一个版本,而不是第二个和第三个

"XML_FIELD": "<IDs><Id Type=\"System.Int32\" Value=\"7\" /></IDs>"
"XML_FIELD": "<IDs>\r\n<Id Type=\"System.Int32\" Value=\"20\" />\r\n</IDs>"
"XML_FIELD": "<IDs>\+r\+n<Id Type=\"System.Int32\" Value=\"20\" />\+r\+n</IDs>"
“XML\u字段”:
“XML\u字段”:“\r\n\r\n”
“XML\U字段”:“\+r\+n\+r\+n”
提前感谢您的帮助!! 问候

好的,明白了

问题来自“\r\n”字符,该字符直接来自我的数据库

无论如何,要执行的更改是针对序列化的

mySerializedString.Replace("\r\n", "\n")
                  .Replace("\n", "\\n")
                  .Replace("\r", "\\r")
                  .Replace("\'", "\\'")
                  .Replace("\"", "\\\"")
                  .Replace("\t", "\\t")
                  .Replace("\b", "\\b")
                  .Replace("\f", "\\f");
要反序列化,请执行相反的操作:

myDeSerializedString.Replace("\\n", "\n")
                    .Replace("\\r", "\r")
                    .Replace("\\'", "\'")
                    .Replace("\\\"", "\"")
                    .Replace("\\t", "\t")
                    .Replace("\\b", "\b")
                    .Replace("\\f", "\f");
注意:在此过程中,我们将丢失“\r\n”字符(替换为“\n”)