首页 > 代码库 > 两种方式模拟Post
两种方式模拟Post
1、HttpWebRequest
public static string DoPost( string target, string content ) { try { string parameter = "reg=" + regNo + "&pwd=" + password + "&sourceadd=" + "&phone=" + target + "&content=" + HttpUtility.UrlEncode(content, Encoding.UTF8); byte[] data =http://www.mamicode.com/ Encoding.ASCII.GetBytes(parameter); HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(channelUrl); request.Method = "POST"; request.ContentType = "application/x-www-form-urlencoded;charset=uft-8"; request.ContentLength = data.Length; using (Stream stream = request.GetRequestStream()) { stream.Write(data, 0, data.Length); } using (WebResponse response = request.GetResponse()) { using (StreamReader sr = new StreamReader(response.GetResponseStream(), Encoding.UTF8)) { return sr.ReadToEnd(); } } } catch (Exception e) { logger.Info("失败" + e.Message); } return ""; }
2、WebClient
public string DoPost(string url, string param) { try { byte[] requestData = http://www.mamicode.com/Encoding.GetEncoding("UTF-8").GetBytes(param); WebClient client = new WebClient(); client.Encoding = Encoding.UTF8; client.Headers.Add("Content-Type", "application/json"); client.Headers.Add("ContentLength", requestData.Length.ToString()); byte[] responseData = http://www.mamicode.com/client.UploadData(url, "POST", requestData); string replaystr = Encoding.GetEncoding("UTF-8").GetString(responseData); return replaystr; } catch (Exception e) { return null; } }
两种方式模拟Post
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。