当前位置: 移动技术网 > IT编程>开发语言>.net > Csharp:HttpWebRequest

Csharp:HttpWebRequest

2019年08月02日  | 移动技术网IT编程  | 我要评论

破跨境被拐妇女案,甘肃恭迎佛祖舍利,斗牛犬图片

        /// <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));  //返回成功
        }

  

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网