c# http协议 web api发送,获取方法-获取网页数据

  public static HttpWebResponse CreatePostHttpResponse(string url, string json, ref string erro)
        {
            Console.WriteLine(url);
            HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;//创建请求对象
            request.Method = "POST";//请求方式
            request.ContentType = "application/json";//链接类型
            try
            {
                if (!(json == null))
                {
                    Console.WriteLine(json);
                    byte[] data = Encoding.UTF8.GetBytes(json);

                    //写入请求流
                    using (Stream stream = request.GetRequestStream())
                    {
                        stream.Write(data, 0, data.Length);
                    }
                }
                erro = "订单请已发送....";
                return request.GetResponse() as HttpWebResponse;
            }
            catch (Exception ex)
            {
                erro = ex.ToString();
                erro = "error";
                return null;
            }                                       //构造查询字符串

        }

  获取:

 public static string HttpGetString_orderSequence(string AGV_Name, ref string orderSequence, ref string OrderFinishTime)
        {
            //  Url = "http://172.17.50.40:7120/api/route/vehicles/AMB-01" + Url;
            string query_URL;
            query_URL = "http://172.17.50.40:7120/api/route/vehicles/" + AGV_Name;
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(query_URL);
            request.Method = "GET";
            request.ContentType = "text/html;charset=UTF-8";
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            Stream myResponseStream = response.GetResponseStream();
            StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("utf-8"));
            string retString = myStreamReader.ReadToEnd();
            myStreamReader.Close();
            myResponseStream.Close();

            dynamic jsonStr = JsonConvert.DeserializeObject<dynamic>(retString);
            //  jsonObj["destinations"][0]["locationName"] = $"{where_name}";
            orderSequence = jsonStr["orderSequence"];
           
            if (jsonStr["state"] == "FINISHED")
                retString = "true";
            else
                retString = "false";
            return retString;
        }

 

posted @ 2021-07-21 17:05  txwtech  阅读(1419)  评论(0编辑  收藏  举报