toosensitive
toosensitive

Reputation: 2375

How to handle comma in url in c#

My url string is

"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"

If I copy it in IE and run, it will return some data. However the same string in c# gives me a bad request exception. Here is my c# code for this, I think I must miss something. Thanks

    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);
        }
    }

Upvotes: 0

Views: 1198

Answers (1)

Evan L
Evan L

Reputation: 3855

I am going to take a shot in the dark here as you didn't show us how you are assigning your URL to m_strUrl.

Most likely you are getting the bad request because a \ character in C# is an escape.

To correct problems like this you either have to escape the \ like this \\

Or a much cleaner way to handle this would be to use the @ sign and make the string a literal.

If the \ in the example you provided are part of the url (not escape characters) the following is your literal url string:

    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";

If you are already trying to escape the commas with the \ your string would look like this.

    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";

Upvotes: 1

Related Questions