using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
namespace iLogisticBase.Common.Setting.Dal
{
public class vUnLoCodeBLL
{
public static string URL = string.Empty;
public vUnLoCodeBLL()
{
URL = "";
}
/// <summary>
/// 发送http post请求
/// </summary>
/// <param name="url">地址</param>
/// <param name="parameters">查询参数集合</param>
/// <returns></returns>
public HttpWebResponse CreatePostHttpResponse(IDictionary<string, string> parameters)
{
try
{
HttpWebRequest request = WebRequest.Create(URL) as HttpWebRequest;//创建请求对象
request.Method = "POST";//请求方式
request.ContentType = "application/x-www-form-urlencoded";//链接类型 //构造查询字符串
if (!(parameters == null || parameters.Count == 0))
{
StringBuilder buffer = new StringBuilder();
bool first = true;
foreach (string key in parameters.Keys)
{
if (!first)
{
buffer.AppendFormat("&{0}={1}", key, parameters[key]);
}
else
{
buffer.AppendFormat("{0}={1}", key, parameters[key]);
first = false;
}
}
byte[] data = Encoding.UTF8.GetBytes(buffer.ToString());
//写入请求流
using (Stream stream = request.GetRequestStream())
{
stream.Write(data, 0, data.Length);
}
}
return request.GetResponse() as HttpWebResponse;
}
catch (Exception)
{
return null;
}
}
/// <summary>
/// 发送http Get请求
/// </summary>
/// <param name="url"></param>
/// <returns></returns>
public HttpWebResponse CreateGetHttpResponse(string url)
{
HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
request.Method = "GET";
request.ContentType = "application/x-www-form-urlencoded";//链接类型
return request.GetResponse() as HttpWebResponse;
}
/// <summary>
/// 从HttpWebResponse对象中提取响应的数据转换为字符串
/// </summary>
/// <param name="webresponse"></param>
/// <returns></returns>
public string GetResponseString(HttpWebResponse webresponse)
{
try
{
using (Stream s = webresponse.GetResponseStream())
{
StreamReader reader = new StreamReader(s, Encoding.UTF8);
return reader.ReadToEnd();
}
}
catch (Exception)
{
return null;
}
}
//post请求并调用
// Dictionary<string, string> dic = new Dictionary<string, string>();
// dic.Add("id", "4");
//textBox1.Text = GetResponseString(CreatePostHttpResponse("https://www.baidu.com/", dic));
get请求并调用
//textBox3.Text = GetResponseString(CreateGetHttpResponse("https://i.cnblogs.com/EditPosts.aspx?opt=1"));
private void get()
{
//string URL = "http://192.168.1.47:60002/api/Port/GetData?";
//var data = new vUnLoCode();
//StringBuilder stringBuilder = new StringBuilder();
//foreach (var item in typeof(vUnLoCode).GetProperties())
//{
// stringBuilder.Append($"{item.Name}={item.GetValue(data, null)}&");
//}
//System.Net.WebClient webClient = new System.Net.WebClient();
//var v = webClient.DownloadString(URL+stringBuilder.ToString());
}
}
}
//调用
Dictionary<string, string> dics = new Dictionary<string, string>();
foreach (var item in typeof(vUnLoCodeDTO).GetProperties())
{
if (item.GetValue(_queryvUnLoCodeDTO, null) != null && item.GetValue(_queryvUnLoCodeDTO, null).ToString() != "")
{
dics.Add(item.Name, item.GetValue(_queryvUnLoCodeDTO, null).ToString());
}
}
if (dics.Count > 0)
{
string result = bll.GetResponseString(bll.CreatePostHttpResponse(dics));
if (result != null)
{
List<vUnLoCode> vUnLoCodes = bll.Serializer(result);
if (vUnLoCodes != null)
{
gcPortCode.DataSource = vUnLoCodes;
gcPortCode.Focus();
}
else
{
MessageBox.Show("请检查输入条件是否正确");
}
}
}