c#发送http请求

直接代码,自己备用

    /**
         * @method:生成验证码
         */
        [JSONMethod]
        [Description ( "生成验证码" )]
        [DomTemplate ( )]
        public object GenerateVerify(IDictionary condition)
        {
            string username = "myname";
            string password = "mypass";
            string phone = "13566655555";
        string content = "";
string vercode = ""; //generate verifyCode here Random random = new Random(); for (int i = 0; i < 6; i++) { int n = (int)(random.Next(0, 10)); vercode += n; } IDictionary cond = new Dictionary<string,string>(); cond.Add("vercode", vercode); //--------------------------------------------- content += "【魔兽科技】欢迎您注册魔兽网,您此次注册验证码是:"+vercode+",千万不要告诉别人哦!"; System.Net.WebRequest request = System.Net.WebRequest.Create("http://www.tosms.cn/Api/SendSms.ashx"); request.Method = "POST"; string postData = "username="+username+"&password="+password+"&phones="+phone+"&content="+content; byte[] byteArray = System.Text.Encoding.UTF8.GetBytes (postData); request.ContentType = "application/x-www-form-urlencoded"; request.ContentLength = byteArray.Length; System.IO.Stream dataStream = request.GetRequestStream (); // Write the data to the request stream. dataStream.Write (byteArray, 0, byteArray.Length); dataStream.Close (); WebResponse response = request.GetResponse (); Console.WriteLine (((HttpWebResponse)response).StatusDescription); dataStream = response.GetResponseStream (); System.IO.StreamReader reader = new System.IO.StreamReader (dataStream); // Read the response status,wish:"1"==成功 string responseFromServer = reader.ReadToEnd (); reader.Close (); dataStream.Close (); response.Close (); return cond; }

 

posted @ 2015-11-18 02:58  君子笑而不语  阅读(457)  评论(0编辑  收藏  举报