繁体   English   中英

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

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

我需要将以下路径作为URI传递。

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

我使用以下解决方案为双引号实现转义符,这导致内部服务器错误。

解决方案1: 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 + ""; 任何帮助是极大的赞赏。

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

我尝试了您提供的代码,结果是: https ://api.mytrade.com/oauth/accesstoken?grant_type=auth_code -d“ code = DBvmp1o9

末尾缺少双引号。 我敢打赌,因为最后的双引号没有被逐字转义。 +""被认为是空字符串,而不是双引号。 您的解决方案2应该是:

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

要么

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

给定accessCode硬编码为“ DBvmp1o9”,我测试的两个代码的输出应为: https ://api.mytrade.com/oauth/accesstoken?grant_type=auth_code -d“ code = DBvmp1o9”

这是我最好的镜头。 如果您获得相同的输出,但仍然收到内部服务器错误,则可以按照您所说的使用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