【问题标题】:How to handle comma in url in c#如何在c#中处理url中的逗号
【发布时间】:2013-06-10 10:52:41
【问题描述】:

我的网址字符串是

"https://MyCom.Produect.App.ResourcePath/ts?Customer_Account=A B C\, LLC,D E F\, LLC&Billing_Code=11,12&fromDateTime=2013-05-13&toDateTime=2013-06-13"

如果我将它复制到 IE 中并运行,它会返回一些数据。 但是,c# 中的相同字符串给了我一个错误的请求异常。 这是我的 c# 代码,我想我必须错过一些东西。谢谢

    public void GetDataAsyn(string m_strUrl)
    {
        var username = m_strEmail;
        var password = m_strPassword;

        var uri = new Uri(m_strUrl);            

        var credentialCache = new CredentialCache();
        credentialCache.Add(uri, "Basic", new NetworkCredential(username, password));
        var httpRequest = (HttpWebRequest)WebRequest.Create(uri);
        httpRequest.Method = "GET";
        httpRequest.ContentType = "application/json";
        httpRequest.UseDefaultCredentials = true;
        httpRequest.Accept = Constants.CONTENT_TYPE_TEXT_CSV;
        httpRequest.UserAgent = Helper.GetUserAgent();
        Helper.SetProxyIfNeeded(httpRequest, uri);
        httpRequest.Headers.Add("Authorization", Helper.GetAuthHeader(username, password));

        ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };
        httpRequest.BeginGetResponse(GetResponseCallback, new object[] { httpRequest });
    }

    private void GetResponseCallback(IAsyncResult asyncResult)
    {
        try
        {
            var obj = (object[])asyncResult.AsyncState;
            var request = (HttpWebRequest)obj[0];
            var response = request.EndGetResponse(asyncResult);
            Stream responseStream = response.GetResponseStream();
            if (responseStream != null)
            {
                var streamReader = new StreamReader(responseStream);
                ReturnedData = streamReader.ReadToEnd();
            }

            ....do something with ReturnedData
        }
        catch (Exception ex)
        {
            Helper.LogError(ex);
        }
    }

【问题讨论】:

  • 如何将 URL 分配给 m_strUrl?您是否使用 @ 符号来制作字符串文字?如果不是,它正在尝试转义逗号\,
  • 谢谢。是的,我正在使用@
  • 好的,您能告诉我们您将 URL 分配给 m_strUrl 变量的位置吗?

标签: c# http url


【解决方案1】:

我将在这里暗中尝试,因为您没有向我们展示您如何将您的 URL 分配给 m_strUrl

您很可能收到错误请求,因为 C# 中的 \ 字符是转义字符。

要纠正这样的问题,您要么必须像这样 \\ 那样转义 \

或者更简洁的处理方法是使用@ 符号并将字符串设为文字。

如果您提供的示例中的 \ 是 url 的一部分(不是转义字符),则以下是您的文字 url 字符串:

    string m_strUrl =  @"https://MyCom.Produect.App.ResourcePath/ts?Customer_Account=A%20B%20C\,%20LLC,D%20E%20F\,%20LLC&Billing_Code=11,12&fromDateTime=2013-05-13&toDateTime=2013-06-13";

如果您已经尝试使用 \ 转义逗号,您的字符串将如下所示。

    string m_strUrl = @"https://MyCom.Produect.App.ResourcePath/ts?Customer_Account=A%20B%20C,%20LLC,D%20E%20F,%20LLC&Billing_Code=11,12&fromDateTime=2013-05-13&toDateTime=2013-06-13";

【讨论】:

  • 谢谢,奇怪的是我的字符串是“MyCom.Produect.App.ResourcePath/ts?Customer_Account=ABC\, LLC,DEF\, LLC&Billing_Code=11,12&fromDateTime=2013-05-13&toDateTime=2013-06-13”,一旦我复制到 IE 中点击回车,字符串被 IE 转义。是的,我尝试用 \, 转义逗号
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-09-26
  • 1970-01-01
  • 2013-04-05
  • 1970-01-01
  • 2017-10-22
  • 1970-01-01
  • 2013-07-09
相关资源
最近更新 更多