zoukankan      html  css  js  c++  java
  • WebClient 数据传输

    数据提交 post  ,get

    public string WebClientPost(string PostData, string PostUrl, string Type)
      {
          string postString = PostData;
          byte[] postData = Encoding.UTF8.GetBytes(postString);//编码,尤其是汉字,事先要看下抓取网页的编码方式  
          string url = PostUrl;//地址  
          WebClient webClient = new WebClient();
          byte[] responseData = null;
          if (Type == "post")
          {
              webClient.Headers.Add("Content-Type", "application/x-www-form-urlencoded");//采取POST方式必须加的header,如果改为GET方式的话就去掉这句话即可  
              responseData = webClient.UploadData(url, "POST", postData);//得到返回字符流  
          }
          else
          {
              responseData = webClient.UploadData(url, "GET", postData);//得到返回字符流  
          }
          string srcString = Encoding.UTF8.GetString(responseData);//解码 
          return srcString;
      }

    文件解析

      1转为JObject

                string _Result = PostData(Appid, AppKey, imgSrc, "", "post");
                JObject jsonData = JObject.Parse(_Result);
                JArray jsonFace = jsonData.GetValue("face") as JArray;
                if (jsonFace == null)
                {
                    msg = "err" + "," + "格式不对";
                }
                else
                {
    
                    int height = 0;
                    int width = 0;
                    int center_x = 0;
                    int center_y = 0;
                    string sex = "";
                    if (jsonFace != null && jsonFace.Count > 0)
                    {
                        height = int.Parse(jsonFace[0]["position"]["height"].ToString());
                        width = int.Parse(jsonFace[0]["position"]["width"].ToString());
                        center_x = int.Parse(jsonFace[0]["position"]["center"]["x"].ToString());
                        center_y = int.Parse(jsonFace[0]["position"]["center"]["y"].ToString());
                        sex = jsonFace[0]["attribute"]["gender"].ToString();
                    }
    }

    2   Dictionary

     Dictionary<string, object> objJson = JsonConvert.DeserializeObject<Dictionary<string, object>>(request);
                string state = objJson["state"].ToString();

    数据下载

    string imgSrc = context.Request.Form["ImgUrl"].ToString();  //服务器连接 
    
    string FileDir = "/PublicResource/";
               string AppName = System.Configuration.ConfigurationManager.AppSettings["AppName"];
     
               if (!string.IsNullOrEmpty(AppName))
               {
                   FileDir = "/PublicResource/" + AppName + "/";
               }
               Guid NewId = Guid.NewGuid();
               WebClient wc = new WebClient();
               string WXPath = context.Server.MapPath(FileDir + NewId.ToString() + ".jpg");
               wc.DownloadFile(imgSrc, WXPath);
  • 相关阅读:
    Andrew Ng机器学习算法入门((七):特征选择和多项式回归
    Andrew Ng机器学习算法入门((六):多变量线性回归方程求解
    Andrew Ng机器学习算法入门((五):矩阵和向量
    Linux常用命令集锦
    Andrew Ng机器学习算法入门(四):阶梯下降算法
    AZscaaner源码解读之数据库连接(一)
    Andrew Ng机器学习算法入门(三):线性回归算法
    Andrew Ng机器学习算法入门(二):机器学习分类
    MapServer
    fetch.js
  • 原文地址:https://www.cnblogs.com/shuaif/p/6121108.html
Copyright © 2011-2022 走看看