//==============================================================
//  版权所有：深圳杰文科技
//  文件名：HttpHelper.cs
//  版本：V1.0  
//  创建者：Jay  ( QQ： 85363208 )
//  创建时间：2017-11-28 16:18
//  创建描述：
//  修改者：
//  修改时间：
//  修改说明： 
//==============================================================

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Web.Script.Serialization;

namespace Infrastructure.Utilities
{
	public static class HttpHelper
	{
		public static string Post(string url, object postbody)
		{
			var data = new JavaScriptSerializer
			{
				MaxJsonLength = 102400000
			}.Serialize(postbody);

			HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
			byte[] bytes = Encoding.UTF8.GetBytes(data);
			request.ContentType = "application/json";
			request.Method = "POST";
			request.ContentLength = bytes.Length;
			var rq = request.GetRequestStream();
			rq.Write(bytes, 0, bytes.Length);
			rq.Close();
			var response = (HttpWebResponse)request.GetResponse();
			var result = new StreamReader(response.GetResponseStream()).ReadToEnd();
			return result;
		}

		public static string Get(string url)
		{
			return Get(url, "");
		}

		public static string Get(string url, string args)
		{
			url = args.IsNullOrEmpty() ? url : url + "?" + args;
			HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
			request.ContentType = "application/json";
			request.Method = "GET";
			HttpWebResponse response = (HttpWebResponse)request.GetResponse();
			Stream myResponseStream = response.GetResponseStream();
			StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.UTF8);
			string result = myStreamReader.ReadToEnd();
			myStreamReader.Close();
			myResponseStream.Close();
			return result;
		}

	}
}
