0

我的网址字符串是

" https://MyCom.Product.App.ResourcePath/ts?Customer_Account=A BC\, LLC,DEF\, 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);
        }
    }
4

1 回答 1

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";
于 2013-06-13T21:44:27.333 回答