【问题标题】:issue with post with string containg & in c#C#中包含&的字符串的帖子问题
【发布时间】:2018-03-05 15:23:19
【问题描述】:

我正在尝试向 url 发布一个帖子,该帖子应如下图所示。

提琴手的正确帖子

问题发生:当我发布它时,它从 & (与号)溢出并为提琴手正文中的键值创建单独的条目。

我在提琴手的帖子

我应该怎么做才能出现在单键值对中?

我正在尝试发布以下数据:

responseData = "paymentid=100131806470125606&result=NOT+CAPTURED&auth=138601&ref=806420053674&tranid=131806429899386&postdate=0305&trackid=100001182618838&udf1=1&udf2=2&udf3=3&udf4=4&udf5=5&amt=5190.0&authRespCode=51"


        using (WebClient wc = new WebClient())
        {
            wc.Headers[HttpRequestHeader.ContentType] = "application/x-www-form-urlencoded";
            string HtmlResult = wc.UploadString("https://example.com/test.php", "authRespCode=1&responseData = " + responseData);
        }

【问题讨论】:

  • 你应该做的是发布minimal reproducible example,因为我们无法猜测你的代码是什么样子
  • 不,你必须edit你的问题。注释不是代码
  • 我已经编辑了添加静态字符串的问题,我使用 webclient 发布。
  • 我已经解决了..在下面发布答案感谢大家的支持。

标签: c# webclient


【解决方案1】:

你可以使用HttpUtility.ParseQueryString:

var res = HttpUtility.ParseQueryString(responseData);

然后你可以像这样访问你的参数:

res["paymentid"]

等等……

【讨论】:

  • 我需要在正文中而不是在查询字符串中发送上述字符串。
  • 没关系 - 该方法解析字符串。也许去尝试它和undersytand更多。甚至可能进入调试模式!?
  • 这个问题看起来与五分钟前完全不同......!
【解决方案2】:

添加这一行解决了我的问题:

responseData = responseData.Replace("&", "&");
responseData = HttpUtility.UrlEncode(responseData);

完整代码:

string responseData = Utilitycl.GetHtmlInputValue(getString, "responseData", Utilitycl.ELEMENTTYPE.INPUT);
responseData = responseData.Replace("&", "&");
responseData = HttpUtility.UrlEncode(responseData);
using (WebClient wc = new WebClient())
{
    wc.Headers[HttpRequestHeader.ContentType] = "application/x-www-form-urlencoded";
    string HtmlResult = wc.UploadString("https://example.com/test.php", "authRespCode=1&responseData = " + responseData);
}

【讨论】:

    【解决方案3】:

    您需要转义您的 URI 字符串:

    string HtmlResult = wc.UploadString("https://example.com/test.php", "authRespCode=1&responseData = " + Uri.EscapeUriString(responseData));
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-12-20
      相关资源
      最近更新 更多