GET
public static string GetJsonStr(string webApi)
{
string serviceAddress = webApi;
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(serviceAddress);
request.Method = "GET";
request.ContentType = "application/json;charset=UTF-8";
//request.ContentType = "application/text";
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream myResponseStream = response.GetResponseStream();
StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.UTF8);
string retString = myStreamReader.ReadToEnd();
return retString;
}
=============================
POST
public static string PostJsonStr(string webApi, string data)
{
WebRequest request = WebRequest.Create(webApi);
request.Method = "Post";
request.ContentType = "application/json;charset=utf-8";
//request.ContentLength = data.Length;
StreamWriter sw = new StreamWriter(request.GetRequestStream());
request.Timeout = 1000*60*2;//超时时间
sw.Write(data);
sw.Flush();
WebResponse response = request.GetResponse();
Stream s = response.GetResponseStream();
StreamReader sr = new StreamReader(s, Encoding.GetEncoding("utf-8"));
string readStr = (sr.ReadToEnd());
return readStr;
}