byte[] content = Encoding.UTF8.GetBytes(data);
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Method = "POST";
request.ContentType = "application/json; charset=utf-8";
request.ContentLength = data.Length;
Stream stream = request.GetRequestStream();
stream.Write(content,0,content.Length);
response = (HttpWebResponse)request.GetResponse();
reader = new StreamReader(response.GetResponseStream(),System.Text.Encoding.UTF8);
String resultData = reader.ReadToEnd();
我得到的數據作爲迴應,也做reader.ReadToEnd()它的快速觀察它顯示VS中的數據,但resultData總是空。無法讀取響應數據使用HttpWebResponse
任何想法我在這裏做什麼錯?
任何你不使用[WebClient]的原因(http://msdn.microsoft.com/en-us/library/system.net.webclient.aspx)? – dtb
無論何時處理實現'IDisposable'的對象,即'StreamReader' /'WebResponse',您都應該使用'using'語句。閱讀http://msdn.microsoft.com/en-us/library/yh598w02.aspx – James