简化无用参数
This commit is contained in:
@@ -7,30 +7,22 @@
|
|||||||
<!--是启用写监控报错日志-->
|
<!--是启用写监控报错日志-->
|
||||||
<add key="IsWriteMonitorLog" value="0"/>
|
<add key="IsWriteMonitorLog" value="0"/>
|
||||||
<add key="IsWriteLog4" value="1"/>
|
<add key="IsWriteLog4" value="1"/>
|
||||||
<!--称重类型:Ground=地面称重(两独立工位), Hanging=空中称重/吊称(放油前+放油后)-->
|
<!--称重类型:Ground=地面称重(两独立工位), Hanging=空中称重/吊称(放油前+放油后)-->
|
||||||
<add key="WeighingType" value="Hanging"/>
|
<add key="WeighingType" value="Hanging"/>
|
||||||
<!--逻辑工位映射:必须与SignalTable_CZ.xlsx的设备类型对应表工位号一致-->
|
<!--逻辑工位映射:必须与SignalTable_CZ.xlsx的设备类型对应表工位号一致-->
|
||||||
<add key="Station1OpName" value="ST04"/>
|
<add key="Station1OpName" value="ST04"/>
|
||||||
<add key="Station2OpName" value="ST05"/>
|
<add key="Station2OpName" value="ST05"/>
|
||||||
<!--数据库连接-->
|
<!--数据库连接-->
|
||||||
<add key="ConnectionString" value="server=.,1333;database=MESBasicDB_WC_CZGKJ;uid=sa;pwd=126.com;Connection Reset=FALSE;Max Pool Size = 1000"/>
|
<add key="ConnectionString" value="server=.,1333;database=MESBasicDB_WC_CZGKJ;uid=sa;pwd=126.com;Connection Reset=FALSE;Max Pool Size = 1000"/>
|
||||||
<!--WebApi-->
|
<!--WebApi-->
|
||||||
<add key="WebApi_Port" value="9981"/>
|
<add key="WebApi_Port" value="9981"/>
|
||||||
|
|
||||||
|
<!--扫码枪:1=启用, 0=禁用-->
|
||||||
<add key="WebApi_ReqUrl_WMS_Call" value="http://127.0.0.1:9981/api/wms/msgRequest_WMS_Call"/>
|
<add key="IsUseBarcode" value="0"/>
|
||||||
<add key="WebApi_ReqUrl_WMS_Return" value="http://127.0.0.1:9981/api/wms/msgRequest_WMS_Return"/>
|
<!--工位1扫码枪COM口,如COM3-->
|
||||||
<add key="WebUrl_AGV_HK" value=" http://127.0.0.1:9981/api/agv/MES_To_AGV_HK_Continue" />
|
<add key="Station1BarCOM" value="COM2"/>
|
||||||
<add key="WebUrl_AGV_HK_CTU" value="http://127.0.0.1:9981/api/agv/MES_To_AGV_HK_CTU" />
|
<!--工位2扫码枪COM口,如COM4-->
|
||||||
|
<add key="Station2BarCOM" value="COM3"/>
|
||||||
<add key="WebUrl_AGV_HK_agvCallback" value="http://127.0.0.1:9981/api/agv/agvCallback" />
|
|
||||||
|
|
||||||
<!--扫码枪:1=启用, 0=禁用-->
|
|
||||||
<add key="IsUseBarcode" value="0"/>
|
|
||||||
<!--工位1扫码枪COM口,如COM3-->
|
|
||||||
<add key="Station1BarCOM" value="COM2"/>
|
|
||||||
<!--工位2扫码枪COM口,如COM4-->
|
|
||||||
<add key="Station2BarCOM" value="COM3"/>
|
|
||||||
<!--扫码枪波特率-->
|
<!--扫码枪波特率-->
|
||||||
<add key="BarcodeBaudRate" value="9600"/>
|
<add key="BarcodeBaudRate" value="9600"/>
|
||||||
|
|
||||||
|
|||||||
@@ -41,12 +41,8 @@ namespace MesWork
|
|||||||
public static int IsWriteLog4 = Convert.ToInt32(AppSettings["IsWriteLog4"]);
|
public static int IsWriteLog4 = Convert.ToInt32(AppSettings["IsWriteLog4"]);
|
||||||
public static string ConnectionString = AppSettings["ConnectionString"];
|
public static string ConnectionString = AppSettings["ConnectionString"];
|
||||||
|
|
||||||
public static string AuthorizationSet = AppSettings["AuthorizationSet"];
|
|
||||||
|
|
||||||
public static int WebApi_Port = Convert.ToInt32(AppSettings["WebApi_Port"]);
|
public static int WebApi_Port = Convert.ToInt32(AppSettings["WebApi_Port"]);
|
||||||
|
|
||||||
public static string WebApi_ReqUrl = AppSettings["WebApi_ReqUrl"];
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 当前工位号
|
/// 当前工位号
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|||||||
@@ -15,42 +15,6 @@ namespace MesWork
|
|||||||
public class B_DB_Opera
|
public class B_DB_Opera
|
||||||
{
|
{
|
||||||
|
|
||||||
public static void Event_WorkStart(string opName, int tagTypeCodeID, string partType, string tagID)
|
|
||||||
{
|
|
||||||
var ChangeTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff");
|
|
||||||
var procedureName = "Event_WorkStart";
|
|
||||||
var sqlParameter = new SqlParameter[] {
|
|
||||||
new SqlParameter("@OpName", opName),
|
|
||||||
new SqlParameter("@TagTypeCodeID", tagTypeCodeID),
|
|
||||||
new SqlParameter("@PartType", partType),
|
|
||||||
new SqlParameter("@ChangeTime",ChangeTime),
|
|
||||||
new SqlParameter("@TagID", tagID)
|
|
||||||
};
|
|
||||||
SqlOperation.ExecuteStoredProcedure(procedureName, sqlParameter, out string errorMessage);
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
public static void Event_WorkEnd(string opName, int tagTypeCodeID, string partType
|
|
||||||
, string barCode, int qualityMask, int outCount, int duration
|
|
||||||
, string tagID)
|
|
||||||
{
|
|
||||||
var ChangeTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff");
|
|
||||||
var procedureName = "Event_WorkEnd";
|
|
||||||
var sqlParameter = new SqlParameter[] {
|
|
||||||
new SqlParameter("@OpName", opName),
|
|
||||||
new SqlParameter("@TagTypeCodeID", tagTypeCodeID),
|
|
||||||
new SqlParameter("@PartType", partType),
|
|
||||||
new SqlParameter("@BarCode", barCode),
|
|
||||||
new SqlParameter("@QualityMask", qualityMask),
|
|
||||||
new SqlParameter("@OutCount", outCount),
|
|
||||||
new SqlParameter("@Duration", duration),
|
|
||||||
new SqlParameter("@ChangeTime",ChangeTime),
|
|
||||||
new SqlParameter("@TagID", tagID)
|
|
||||||
};
|
|
||||||
|
|
||||||
SqlOperation.ExecuteStoredProcedure(procedureName, sqlParameter, out string errorMessage);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
public static void Event_Signal_Log(string opName, int tagTypeCodeID, string signalName, string value)
|
public static void Event_Signal_Log(string opName, int tagTypeCodeID, string signalName, string value)
|
||||||
{
|
{
|
||||||
@@ -101,36 +65,6 @@ namespace MesWork
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void Event_AlarmAndTip_Start(string opName, int tagTypeCodeID, string alarmText, string tagID, int Alarm1_Tip2)
|
|
||||||
{
|
|
||||||
var ChangeTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff");
|
|
||||||
var procedureName = "Event_AlarmAndTip_Start";
|
|
||||||
var sqlParameter = new SqlParameter[] {
|
|
||||||
new SqlParameter("@OpName", opName),
|
|
||||||
new SqlParameter("@TagTypeCodeID", tagTypeCodeID),
|
|
||||||
new SqlParameter("@AlarmText", alarmText),
|
|
||||||
new SqlParameter("@ChangeTime",ChangeTime),
|
|
||||||
new SqlParameter("@TagID", tagID),
|
|
||||||
new SqlParameter("@Alarm1_Tip2", Alarm1_Tip2),
|
|
||||||
};
|
|
||||||
SqlOperation.ExecuteStoredProcedure(procedureName, sqlParameter, out string errorMessage);
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
public static void MES_StatusChange(string opName, int StatusCode, string TriggerTime)
|
|
||||||
{
|
|
||||||
var procedureName = "设备管理_状态变化新增";
|
|
||||||
var sqlParameter = new SqlParameter[] {
|
|
||||||
new SqlParameter("@opName", opName),
|
|
||||||
new SqlParameter("@StatusCode", StatusCode),
|
|
||||||
new SqlParameter("@TriggerTime", TriggerTime),
|
|
||||||
};
|
|
||||||
|
|
||||||
SqlOperation.ExecuteStoredProcedure(procedureName, sqlParameter, out string errorMessage);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
public static void Query_AlarmData(string opName, out int count, out string text)
|
public static void Query_AlarmData(string opName, out int count, out string text)
|
||||||
{
|
{
|
||||||
count = 0;
|
count = 0;
|
||||||
@@ -151,22 +85,6 @@ namespace MesWork
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public static void Event_DeviceStatus_UseTime(string opName, string Onhours, string Workhours, string DeviceStatus)
|
|
||||||
{
|
|
||||||
var ChangeTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff");
|
|
||||||
var procedureName = "Event_DeviceStatus_UseTime";
|
|
||||||
var sqlParameter = new SqlParameter[] {
|
|
||||||
new SqlParameter("@OpName", opName),
|
|
||||||
new SqlParameter("@Onhours", Onhours),
|
|
||||||
new SqlParameter("@Workhours", Workhours),
|
|
||||||
new SqlParameter("@DeviceStatus", DeviceStatus),
|
|
||||||
new SqlParameter("@ChangeTime",ChangeTime),
|
|
||||||
};
|
|
||||||
|
|
||||||
SqlOperation.ExecuteStoredProcedure(procedureName, sqlParameter, out string errorMessage);
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
public static void Event_DeviceStatus_Change(string opName, string DeviceStatus)
|
public static void Event_DeviceStatus_Change(string opName, string DeviceStatus)
|
||||||
{
|
{
|
||||||
var ChangeTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff");
|
var ChangeTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff");
|
||||||
|
|||||||
@@ -1,362 +1,367 @@
|
|||||||
using MesWork;
|
using MesWork;
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
using System.Configuration;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Net;
|
using System.Net;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
namespace PLMTEST
|
namespace PLMTEST
|
||||||
{
|
{
|
||||||
public class HttpCli
|
public class HttpCli
|
||||||
{
|
{
|
||||||
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 指定Url地址使用Get 方式获取全部字符串
|
/// 指定Url地址使用Get 方式获取全部字符串
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="url">请求链接地址</param>
|
/// <param name="url">请求链接地址</param>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public static string Get(string url)
|
public static string Get(string url)
|
||||||
{
|
{
|
||||||
string result = "";
|
string result = "";
|
||||||
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
|
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
|
||||||
HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
|
HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
|
||||||
Stream stream = resp.GetResponseStream();
|
Stream stream = resp.GetResponseStream();
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
//获取内容
|
//获取内容
|
||||||
using (StreamReader reader = new StreamReader(stream))
|
using (StreamReader reader = new StreamReader(stream))
|
||||||
|
{
|
||||||
|
result = reader.ReadToEnd();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
|
stream.Close();
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void GetFile(string url,string path)
|
||||||
|
{
|
||||||
|
WebRequest request = WebRequest.Create(url);
|
||||||
|
WebResponse response = request.GetResponse();
|
||||||
|
if (response.ContentType.ToLower().Length > 0)
|
||||||
|
{
|
||||||
|
using (Stream reader = response.GetResponseStream())
|
||||||
|
{
|
||||||
|
using (FileStream writer = new FileStream(path, FileMode.OpenOrCreate, FileAccess.Write))
|
||||||
|
{
|
||||||
|
byte[] buffer = new byte[1024];
|
||||||
|
int c = 0;
|
||||||
|
while ((c = reader.Read(buffer, 0, buffer.Length)) > 0)
|
||||||
|
{
|
||||||
|
writer.Write(buffer, 0, c);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
//HttpContext
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static byte[] GetFile_Bytes(string url)
|
||||||
|
{
|
||||||
|
byte[] bytesAll = null;
|
||||||
|
WebRequest request = WebRequest.Create(url);
|
||||||
|
WebResponse response = request.GetResponse();
|
||||||
|
if (response.ContentType.ToLower().Length > 0)
|
||||||
|
{
|
||||||
|
using (Stream reader = response.GetResponseStream())
|
||||||
|
{
|
||||||
|
MemoryStream ms = new MemoryStream();
|
||||||
|
byte[] buffer = new byte[1024];
|
||||||
|
while (true)
|
||||||
|
{
|
||||||
|
int sz = reader.Read(buffer, 0, 1024);
|
||||||
|
if (sz == 0) break;
|
||||||
|
ms.Write(buffer, 0, sz);
|
||||||
|
}
|
||||||
|
bytesAll = ms.ToArray();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//如是图片
|
||||||
|
//System.Drawing.Image img = System.Drawing.Image.FromStream(ms);
|
||||||
|
|
||||||
|
return bytesAll;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 发送Get请求
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="url">地址</param>
|
||||||
|
/// <param name="dic">请求参数定义</param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public static string Get(string url, Dictionary<string, string> dic)
|
||||||
|
{
|
||||||
|
string result = "";
|
||||||
|
StringBuilder builder = new StringBuilder();
|
||||||
|
builder.Append(url);
|
||||||
|
if (dic.Count > 0)
|
||||||
|
{
|
||||||
|
builder.Append("?");
|
||||||
|
int i = 0;
|
||||||
|
foreach (var item in dic)
|
||||||
|
{
|
||||||
|
if (i > 0)
|
||||||
|
builder.Append("&");
|
||||||
|
builder.AppendFormat("{0}={1}", item.Key, item.Value);
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(builder.ToString());
|
||||||
|
//添加参数
|
||||||
|
HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
|
||||||
|
Stream stream = resp.GetResponseStream();
|
||||||
|
try
|
||||||
|
{
|
||||||
|
//获取内容
|
||||||
|
using (StreamReader reader = new StreamReader(stream))
|
||||||
|
{
|
||||||
|
result = reader.ReadToEnd();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
|
stream.Close();
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 指定Post地址使用Get 方式获取全部字符串
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="url">请求后台地址</param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public static string Post(string url)
|
||||||
|
{
|
||||||
|
string result = "";
|
||||||
|
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
|
||||||
|
req.Method = "POST";
|
||||||
|
HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
|
||||||
|
Stream stream = resp.GetResponseStream();
|
||||||
|
//获取内容
|
||||||
|
using (StreamReader reader = new StreamReader(stream, Encoding.UTF8))
|
||||||
|
{
|
||||||
|
result = reader.ReadToEnd();
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 指定Post地址使用Get 方式获取全部字符串
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="url">请求后台地址</param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public static string Post(string url, Dictionary<string, string> dic)
|
||||||
|
{
|
||||||
|
string result = "";
|
||||||
|
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
|
||||||
|
req.Method = "POST";
|
||||||
|
req.ContentType = "application/x-www-form-urlencoded";
|
||||||
|
#region 添加Post 参数
|
||||||
|
StringBuilder builder = new StringBuilder();
|
||||||
|
int i = 0;
|
||||||
|
foreach (var item in dic)
|
||||||
|
{
|
||||||
|
if (i > 0)
|
||||||
|
builder.Append("&");
|
||||||
|
builder.AppendFormat("{0}={1}", item.Key, item.Value);
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
byte[] data = Encoding.UTF8.GetBytes(builder.ToString());
|
||||||
|
req.ContentLength = data.Length;
|
||||||
|
using (Stream reqStream = req.GetRequestStream())
|
||||||
|
{
|
||||||
|
reqStream.Write(data, 0, data.Length);
|
||||||
|
reqStream.Close();
|
||||||
|
}
|
||||||
|
#endregion
|
||||||
|
HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
|
||||||
|
Stream stream = resp.GetResponseStream();
|
||||||
|
//获取响应内容
|
||||||
|
using (StreamReader reader = new StreamReader(stream, Encoding.UTF8))
|
||||||
|
{
|
||||||
|
result = reader.ReadToEnd();
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 指定Post地址使用Get 方式获取全部字符串
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="url">请求后台地址</param>
|
||||||
|
/// <param name="content">Post提交数据内容(utf-8编码的)</param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public static string Post_Authorization(string url, string content,out string error )
|
||||||
|
{
|
||||||
|
string result = "";
|
||||||
|
error = "";
|
||||||
|
try
|
||||||
|
{
|
||||||
|
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
|
||||||
|
req.Timeout = 30000;
|
||||||
|
req.Method = "POST";
|
||||||
|
req.ContentType = "application/json";
|
||||||
|
|
||||||
|
// 添加自定义 Header
|
||||||
|
string authorization = ConfigurationManager.AppSettings["AuthorizationSet"];
|
||||||
|
if (!string.IsNullOrWhiteSpace(authorization))
|
||||||
{
|
{
|
||||||
result = reader.ReadToEnd();
|
req.Headers.Add("Authorization", authorization);
|
||||||
}
|
}
|
||||||
}
|
req.Headers.Add("uuid", Guid.NewGuid().ToString().ToUpper());
|
||||||
finally
|
|
||||||
{
|
#region 添加Post 参数
|
||||||
stream.Close();
|
byte[] data = Encoding.UTF8.GetBytes(content);
|
||||||
}
|
req.ContentLength = data.Length;
|
||||||
return result;
|
using (Stream reqStream = req.GetRequestStream())
|
||||||
}
|
{
|
||||||
|
reqStream.Write(data, 0, data.Length);
|
||||||
public static void GetFile(string url,string path)
|
reqStream.Close();
|
||||||
{
|
}
|
||||||
WebRequest request = WebRequest.Create(url);
|
#endregion
|
||||||
WebResponse response = request.GetResponse();
|
|
||||||
if (response.ContentType.ToLower().Length > 0)
|
HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
|
||||||
{
|
Stream stream = resp.GetResponseStream();
|
||||||
using (Stream reader = response.GetResponseStream())
|
//获取响应内容
|
||||||
{
|
using (StreamReader reader = new StreamReader(stream, Encoding.UTF8))
|
||||||
using (FileStream writer = new FileStream(path, FileMode.OpenOrCreate, FileAccess.Write))
|
{
|
||||||
{
|
result = reader.ReadToEnd();
|
||||||
byte[] buffer = new byte[1024];
|
}
|
||||||
int c = 0;
|
}
|
||||||
while ((c = reader.Read(buffer, 0, buffer.Length)) > 0)
|
catch (Exception err)
|
||||||
{
|
{
|
||||||
writer.Write(buffer, 0, c);
|
error = $"WebApi Post请求调用错误:{err.Message}";
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
return result;
|
||||||
//HttpContext
|
}
|
||||||
}
|
public static string Post(string url, string content, out string error)
|
||||||
}
|
{
|
||||||
|
string result = "";
|
||||||
public static byte[] GetFile_Bytes(string url)
|
error = "";
|
||||||
{
|
try
|
||||||
byte[] bytesAll = null;
|
{
|
||||||
WebRequest request = WebRequest.Create(url);
|
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
|
||||||
WebResponse response = request.GetResponse();
|
req.Timeout = 30000;
|
||||||
if (response.ContentType.ToLower().Length > 0)
|
req.Method = "POST";
|
||||||
{
|
req.ContentType = "application/json";
|
||||||
using (Stream reader = response.GetResponseStream())
|
|
||||||
{
|
#region 添加Post 参数
|
||||||
MemoryStream ms = new MemoryStream();
|
byte[] data = Encoding.UTF8.GetBytes(content);
|
||||||
byte[] buffer = new byte[1024];
|
req.ContentLength = data.Length;
|
||||||
while (true)
|
using (Stream reqStream = req.GetRequestStream())
|
||||||
{
|
{
|
||||||
int sz = reader.Read(buffer, 0, 1024);
|
reqStream.Write(data, 0, data.Length);
|
||||||
if (sz == 0) break;
|
reqStream.Close();
|
||||||
ms.Write(buffer, 0, sz);
|
}
|
||||||
}
|
#endregion
|
||||||
bytesAll = ms.ToArray();
|
|
||||||
}
|
HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
|
||||||
}
|
Stream stream = resp.GetResponseStream();
|
||||||
|
//获取响应内容
|
||||||
//如是图片
|
using (StreamReader reader = new StreamReader(stream, Encoding.UTF8))
|
||||||
//System.Drawing.Image img = System.Drawing.Image.FromStream(ms);
|
{
|
||||||
|
result = reader.ReadToEnd();
|
||||||
return bytesAll;
|
}
|
||||||
|
}
|
||||||
}
|
catch (Exception err)
|
||||||
|
{
|
||||||
|
error = $"WebApi Post请求调用错误:{err.Message}";
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// 发送Get请求
|
/// <summary>
|
||||||
/// </summary>
|
/// Http下载文件
|
||||||
/// <param name="url">地址</param>
|
/// </summary>
|
||||||
/// <param name="dic">请求参数定义</param>
|
/// <param name="uri">下载地址</param>
|
||||||
/// <returns></returns>
|
/// <param name="filefullpath">存放完整路径(含文件名)</param>
|
||||||
public static string Get(string url, Dictionary<string, string> dic)
|
/// <param name="size">每次多的大小</param>
|
||||||
{
|
/// <returns>下载操作是否成功</returns>
|
||||||
string result = "";
|
|
||||||
StringBuilder builder = new StringBuilder();
|
public static bool DownLoadFiles(string uri, string filefullpath, int size = 1000000)
|
||||||
builder.Append(url);
|
{
|
||||||
if (dic.Count > 0)
|
try
|
||||||
{
|
{
|
||||||
builder.Append("?");
|
if (File.Exists(filefullpath))
|
||||||
int i = 0;
|
{
|
||||||
foreach (var item in dic)
|
try
|
||||||
{
|
{
|
||||||
if (i > 0)
|
File.Delete(filefullpath);
|
||||||
builder.Append("&");
|
}
|
||||||
builder.AppendFormat("{0}={1}", item.Key, item.Value);
|
catch (Exception ex)
|
||||||
i++;
|
{
|
||||||
}
|
return false;
|
||||||
}
|
}
|
||||||
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(builder.ToString());
|
}
|
||||||
//添加参数
|
|
||||||
HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
|
string fileDirectory = System.IO.Path.GetDirectoryName(filefullpath);
|
||||||
Stream stream = resp.GetResponseStream();
|
|
||||||
try
|
if (!Directory.Exists(fileDirectory))
|
||||||
{
|
{
|
||||||
//获取内容
|
Directory.CreateDirectory(fileDirectory);
|
||||||
using (StreamReader reader = new StreamReader(stream))
|
}
|
||||||
{
|
|
||||||
result = reader.ReadToEnd();
|
FileStream fs = new FileStream(filefullpath, FileMode.Create);
|
||||||
}
|
|
||||||
}
|
byte[] buffer = new byte[size];
|
||||||
finally
|
|
||||||
{
|
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(uri);
|
||||||
stream.Close();
|
|
||||||
}
|
request.Timeout = 100000;
|
||||||
return result;
|
|
||||||
}
|
request.AddRange((int)fs.Length);
|
||||||
|
|
||||||
|
Stream ns = request.GetResponse().GetResponseStream();
|
||||||
|
|
||||||
/// <summary>
|
long contentLength = request.GetResponse().ContentLength;
|
||||||
/// 指定Post地址使用Get 方式获取全部字符串
|
|
||||||
/// </summary>
|
int length = ns.Read(buffer, 0, buffer.Length);
|
||||||
/// <param name="url">请求后台地址</param>
|
|
||||||
/// <returns></returns>
|
while (length > 0)
|
||||||
public static string Post(string url)
|
{
|
||||||
{
|
fs.Write(buffer,0 , length);
|
||||||
string result = "";
|
buffer = new byte[size];
|
||||||
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
|
length = ns.Read(buffer, 0, buffer.Length);
|
||||||
req.Method = "POST";
|
}
|
||||||
HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
|
|
||||||
Stream stream = resp.GetResponseStream();
|
fs.Close();
|
||||||
//获取内容
|
|
||||||
using (StreamReader reader = new StreamReader(stream, Encoding.UTF8))
|
return true;
|
||||||
{
|
}
|
||||||
result = reader.ReadToEnd();
|
catch (Exception ex)
|
||||||
}
|
{
|
||||||
return result;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// 指定Post地址使用Get 方式获取全部字符串
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="url">请求后台地址</param>
|
|
||||||
/// <returns></returns>
|
}
|
||||||
public static string Post(string url, Dictionary<string, string> dic)
|
}
|
||||||
{
|
|
||||||
string result = "";
|
|
||||||
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
|
|
||||||
req.Method = "POST";
|
|
||||||
req.ContentType = "application/x-www-form-urlencoded";
|
|
||||||
#region 添加Post 参数
|
|
||||||
StringBuilder builder = new StringBuilder();
|
|
||||||
int i = 0;
|
|
||||||
foreach (var item in dic)
|
|
||||||
{
|
|
||||||
if (i > 0)
|
|
||||||
builder.Append("&");
|
|
||||||
builder.AppendFormat("{0}={1}", item.Key, item.Value);
|
|
||||||
i++;
|
|
||||||
}
|
|
||||||
byte[] data = Encoding.UTF8.GetBytes(builder.ToString());
|
|
||||||
req.ContentLength = data.Length;
|
|
||||||
using (Stream reqStream = req.GetRequestStream())
|
|
||||||
{
|
|
||||||
reqStream.Write(data, 0, data.Length);
|
|
||||||
reqStream.Close();
|
|
||||||
}
|
|
||||||
#endregion
|
|
||||||
HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
|
|
||||||
Stream stream = resp.GetResponseStream();
|
|
||||||
//获取响应内容
|
|
||||||
using (StreamReader reader = new StreamReader(stream, Encoding.UTF8))
|
|
||||||
{
|
|
||||||
result = reader.ReadToEnd();
|
|
||||||
}
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// 指定Post地址使用Get 方式获取全部字符串
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="url">请求后台地址</param>
|
|
||||||
/// <param name="content">Post提交数据内容(utf-8编码的)</param>
|
|
||||||
/// <returns></returns>
|
|
||||||
public static string Post_Authorization(string url, string content,out string error )
|
|
||||||
{
|
|
||||||
string result = "";
|
|
||||||
error = "";
|
|
||||||
try
|
|
||||||
{
|
|
||||||
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
|
|
||||||
req.Timeout = 30000;
|
|
||||||
req.Method = "POST";
|
|
||||||
req.ContentType = "application/json";
|
|
||||||
|
|
||||||
// 添加自定义 Header
|
|
||||||
req.Headers.Add("Authorization", MesWorkForm.AuthorizationSet);
|
|
||||||
req.Headers.Add("uuid", Guid.NewGuid().ToString().ToUpper());
|
|
||||||
|
|
||||||
#region 添加Post 参数
|
|
||||||
byte[] data = Encoding.UTF8.GetBytes(content);
|
|
||||||
req.ContentLength = data.Length;
|
|
||||||
using (Stream reqStream = req.GetRequestStream())
|
|
||||||
{
|
|
||||||
reqStream.Write(data, 0, data.Length);
|
|
||||||
reqStream.Close();
|
|
||||||
}
|
|
||||||
#endregion
|
|
||||||
|
|
||||||
HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
|
|
||||||
Stream stream = resp.GetResponseStream();
|
|
||||||
//获取响应内容
|
|
||||||
using (StreamReader reader = new StreamReader(stream, Encoding.UTF8))
|
|
||||||
{
|
|
||||||
result = reader.ReadToEnd();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
catch (Exception err)
|
|
||||||
{
|
|
||||||
error = $"WebApi Post请求调用错误:{err.Message}";
|
|
||||||
}
|
|
||||||
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
public static string Post(string url, string content, out string error)
|
|
||||||
{
|
|
||||||
string result = "";
|
|
||||||
error = "";
|
|
||||||
try
|
|
||||||
{
|
|
||||||
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
|
|
||||||
req.Timeout = 30000;
|
|
||||||
req.Method = "POST";
|
|
||||||
req.ContentType = "application/json";
|
|
||||||
|
|
||||||
#region 添加Post 参数
|
|
||||||
byte[] data = Encoding.UTF8.GetBytes(content);
|
|
||||||
req.ContentLength = data.Length;
|
|
||||||
using (Stream reqStream = req.GetRequestStream())
|
|
||||||
{
|
|
||||||
reqStream.Write(data, 0, data.Length);
|
|
||||||
reqStream.Close();
|
|
||||||
}
|
|
||||||
#endregion
|
|
||||||
|
|
||||||
HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
|
|
||||||
Stream stream = resp.GetResponseStream();
|
|
||||||
//获取响应内容
|
|
||||||
using (StreamReader reader = new StreamReader(stream, Encoding.UTF8))
|
|
||||||
{
|
|
||||||
result = reader.ReadToEnd();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
catch (Exception err)
|
|
||||||
{
|
|
||||||
error = $"WebApi Post请求调用错误:{err.Message}";
|
|
||||||
}
|
|
||||||
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Http下载文件
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="uri">下载地址</param>
|
|
||||||
/// <param name="filefullpath">存放完整路径(含文件名)</param>
|
|
||||||
/// <param name="size">每次多的大小</param>
|
|
||||||
/// <returns>下载操作是否成功</returns>
|
|
||||||
|
|
||||||
public static bool DownLoadFiles(string uri, string filefullpath, int size = 1000000)
|
|
||||||
{
|
|
||||||
try
|
|
||||||
{
|
|
||||||
if (File.Exists(filefullpath))
|
|
||||||
{
|
|
||||||
try
|
|
||||||
{
|
|
||||||
File.Delete(filefullpath);
|
|
||||||
}
|
|
||||||
catch (Exception ex)
|
|
||||||
{
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
string fileDirectory = System.IO.Path.GetDirectoryName(filefullpath);
|
|
||||||
|
|
||||||
if (!Directory.Exists(fileDirectory))
|
|
||||||
{
|
|
||||||
Directory.CreateDirectory(fileDirectory);
|
|
||||||
}
|
|
||||||
|
|
||||||
FileStream fs = new FileStream(filefullpath, FileMode.Create);
|
|
||||||
|
|
||||||
byte[] buffer = new byte[size];
|
|
||||||
|
|
||||||
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(uri);
|
|
||||||
|
|
||||||
request.Timeout = 100000;
|
|
||||||
|
|
||||||
request.AddRange((int)fs.Length);
|
|
||||||
|
|
||||||
Stream ns = request.GetResponse().GetResponseStream();
|
|
||||||
|
|
||||||
long contentLength = request.GetResponse().ContentLength;
|
|
||||||
|
|
||||||
int length = ns.Read(buffer, 0, buffer.Length);
|
|
||||||
|
|
||||||
while (length > 0)
|
|
||||||
{
|
|
||||||
fs.Write(buffer,0 , length);
|
|
||||||
buffer = new byte[size];
|
|
||||||
length = ns.Read(buffer, 0, buffer.Length);
|
|
||||||
}
|
|
||||||
|
|
||||||
fs.Close();
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
catch (Exception ex)
|
|
||||||
{
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|||||||
Reference in New Issue
Block a user