• C# Post请求方法


    //1测试有效(Web端)
    public
    static string HttpPost(string sim_uid, string mobile, string msg) { string _url = string.Format("需要请求的url"); //json参数 string jsonParam = Newtonsoft.Json.JsonConvert.SerializeObject(new { uid = sim_uid,//用戶openid mobile = mobile, msg = msg }); var request = (HttpWebRequest)WebRequest.Create(_url); request.Method = "POST"; request.ContentType = "application/json;charset=UTF-8";//ContentType byte[] byteData = Encoding.UTF8.GetBytes(jsonParam); int length = byteData.Length; request.ContentLength = length; Stream writer = request.GetRequestStream(); writer.Write(byteData, 0, length); writer.Close(); var response = (HttpWebResponse)request.GetResponse(); var responseString = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding("utf-8")).ReadToEnd(); return responseString.ToString(); }
            //2post(未测试)
         public static string PostReqiest(string url, Dictionary<string, string> dic) { string result = ""; ServicePointManager.Expect100Continue = true; ServicePointManager.CheckCertificateRevocationList = true; ServicePointManager.DefaultConnectionLimit = 100; ServicePointManager.SecurityProtocol = (SecurityProtocolType)(0xc0 | 0x300 | 0xc00); ServicePointManager.ServerCertificateValidationCallback = delegate { return true; }; HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url); req.ProtocolVersion = HttpVersion.Version10; req.KeepAlive = false; req.Method = "POST"; req.ContentType = "application/json"; #region 添加Post 参数 StringBuilder builder = new StringBuilder(); int i = 0; foreach (var item in dic) { if (i > 0) builder.Append("&"); builder.AppendFormat("{0}={1}", item.Key, item.Value); i++; } byte[] data = Encoding.UTF8.GetBytes(builder.ToString()); req.ContentLength = data.Length; using (Stream reqStream = req.GetRequestStream()) { reqStream.Write(data, 0, data.Length); reqStream.Close(); } #endregion HttpWebResponse resp = (HttpWebResponse)req.GetResponse(); Stream stream = resp.GetResponseStream(); //获取响应内容 using (StreamReader reader = new StreamReader(stream, Encoding.UTF8)) { result = reader.ReadToEnd(); } return result; }

    1.Get请求(winform端)
    string strURL = "http://localhost/WinformSubmit.php?tel=11111&name=张三";
    System.Net.HttpWebRequest request;
    // 创建一个HTTP请求
    request = (System.Net.HttpWebRequest)WebRequest.Create(strURL);
    //request.Method="get";
    System.Net.HttpWebResponse response;
    response = (System.Net.HttpWebResponse)request.GetResponse();
    System.IO.StreamReader myreader = new System.IO.StreamReader(response.GetResponseStream(), Encoding.UTF8);
    string responseText = myreader.ReadToEnd();
    myreader.Close();
    MessageBox.Show(responseText);


    2.Post请求(winform端) string strURL = "http://localhost/WinformSubmit.php"; System.Net.HttpWebRequest request; request = (System.Net.HttpWebRequest)WebRequest.Create(strURL); //Post请求方式 request.Method = "POST"; // 内容类型 request.ContentType = "application/x-www-form-urlencoded"; // 参数经过URL编码 string paraUrlCoded = System.Web.HttpUtility.UrlEncode("keyword"); paraUrlCoded += "=" + System.Web.HttpUtility.UrlEncode("多月"); byte[] payload; //将URL编码后的字符串转化为字节 payload = System.Text.Encoding.UTF8.GetBytes(paraUrlCoded); //设置请求的 ContentLength request.ContentLength = payload.Length; //获得请 求流 System.IO.Stream writer = request.GetRequestStream(); //将请求参数写入流 writer.Write(payload, 0, payload.Length); // 关闭请求流 writer.Close(); System.Net.HttpWebResponse response; // 获得响应流 response = (System.Net.HttpWebResponse)request.GetResponse(); System.IO.StreamReader myreader = new System.IO.StreamReader(response.GetResponseStream(), Encoding.UTF8); string responseText = myreader.ReadToEnd(); myreader.Close(); MessageBox.Show(responseText);
  • 相关阅读:
    SQL Server死锁总结
    dao层知识点总结
    减少数据库资源开销
    java string(2)
    java读写锁实现数据同步访问
    并发集合(转)
    JDBC在Java Web中的应用——分页查询
    jdbc分页
    jdbc如何锁定某一条数据或者表,不让别人操作?
    数据库锁机制
  • 原文地址:https://www.cnblogs.com/romanticcrystal/p/15588378.html
Copyright © 2020-2023  润新知