Winform传递参数至Url并获得返回值

在winform里面调用URL并传递参数主要使用HttpWebRequest对象,分为两种形式post和get,个人感觉就是模拟了浏览器,

1.post方法:

 /// <summary>
        /// 使用post方法,调用短信接口
        /// </summary>
        /// <param name="PhoneNumber"></param>
        /// <param name="SmsContent"></param>
        /// <param name="PhoneNumberType"></param>
        /// <param name="SmsUser"></param>
        private void CallMsgCenterToSendMsgPost(string PhoneNumber, string SmsContent, string PhoneNumberType, string SmsUser)
        {
            string formUrl = ConfigurationSettings.AppSettings["formUrl"].ToString().Trim();//url地址
            string formData = "PhoneNumber=" + PhoneNumber + "&SmsContent=" +SmsContent + "&PhoneNumberType=" + PhoneNumberType + "&SmsUser=" + SmsUser + "";
            CookieContainer cookieContainer = new CookieContainer();
            // 将提交的字符串数据转换成字节数组 
            byte[] postData = Encoding.UTF8.GetBytes(formData);
            
            // 设置提交的相关参数 
            HttpWebRequest request = WebRequest.Create(formUrl) as HttpWebRequest;
            Encoding myEncoding = Encoding.GetEncoding("gb2312");  
            request.Method = "POST";
            request.KeepAlive = false;
            request.AllowAutoRedirect = true;
            request.ContentType = "application/x-www-form-urlencoded";
            request.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 2.0.50727; .NET CLR 3.0.04506.648; .NET CLR 3.5.21022; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729)";
            request.CookieContainer = cookieContainer;
            request.ContentLength = postData.Length;

            // 提交请求数据 
            System.IO.Stream outputStream = request.GetRequestStream();
            outputStream.Write(postData, 0, postData.Length);
            outputStream.Close();

            HttpWebResponse response;
            Stream responseStream;
            StreamReader reader;
            string srcString;
            response = request.GetResponse() as HttpWebResponse;
            responseStream = response.GetResponseStream();
            reader = new System.IO.StreamReader(responseStream, Encoding.UTF8);
            srcString = reader.ReadToEnd();
            reader.Close();
        }

2,get方法

 /// <summary>
        /// 使用get方法
        /// </summary>
        /// <param name="PhoneNumber"></param>
        /// <param name="SmsContent"></param>
        /// <param name="PhoneNumberType"></param>
        /// <param name="SmsUser"></param>
        private void CallMsgCenterToSendMsgGet(string PhoneNumber, string SmsContent, string PhoneNumberType, string SmsUser)
        {
            string formUrl = ConfigurationSettings.AppSettings["formUrl"].ToString().Trim();
            string formData = "PhoneNumber=" + HttpUtility.UrlEncode(PhoneNumber) + "&SmsContent=" + HttpUtility.UrlEncode(SmsContent) + "&PhoneNumberType=" + HttpUtility.UrlEncode(PhoneNumberType) + "&SmsUser=" + HttpUtility.UrlEncode(SmsUser) + "";
            CookieContainer cookieContainer = new CookieContainer();

            formUrl = formUrl + "?" + formData;
            // 设置提交的相关参数 
            HttpWebRequest request = WebRequest.Create(formUrl) as HttpWebRequest;
          
            request.Method = "GET";
            request.KeepAlive = false;
            request.AllowAutoRedirect = true;
            request.ContentType = "application/x-www-form-urlencoded";
            request.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 2.0.50727; .NET CLR 3.0.04506.648; .NET CLR 3.5.21022; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729)";
            request.CookieContainer = cookieContainer;


            HttpWebResponse SendSMSResponse = (HttpWebResponse)request.GetResponse();


            StreamReader SendSMSResponseStream = new StreamReader(SendSMSResponse.GetResponseStream());

            string strRespone = SendSMSResponseStream.ReadToEnd();

            SendSMSResponse.Close();
            SendSMSResponseStream.Close();   
        }

3,调用起来都比较简单的,主要是在传递参数的过程中注意中文的处理,否则会变为乱码,获取返回值当然就使用HttpWebResponse对象了

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值