我的网址字符串是
" 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);
}
}