string URL_WriteFeederMemory = "http://192.168.1.162:5060/api/v1/Feeder/Write_feeder_memory";
string str_WriteFeederMemory = "";
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(URL_WriteFeederMemory);
request.Method = "POST";
request.ContentType = "application/json";
SetWebRequest(request);
byte[] data_SA = Encoding.UTF8.GetBytes(str_WriteFeederMemory);
WriteRequestData(request, data_SA);
try
{
WebResponse response = request.GetResponse();
string str_response = ReadResponse(response);
Console.WriteLine(str_response);
}
catch (WebException ex)
{
//当返回 status 非200,通过下面方式获取到body
HttpWebResponse response = (HttpWebResponse)ex.Response;
string str_response = ReadResponse(response);
Console.WriteLine(str_response);
}
设置Request消息
private static void SetWebRequest(HttpWebRequest request)
{
request.Credentials = CredentialCache.DefaultCredentials;
request.Timeout = 10000;
}
发送Request消息
private static void WriteRequestData(HttpWebRequest request, byte[] data)
{
request.ContentLength = data.Length;
Stream writer = request.GetRequestStream();
writer.Write(data, 0, data.Length);
writer.Close();
}
获取Response消息
private static string ReadResponse(WebResponse response)
{
StreamReader sr = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
String retStr = sr.ReadToEnd();
sr.Close();
return retStr;
}