终端一体化运控平台
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

AGVHelper.cs 1.9 KiB

2 年之前
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Net;
  7. using System.Web;
  8. using Newtonsoft.Json;
  9. using System.Net.Http;
  10. namespace BPASmartClient.AGV
  11. {
  12. public class AGVHelper
  13. {
  14. public static AGVHelper _Instance { get; set; }
  15. public static AGVHelper GetInstance => _Instance ?? (_Instance = new AGVHelper());
  16. public AGVHelper()
  17. {
  18. }
  19. public string HttpRequest(string url, string head, string body)
  20. {
  21. return PostData(url, head, body);
  22. }
  23. public string PostData(string url, string head, string body)
  24. {
  25. HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
  26. request.Method = "POST";
  27. request.Headers["header"] = head;
  28. //request.Proxy = new WebProxy("192.168.1.12",80);
  29. byte[] bytes = Encoding.UTF8.GetBytes(body);
  30. request.ContentType = "application/json; charset=UTF-8"; ;//窗体数据被编码为名称/值对形式
  31. //request.ContentType = "application/json";
  32. request.ContentLength = bytes.Length;
  33. Stream myResponseStream = request.GetRequestStream();
  34. myResponseStream.Write(bytes, 0, bytes.Length);
  35. HttpWebResponse response = (HttpWebResponse)request.GetResponse();
  36. StreamReader myStreamReader = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
  37. string retString = myStreamReader.ReadToEnd();
  38. myStreamReader.Close();
  39. myResponseStream.Close();
  40. if (response != null)
  41. {
  42. response.Close();
  43. }
  44. if (request != null)
  45. {
  46. request.Abort();
  47. }
  48. return retString;//返回响应报文
  49. }
  50. }
  51. }