public string TypedTableBase(string url, string zdTable, string type)
{
//字典表下拉带出
Encoding encoding = Encoding.UTF8;
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);//webrequest请求api地址
// request.Accept = "text/html,application/xhtml+xml,*/*";
request.ContentType = "application/json";
request.UserAgent = "Mozilla/5.0 (Windows NT 6.2; WOW64; rv:24.0) Gecko/20100101 Firefox/24.0";
request.KeepAlive = true;
request.ProtocolVersion = HttpVersion.Version11;
request.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
request.Method = type.ToUpper().ToString();//get或者post
byte[] buffer = encoding.GetBytes(zdTable);
request.ContentLength = buffer.Length;
using (Stream reqStream = request.GetRequestStream())
{
reqStream.Write(buffer, 0, buffer.Length);
}
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
using (StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8))
{
string reacerend = reader.ReadToEnd();
//转成list格式
return reacerend;
}
}
public string HttpPostCreateRequestM(string url, Object input)
{
System.GC.Collect();//垃圾回收,回收没有正常关闭的http连接
string result = "";//返回结果
HttpWebRequest request = null;
HttpWebResponse response = null;
try
{
string jsonString = JsonConvert.SerializeObject(input);
byte[] objectContent = Encoding.UTF8.GetBytes(jsonString);
//byte[] bs = Encoding.ASCII.GetBytes(param);
//设置最大连接数
ServicePointManager.DefaultConnectionLimit = 200;
request = (HttpWebRequest)WebRequest.Create(url);
request.UserAgent = "Mozilla/5.0 (Windows NT 6.2; WOW64; rv:24.0) Gecko/20100101 Firefox/24.0";
request.KeepAlive = true;
request.ProtocolVersion = HttpVersion.Version11;
request.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
request.Credentials = CredentialCache.DefaultCredentials;
request.AllowAutoRedirect = true;
request.Method = "POST";
request.Timeout = 60 * 1000;
//设置POST的数据类型和长度
//request.ContentType = "application/x-www-form-urlencoded";
request.ContentType = "application/json";
request.ContentLength = objectContent.Length;
using (Stream reqStream = request.GetRequestStream())
{
reqStream.Write(objectContent, 0, objectContent.Length);
}
//获取服务端返回
response = (HttpWebResponse)request.GetResponse();
//获取服务端返回数据
StreamReader sr = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
result = sr.ReadToEnd().Trim();
sr.Close();
}
catch (System.Threading.ThreadAbortException e)
{
System.Threading.Thread.ResetAbort();
throw e;
}
catch (WebException e)
{
if (e.Status == WebExceptionStatus.ProtocolError)
{
throw e;
}
}
catch (Exception e)
{
throw e;
}
finally
{
//关闭连接和流
if (response != null)
{
response.Close();
}
if (request != null)
{
request.Abort();
}
}
return result;
}
Winform:
var PointCallingType = TypedTableBase("http://172.23.13.36:8099/api/services/app/transport/GetDataDic?typeNa=交接" + TypeName, TypeName, "POST");
var jj = (JObject)JsonConvert.DeserializeObject(PointCallingType);
List<DataDic> dataDic = JsonConvert.DeserializeObject<List<DataDic>>(JsonConvert.SerializeObject(jj.First.First));