简体   繁体   English

在C#中将双引号作为字符串传递的问题

[英]Issue with passing double-quotes as a string in c#

I need to pass the below path as URI. 我需要将以下路径作为URI传递。

 https://api.mytrade.com/oauth/accesstoken?grant_type=auth_code -d "code=DBvmp1o9"

I used the below solutions to implement escape charecter for the double-quotes which resulted in Internal Server error. 我使用以下解决方案为双引号实现转义符,这导致内部服务器错误。

Solution 1: string URI = "https://api.mytrade.com/oauth/accesstoken?grant_type=auth_code -d \\"code=" + accessCode + "\\""; 解决方案1: string URI = "https://api.mytrade.com/oauth/accesstoken?grant_type=auth_code -d \\"code=" + accessCode + "\\"";

Solution 2 (Verbatim string literal): string URI = @"https://api.mytrade.com/oauth/accesstoken?grant_type=auth_code -d ""code=" + accessCode + ""; 解决方案2(逐字字符串文字): string URI = @"https://api.mytrade.com/oauth/accesstoken?grant_type=auth_code -d ""code=" + accessCode + ""; Any help is greatly appreciated. 任何帮助是极大的赞赏。

如果您使用转义符,则不应使用双“ //” ..尝试使用单“ /” ..它将是有效的..或在双引号之前仅使用@。

I tried the code you provide, and the result is: https://api.mytrade.com/oauth/accesstoken?grant_type=auth_code -d "code=DBvmp1o9 我尝试了您提供的代码,结果是: https ://api.mytrade.com/oauth/accesstoken?grant_type=auth_code -d“ code = DBvmp1o9

Lacking double-quotes at the end. 末尾缺少双引号。 I bet that because the last double-quotes wasnt escaped by verbatim. 我敢打赌,因为最后的双引号没有被逐字转义。 +"" considered empty string instead of double-quotes. +""被认为是空字符串,而不是双引号。 Your solution 2 should be: 您的解决方案2应该是:

string URI = @"https://api.mytrade.com/oauth/accesstoken?grant_type=auth_code -d ""code=" + accessCode + @"""";

Or 要么

string URI = string.Format(@"https://api.mytrade.com/oauth/accesstoken?grant_type=auth_code -d ""code={0}""", accessCode);

Given the accessCode hardcoded to "DBvmp1o9", the output of both code as i tested should be: https://api.mytrade.com/oauth/accesstoken?grant_type=auth_code -d "code=DBvmp1o9" 给定accessCode硬编码为“ DBvmp1o9”,我测试的两个代码的输出应为: https ://api.mytrade.com/oauth/accesstoken?grant_type=auth_code -d“ code = DBvmp1o9”

This is my best shot. 这是我最好的镜头。 If you get the same output but still getting the Internal Server Error, then you can go with implementing using uri-escape as you said. 如果您获得相同的输出,但仍然收到内部服务器错误,则可以按照您所说的使用uri-escape进行实现。

我希望使用带转义双引号的逐字字符串:

string URI = @"https://api.mytrade.com/oauth/accesstoken?grant_type=auth_code -d \""code=accessCode\""";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM