0
我想從WEB API調用中獲取結果,但無法找到檢索JSON結果的方法。 這裏是一個調用Web API代碼:如何在調用WEB API後獲取JSON響應字符串
string baseAddress = "http://server001/";
HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(baseAddress + "API/Import");
req.Method = "POST";
req.ContentType = "application/x-www-form-urlencoded";
Stream reqStream = req.GetRequestStream();
string fileContents = xml;
byte[] fileToSend = Encoding.UTF8.GetBytes(fileContents);
reqStream.Write(fileToSend, 0, fileToSend.Length);
reqStream.Close();
HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
Console.WriteLine("HTTP/{0} {1} {2}", resp.ProtocolVersion, (int)resp.StatusCode, resp.StatusDescription);
//string jsonResult = resp. <=== here
提琴手調用Web API後的結果:
HTTP/1.1 200 OK
Cache-Control: no-cache
Pragma: no-cache
Content-Type: application/json; charset=utf-8
Expires: -1
Server: Microsoft-IIS/8.0
X-AspNet-Version: 4.0.30319
X-Powered-By: ASP.NET
Date: Fri, 17 Jan 2014 01:10:00 GMT
Content-Length: 57
{"isError":false,"Msg":"Server data added successfully."}
謝謝!
對!謝謝! – Max