using System;
using System.Text;
using System.Net;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
string requestMethod = "POST";
string param = "platformKey=test";
string url = "http://192.168.x.xx:xxx/api/info/getListByPlatformKey";
string result = RequestAPI(requestMethod, url, param);
Console.WriteLine(result);
requestMethod = "GET";
param = "systemid=1";
url = "http://192.168.x.xx:xxx/api/info/info";
result = RequestAPI(requestMethod, url, param);
Console.WriteLine(result);
Console.ReadLine();
}
private static string RequestAPI(string requestMethod, string url, string param)
{
requestMethod = requestMethod.ToUpper();
string content = string.Empty;
byte[] bs = Encoding.ASCII.GetBytes(param);
if (requestMethod == "GET")
{
url = string.Format("{0}?{1}", url, param);
}
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
req.Method = requestMethod;
req.ContentType = "application/x-www-form-urlencoded";
req.ContentLength = bs.Length;
req.Timeout = 120 * 1000;
try
{
if (requestMethod == "POST")
{
using (System.IO.Stream reqStream = req.GetRequestStream())
{
reqStream.Write(bs, 0, bs.Length);
}
}
using (WebResponse wr = req.GetResponse()) //返回
{
System.IO.Stream res = wr.GetResponseStream();
using (System.IO.StreamReader reader = new System.IO.StreamReader(res))
{
content = reader.ReadToEnd();
}
}
}
catch (Exception ex)
{
content = ex.Message;
}
return content;
}
}
}