zoukankan      html  css  js  c++  java
  • csharp: HttpWebRequest and HttpWebResponse

    http://stackoverflow.com/questions/4015324/http-request-with-post

     Response.Charset = "GBK";  //GBK  //gb2312
                this.CodePage = 936;
                Session.CodePage = 936;
                Encoding myEncoding = Encoding.GetEncoding("GBK");
                byte[] data = System.Text.Encoding.GetEncoding("GBK").GetBytes("907953"); //System.Text.UnicodeEncoding.ASCII.GetBytes("135246");//UTF8
                Base64Encoder myEncoder = new Base64Encoder(data);
                StringBuilder sb = new StringBuilder();
                sb.Append(myEncoder.GetEncoded());
                string content = HttpUtility.UrlEncode("塗聚文:需要還3本書的時間為:2014-07-12:15:58,csharp", myEncoding);
                String sResult = "http://www2.lansea.com.cn/servlet/UserServiceAPI?method=sendSMS&isLongSms=0&username=87&password=232&smstype=0&mobile=13824350518&content=" + content + "";
      
                //string param = HttpUtility.UrlEncode(sResult, myEncoding);
                Response.Write(sResult);
    
    
                byte[] postBytes = Encoding.GetEncoding("GBK").GetBytes(sResult); // Encoding.GetEncoding("GBK").GetBytes(sResult);
                try
                {
    
                    HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(sResult);//sResult
                    req.Method = "POST";
                    req.ContentType = "application/x-www-form-urlencoded;charset=GBK";
                    req.Headers["Accept-Charset"] = "GBK";
                    req.Headers["Accept-Language"] = "zh-CN,zh;q=0.";
                    req.AllowAutoRedirect = false;
                    req.ContentLength = postBytes.Length;
                    Stream webStream = req.GetRequestStream(); //发送数据 
                    webStream.Write(postBytes, 0, postBytes.Length);
                    webStream.Close();
                    //获取返回数据
                    HttpWebResponse webResponse = (HttpWebResponse)req.GetResponse();
                    StreamReader reader = new StreamReader(webResponse.GetResponseStream(), Encoding.GetEncoding("GBK"));
                    sResult = reader.ReadToEnd();
                    sResult.Trim();
                    this.TextBox1.Text = sResult;//返回是否发送成功的结果
                }
                catch (Exception ex)
                {
                    ex.Message.ToString();
                    //sResult = "";
                    //return sResult;
                }
    
  • 相关阅读:
    day84
    模型层之单表操作
    Django的模板层
    Django框架导读
    创建Django项目
    名称空间2.0path
    js基础之BOM和DOM
    LG5003 跳舞的线
    20191003 「HZOJ NOIP2019 Round #8」20191003模拟
    LG3092 「USACO2013NOV」No Change 状压DP
  • 原文地址:https://www.cnblogs.com/geovindu/p/3833979.html
Copyright © 2011-2022 走看看