Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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);
        }
    }
share|improve this question
1  
How are you assigning the URL to m_strUrl? Are you using the @ sign to make the string literal? If not it is trying to escape the commas \,. – Evan lewis yesterday
Thanks. Yes, I am using @ – toosensitive yesterday
Okay, can you please show us where you are assigning the URL to your m_strUrl variable? – Evan lewis yesterday

1 Answer

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";
share|improve this answer
Thanks, the strange thing is my string is "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", once I copy it in IE and click enter, the string is escaped by IE. Yes, I try to escape comma in with \, – toosensitive yesterday

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.