调用接口并获取放回json值

public static string PostDataGetHtml(string uri, string postData)
{
try
{
byte[] data = Encoding.UTF8.GetBytes(postData);

Uri uRI = new Uri(uri);
HttpWebRequest req = WebRequest.Create(uRI) as HttpWebRequest;
req.Method = "POST";
req.KeepAlive = true;
req.ContentType = "application/x-www-form-urlencoded";
req.ContentLength = data.Length;
req.AllowAutoRedirect = true;

Stream outStream = req.GetRequestStream();
outStream.Write(data, 0, data.Length);
outStream.Close();

HttpWebResponse res = req.GetResponse() as HttpWebResponse;
Stream inStream = res.GetResponseStream();
StreamReader sr = new StreamReader(inStream, Encoding.UTF8);
string htmlResult = sr.ReadToEnd();

return htmlResult;
}
catch (Exception ex)
{
return "网络错误:" + ex.Message.ToString();
}
}

posted @ 2015-08-17 15:13  595824023  阅读(354)  评论(0编辑  收藏  举报