123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475 |
- 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 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;
- }
- }
- }
- }
|