• 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;
            }
    欢迎讨论,相互学习。 txwtech@163.com
  • 相关阅读:
    PHP查找服务器某个目录下的文件
    CentOS添加用户,管理员权限
    centos图形化桌面安装过程
    正则表达式匹配查询
    Windows远程桌面多用户登录的问题
    VM虚拟机Android安装图形界面
    Ubuntu 根目录作用
    win7个性化不能换界面:此页面上的一个或多个设置已被系统管理员禁用,关机里的切换用户和锁定为灰色
    springboot 使用 jedis 连接 Redis 数据库
    JAVA 中 Map 与实体类相互转换的简单方法
  • 原文地址:https://www.cnblogs.com/txwtech/p/15040389.html
Copyright © 2020-2023  润新知