public static string Method_Name(
string requestMethod,
string requestUrl,
string requestBody = null,
Dictionary<string, string> requestHeader = null,
string requestUserAgent = null,
Boolean isContentType = false)
{
string responseString = String.Empty;
try
{
Uri requestUri = new Uri(requestUrl);
WebRequest request = WebRequest.Create(requestUri);
request.Timeout = 60000;
if (requestHeader != null)
{
foreach (string key in requestHeader.Keys)
{
request.Headers.Add(key, requestHeader[key]);
}
}
request.Headers.Add("X-FD-TrustLevel", "trusted");
request.Headers.Add("X-FD-Version", "1.0");
request.Method = requestMethod;
// For MAS 1.0 POST Call
if (isContentType)
{
request.ContentType = @"application/json";
}
if (!String.IsNullOrEmpty(requestUserAgent))
{
((HttpWebRequest)request).UserAgent = requestUserAgent;
}
if (!String.IsNullOrEmpty(requestBody))
{
Stream stream = request.GetRequestStream();
string requestString = requestBody;
stream.Write(Encoding.Default.GetBytes(requestString), 0, requestString.Length);
stream.Close();
}
/* ERROR */ HttpWebResponse response = (HttpWebResponse)request.GetResponse();
using (StreamReader responseStream = new StreamReader(response.GetResponseStream()))
{
responseString = responseStream.ReadToEnd();
}
}
catch (Exception ex)
{
AnswersExceptionCollection.GetInstance().AddException(ex);
}
return responseString;
}
}
当代码执行到达时,我收到以下消息HttpWebResponse response = (HttpWebResponse)request.GetResponse();
:
发生错误 - 对象引用未设置为对象的实例。
我知道为什么会发生这种错误,但在这种特殊情况下我无法找到根本原因。任何帮助都感激不尽。