void TestNTFD()
{
try
{
try
{
ServicePointManager.SecurityProtocol = (SecurityProtocolType)192 | (SecurityProtocolType)768 | (SecurityProtocolType)3072;
ServicePointManager.ServerCertificateValidationCallback +=
delegate(object sender2, System.Security.Cryptography.X509Certificates.X509Certificate certificate,
System.Security.Cryptography.X509Certificates.X509Chain chain,
System.Net.Security.SslPolicyErrors sslPolicyErrors)
{
return true;
};
}
catch { }
string url = "https://10.20.1.108:8443/api/iot/SetIotDeviceStatus";
Dictionary<string, string> parameters = new Dictionary<string, string>() {
{ "org_id","10211" },
{ "iot_device_id_list","1,2,3" },
{ "iot_status","1" },
{ "staff_id","1" },
{ "token","6BC43987-84D7-4F93-B185-F658D6D5F246" }
};
var res = GetResponseResult(url, GetRequestData(parameters),
"post", "application/x-www-form-urlencoded;charset=utf-8"
, defaultEncodType, defaultTimeout, parameters);
string ra = "";
}
catch (Exception ex)
{
string err = ex + "";
}
}
static HttpWebRequest GetHttpWebRequest(string requestURI, string requestData, string method,
string contentType, Encoding encodType, int timeout, Dictionary<string, string> headers)
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(requestURI);
// request method
request.Method = method;
request.ReadWriteTimeout = timeout;
request.Timeout = timeout;
// headers
if (headers != null && headers.Count > 0)
{
foreach (var item in headers)
{
request.Headers.Add(item.Key, item.Value);
}
}
// contentType
if (!string.IsNullOrEmpty(contentType))
{
request.ContentType = contentType;
}
// content
if (!string.IsNullOrEmpty(requestData))
{
byte[] buf = encodType.GetBytes(requestData);
request.ContentLength = buf.Length;
using (Stream reqStream = request.GetRequestStream())
{
reqStream.Write(buf, 0, buf.Length);
reqStream.Close();
}
}
return request;
}
static string GetResponseResult(string requestURI, string requestData,
string method,
string contentType, Encoding encodType, int timeout, Dictionary<string, string> headers)
{
HttpWebRequest myRequest = GetHttpWebRequest(requestURI, requestData, method,
contentType, encodType, timeout, headers);
string result = string.Empty;
using (HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse())
{
if (myResponse.StatusCode == HttpStatusCode.OK)
{
using (StreamReader reader = new StreamReader(myResponse.GetResponseStream(), encodType))
{
result = reader.ReadToEnd();
}
}
}
return result;
}
static string GetRequestData(Dictionary<string, string> postParameters)
{
if (postParameters == null || postParameters.Count < 1)
{
return string.Empty;
}
StringBuilder paraStrBuilder = new StringBuilder();
foreach (string key in postParameters.Keys)
{
paraStrBuilder.AppendFormat("{0}={1}&", key, postParameters[key]);
}
string para = paraStrBuilder.ToString();
if (para.EndsWith("&"))
{
para = para.Remove(para.Length - 1, 1);
}
return para.ToString();
}
/// <summary>
/// default encode: UTF8
/// </summary>
static Encoding defaultEncodType = Encoding.UTF8;
/// <summary>
/// timeout,Unit is milliseconds, default is 10 s
/// </summary>
static int defaultTimeout = 10000;