1 回答

TA貢獻1859條經驗 獲得超6個贊
當我注意到響應沒有被處理時,我正在調整問題以演示對內存流的讀取。這有 95% 可能是您的潛在問題。Streams 和 StreamReaders 也是一次性的,應該使用 using() 閉包進行包裝。
public object GetData()
{
object response = "";
string token = "EF232354";
string baseUrl = ConfigurationManager.AppSettings["BaseURL"].ToString();
string endPoint = ConfigurationManager.AppSettings["EndPoint"].ToString();
var httpWebRequest = (HttpWebRequest) WebRequest.Create(baseUrl + endPoint);
httpWebRequest.ContentType = "application/json";
httpWebRequest.Method = HttpVerb.GET.ToString();
httpWebRequest.Headers.Add("token", token);
using (var httpResponse = (HttpWebResponse) httpWebRequest.GetResponse())
{
using (Stream dataStream = httpResponse.GetResponseStream())
{
using (StreamReader reader = new StreamReader(dataStream))
{
using(JsonReader sdr = new JsonTextReader(reader))
{
JsonSerializer serializer = new JsonSerializer();
response = serializer.Deserialize(sdr);
}
return response;
}
}
httpResponse.Close(); // For good measure. *should* be covered by Dispose.
}
}
- 1 回答
- 0 關注
- 357 瀏覽
添加回答
舉報