WebClient 數據傳輸

數據提交 post  ,getweb

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;
  }

文件解析json

  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   Dictionaryapp

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

 

 

數據下載post

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);
相關文章
相關標籤/搜索