zoukankan      html  css  js  c++  java
  • C#中Post和Get提交

    1、Post提交

     private string PostWebRequest(string Url, string paramData, string dataEncode)
            {
                string ret = string.Empty;
                try
                {
                    Encoding myEncoding = Encoding.GetEncoding(dataEncode);
                    byte[] byteArray = myEncoding.GetBytes(paramData); //转化
                    HttpWebRequest webReq = (HttpWebRequest)WebRequest.Create(Url);
                    webReq.Method = “POST”;
                    webReq.ContentType = "application/x-www-form-urlencoded";
    
                    webReq.ContentLength = byteArray.Length;
                    Stream newStream = webReq.GetRequestStream();
                    newStream.Write(byteArray, 0, byteArray.Length);//写入参数
                    newStream.Close();
                    HttpWebResponse response = (HttpWebResponse)webReq.GetResponse();
                    StreamReader sr = new StreamReader(response.GetResponseStream(), myEncoding);
                    ret = sr.ReadToEnd();
                    sr.Close();
                    response.Close();
                }
                catch (Exception ex)
                {
                    WriteFileLog(ex.ToString());
                    MessageBox.Show(ex.Message);
                }
                return ret;
            }

    2、Get提交

       private string GetWebRequest(string Url, string paramData, string dataEncode)
            {
                string requestString = "";
                try
                {
                    string reallyUrl = Url;
    
                    Encoding myEncoding = Encoding.GetEncoding(dataEncode);
                    // CookieContainer cookieContainer = new CookieContainer();
                    HttpWebRequest request = WebRequest.Create(reallyUrl) as HttpWebRequest;
                    //  ServicePointManager.CheckCertificateRevocationList = false;
                    request.Method = “GET”;
                    //   request.KeepAlive = false;
                    //   request.AllowAutoRedirect = true;
                    request.ContentType = "application/x-www-form-urlencoded";
    
                    HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                    StreamReader responseStream = new StreamReader(response.GetResponseStream(), myEncoding);
                    requestString = responseStream.ReadToEnd();
                    response.Close();
                    responseStream.Close();
                }
                catch (Exception ex)
                {
    
                    WriteFileLog(ex.ToString());
                    MessageBox.Show(ex.Message);
                }
  • 相关阅读:
    使用dtaidistance实现dtw算法(二)
    一元二次曲线拟合Excel用
    使用dtaidistance实现dtw算法
    一元二次曲线拟合
    爬取豆瓣电影top250
    pandasgui
    就业生态解析篇——数据预处理代码部分
    爬取51job招聘信息(一)
    负载均衡-LVS
    负载均衡
  • 原文地址:https://www.cnblogs.com/zxiong/p/4463752.html
Copyright © 2011-2022 走看看