using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;
using System.Web;
using Newtonsoft.Json;
using System.Net.Http;


namespace BPASmartClient.AGV
{
    public class AGVHelper
    {
        public static AGVHelper _Instance { get; set; }
        public static AGVHelper GetInstance => _Instance ?? (_Instance = new AGVHelper());
     
        public AGVHelper() 
        {
            
        }

        public string HttpRequest(string url, string head, string body)
        { 
            return PostData(url, head, body);
        }
        public string PostData(string url, string head, string body)
        {
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
            request.Method = "POST";
            request.Headers["header"] = head;
            //request.Proxy = new WebProxy("192.168.1.12",80);
            byte[] bytes = Encoding.UTF8.GetBytes(body);
            request.ContentType = "application/json; charset=UTF-8"; ;//窗体数据被编码为名称/值对形式
            //request.ContentType = "application/json";
            request.ContentLength = bytes.Length;
            Stream myResponseStream = request.GetRequestStream();
            myResponseStream.Write(bytes, 0, bytes.Length);
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            StreamReader myStreamReader = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
            string retString = myStreamReader.ReadToEnd();
            myStreamReader.Close();
            myResponseStream.Close();

            if (response != null)
            {
                response.Close();
            }
            if (request != null)
            {
                request.Abort();
            }
            return retString;//返回响应报文


        }
        
    }
}
