using Microsoft.Extensions.Configuration;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Net;
using System.Collections.Specialized;

namespace WebAPIBase.Utils
{
    public class WebClientHelper
    {
        //private string url;

        public WebClientHelper()
        {
            //IConfigurationBuilder builder = new ConfigurationBuilder()
            //  .SetBasePath(Directory.GetCurrentDirectory())
            //  .AddJsonFile("appsettings.json");

            //IConfigurationRoot Configuration = builder.Build();

            //url = Configuration["Logging:AppSettings:webserviceUrl"];
        }

        /// <summary>
        /// http post
        /// </summary>
        /// <param name="url"></param>
        /// <param name="postValues"></param>
        /// <returns></returns>
        public static string ClientPost(string url, NameValueCollection postValues)
        {
            try
            {
                Console.WriteLine($"url:{url}");
                WebClient webClient = new WebClient();
                // 向服务器发送POST数据
                byte[] responseArray = webClient.UploadValues(url, "POST", postValues);
                string response = Encoding.UTF8.GetString(responseArray);
                return response;
            }
            catch (Exception ex)
            {
                throw;
            }
        }

        /// <summary>
        /// http get
        /// </summary>
        /// <param name="url"></param>
        /// <param name="getValues"></param>
        /// <returns></returns>
        public static string ClientGet(string url, NameValueCollection getValues)
        {
            try
            {

                WebClient webClient = new WebClient();

                // 向服务器发送POST数据
                byte[] responseArray = webClient.UploadValues(url, "GET", getValues);
                string response = Encoding.UTF8.GetString(responseArray);
                return response;
            }
            catch (Exception ex)
            {
                throw;
            }
        }
    }
}
