问题:特殊字符(特别是$和C# )导致了Web ASP.NET C#中的不一致行为。调用API后,结果有时是-
结果:{“消息”:“发生错误”}
".........//api/data?Message=Test%20For%20%245700.00%20at%20"
,测试价格为5700.00美元
我试过的
使用Uri.EscapeDataString(request);调用API的
都会通过。
问:是否有一种允许特殊字符通过API (就像字符串一样)的方法?即使包含了一个特殊的字符,也有任何方法来获取简单的字符串吗?下面是一些得到信息的代码-
public List<Results> Get(string Message)
{
//DO SOMETHING WITH THE MESSAGE AS A STRING & RETURN RESULTS
}还有,我是怎么叫API的-
//CALLING API STEPS
string url = ".....//api/data?Message=";
string message = "Test For $5700.00 at ";
string completeURL = url + Uri.EscapeDataString(message);
var client = new WebClient();
var content = client.DownloadString(completeURL);我在下面添加了一些额外的测试结果-
测试结果-通过
string url = ".....//api/data?Message=";
string message = "Test For $5700.00 at ";
message = message.Replace("$", "%24");//Adding this line before the URL-encoding fixes the problem
string completeURL = url + Uri.EscapeDataString(message);
var client = new WebClient();
var content = client.DownloadString(completeURL);
//PASS - COMPLETE URL: ".....//api/data?Message=Test%20for%20%25245700.00%20at%20"
//RESULT - API CALL SUCCESSFUL测试结果-失败
string url = ".....//api/data?Message=";
string message = "Test For $5700.00 at ";
string completeURL = url + Uri.EscapeDataString(message);
var client = new WebClient();
var content = client.DownloadString(completeURL);
//FAIL - COMPLETE URL: ".....//api/data?Message=Test%20for%20%245700.00%20at%20"
//RESULT - EXCEPTION -> "The remote server returned an error: (500) Internal Server Error."发布于 2022-05-27 21:23:15
感谢您添加测试结果:
//PASS - COMPLETE URL: ".....//api/data?Message=Test%20for%20%25245700.00%20at%20"
//RESULT - API CALL SUCCESSFUL
//FAIL - COMPLETE URL: ".....//api/data?Message=Test%20for%20%245700.00%20at%20"
//RESULT - EXCEPTION -> "The remote server returned an error: (500) Internal Server Error."似乎很清楚发生了什么:
在“成功”情况下,您要传递一个百分比符号("%",十六进制0x25),而值"245700.00".
我怀疑失败案例(HTTP错误500)和“成功”案例都是错误的。
我想你的意思是价值是"5700.00",而不是"245700.00“。
建议:
https://stackoverflow.com/questions/72390045
复制相似问题