首页 > 学院 > 开发设计 > 正文

csharp: HttpWebRequest and HttpWebResponse

2019-11-17 01:37:38
字体:
来源:转载
供稿:网友

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;            }


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表