WebClient 資料轉送

來源:互聯網
上載者:User

標籤:encoding   gui   set   ppa   request   ret   map   cstring   資料轉送   

資料提交 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);

 

WebClient 資料轉送

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.