c# 如何像这样发送 HTTP 命令 - http://192.168.10.1/api/control?alert=101002
c# How to send HTTP command as this - http://192.168.10.1/api/control?alert=101002
我有来自 Patlite 的 hardware,
这个硬件有HTTP命令控制功能,比如我把url“http://192.168.10.1/api/control?alert=101002”复制到我电脑里的chrome,它会根据需要激活硬件
我想从我的代码发送命令。
我尝试了这段代码但没有成功:
System.Net.ServicePointManager.Expect100Continue = false;
WebRequest request = WebRequest.Create("http://10.0.22.222/api/control");
request.Method = "post";
request.ContentType = "application/x-www-form-urlencoded";
string postData = "alert=101002";
byte[] byteArray = Encoding.UTF8.GetBytes(postData);
request.ContentLength = byteArray.Length;
// Get the request stream.
Stream dataStream = request.GetRequestStream();
// Write the data to the request stream.
dataStream.Write(byteArray, 0, byteArray.Length);
// Close the Stream object.
dataStream.Close();
WebResponse response = request.GetResponse();
说明书上有图:
谢谢
您需要为此创建一个 webrequest 实例。
WebRequest request = WebRequest.Create("http://192.168.10.1/api/control?alert=101002");
WebResponse response = request.GetResponse();
您可能需要将一些属性设置为请求方法和凭据才能正常工作。
看到这个:
https://msdn.microsoft.com/en-us/library/456dfw4f(v=vs.100).aspx
public static string Get(string url, Encoding encoding)
{
try
{
var wc = new WebClient { Encoding = encoding };
var readStream = wc.OpenRead(url);
using (var sr = new StreamReader(readStream, encoding))
{
var result = sr.ReadToEnd();
return result;
}
}
catch (Exception e)
{
//throw e;
return e.Message;
}
}
喜欢这个代码使用url“http://192.168.10.1/api/control?alert=101002”发送得到request.Good幸运!
我有来自 Patlite 的 hardware, 这个硬件有HTTP命令控制功能,比如我把url“http://192.168.10.1/api/control?alert=101002”复制到我电脑里的chrome,它会根据需要激活硬件
我想从我的代码发送命令。
我尝试了这段代码但没有成功:
System.Net.ServicePointManager.Expect100Continue = false;
WebRequest request = WebRequest.Create("http://10.0.22.222/api/control");
request.Method = "post";
request.ContentType = "application/x-www-form-urlencoded";
string postData = "alert=101002";
byte[] byteArray = Encoding.UTF8.GetBytes(postData);
request.ContentLength = byteArray.Length;
// Get the request stream.
Stream dataStream = request.GetRequestStream();
// Write the data to the request stream.
dataStream.Write(byteArray, 0, byteArray.Length);
// Close the Stream object.
dataStream.Close();
WebResponse response = request.GetResponse();
说明书上有图:
谢谢
您需要为此创建一个 webrequest 实例。
WebRequest request = WebRequest.Create("http://192.168.10.1/api/control?alert=101002");
WebResponse response = request.GetResponse();
您可能需要将一些属性设置为请求方法和凭据才能正常工作。
看到这个:
https://msdn.microsoft.com/en-us/library/456dfw4f(v=vs.100).aspx
public static string Get(string url, Encoding encoding)
{
try
{
var wc = new WebClient { Encoding = encoding };
var readStream = wc.OpenRead(url);
using (var sr = new StreamReader(readStream, encoding))
{
var result = sr.ReadToEnd();
return result;
}
}
catch (Exception e)
{
//throw e;
return e.Message;
}
}
喜欢这个代码使用url“http://192.168.10.1/api/control?alert=101002”发送得到request.Good幸运!