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);
                }
  • 相关阅读:
    JavaScript实现接口的三种经典方式
    javascript实现继承3种方式: 原型继承、借用构造函数继承、组合继承,模拟extends方法继承
    JavaScript简单重写构造器的原型
    cocos2d-x中的宏定义CC_PROPERTY
    CCCallFunc CCCallFuncN CCCallFuncND的区别和使用
    action(二)
    action(一)
    CShopDialog类
    cocos2d-x与ISO内存管理(转)
    CGameConfig类
  • 原文地址:https://www.cnblogs.com/zxiong/p/4463752.html
Copyright © 2011-2022 走看看