欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Csharp:HttpWebRequest

程序员文章站 2022-06-10 17:50:13
/// /// Define other methods and classes here /// /// /// /// public static Task MakeAsyncRequest(string contentType, string contenttxt, ... ......
        /// <summary>
        ///  define other methods and classes here
        /// </summary>
        /// <param name="url"></param>
        /// <param name="contenttype"></param>
        /// <returns></returns>
        public static task<string> makeasyncrequest(string contenttype, string contenttxt, string mobile)
        {
            string url = "http://www.dusystem.com/userserviceapi";
            httpwebrequest request = (httpwebrequest)webrequest.create(url);
            request.contenttype = contenttype;
            request.method = webrequestmethods.http.post; //get
            request.timeout = 20000;
            request.proxy = null;
            byte[] data = system.text.encoding.getencoding("gbk").getbytes("geovindu"); //system.text.unicodeencoding.ascii.getbytes("geovindu");//utf8  
            base64encoder myencoder = new base64encoder(data);
            stringbuilder sb = new stringbuilder();
            sb.append(myencoder.getencoded());
            string msg = urlencode(contenttxt);            
            string content = string.format("method={0}&islongsms={1}&username={2}&password={3}&smstype={4}&mobile={5}&content={6}", "ms", 0, "geovindu", sb.tostring(), 0, mobile, msg);//
            byte[] bytes = encoding.getencoding("gbk").getbytes(content);
            request.contentlength = bytes.length;
            stream os = request.getrequeststream();
            //req.getresponseasync();

            os.write(bytes, 0, bytes.length);
            os.close();
            //system.net.webresponse resp = req.getresponse();
            webresponse resp = request.getresponse();
            
            task<webresponse> task = task.factory.fromasync(
                request.begingetresponse,
                asyncresult => request.endgetresponse(asyncresult),
                (object)null);

            return task.continuewith(t => readstreamfromresponse(resp, content));
        }

        /// <summary>
        /// 对内容进行编码
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        private static string urlencode(string str)
        {
            stringbuilder sb = new stringbuilder();
            byte[] bystr = system.text.encoding.getencoding("gbk").getbytes(str); //默认是system.text.encoding.default.getbytes(str)
            for (int i = 0; i < bystr.length; i++)
            {
                sb.append(@"%" + convert.tostring(bystr[i], 16));
            }

            return (sb.tostring());
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="response"></param>
        /// <returns></returns>
        private static string readstreamfromresponse(webresponse response, string content)
        {
            stream responsestream = response.getresponsestream();
            using (streamreader sr = new streamreader(responsestream))
            {
                string strcontent = sr.readtoend();
                return strcontent;
            }
        }



        delegate string synchoperation(string value);
        /// <summary>
        /// 
        /// </summary>
        /// <param name="callback"></param>
        /// <param name="value"></param>
         void beginthesynchronousoperation(asynccallback callback, string value)
        {
            synchoperation op = new synchoperation(synchronousoperation);
            op.begininvoke(value, callback, op);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
         string synchronousoperation(string value)
        {

            string str = "geovindu";
            thread.sleep(10000);
            str = str + value;
            return str;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="result"></param>
         void callbackoperation(iasyncresult result)
        {
            // get your delegate
            var ar = result.asyncstate as synchoperation;
            // end invoke and get value
            var returned = ar.endinvoke(result);
            response.write(returned);
            
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void button1_click(object sender, eventargs e)
        {
           beginthesynchronousoperation(callbackoperation, this.textbox1.text.trim());
            var task = makeasyncrequest("application/x-www-form-urlencoded", "geovindu", "1388888888");
            response.write(string.format("got response of {0}", task.result));  //返回成功
        }