前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >C# 发HTTP请求

C# 发HTTP请求

原创
作者头像
用户7705674
修改2021-11-03 10:00:59
5180
修改2021-11-03 10:00:59
举报
文章被收录于专栏:css小迷妹
代码语言:javascript
复制
protected void btnSend_Click(object sender, EventArgs e)

            {

            string url = "http://localhost:3547/waplocation.aspx";  
            string mobileNo = this.txtMobileNo.Text;  

            StringBuilder reqStr = new StringBuilder(100);  
            reqStr.Append("reqtype=" + txtReqType.Text + "&mobile=" + mobileNo);  
            //reqStr.Append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");  
            //reqStr.Append("<request>");  
            //reqStr.Append("<head><reqtype>" +txtReqType.Text +"</reqtype></head>");  
            //reqStr.Append("<body>");  
            //reqStr.Append("<mobiles>");  
            //reqStr.Append("<mobile>" + mobileNo +"</mobile>");  
            //reqStr.Append("</mobiles>");  
            //reqStr.Append("</body>");  
            //reqStr.Append("</request>");  

            string postData = reqStr.ToString();  

            ASCIIEncoding encoding = new ASCIIEncoding();  
            byte[] data = encoding.GetBytes(postData);  
            HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(url);  

            myRequest.Method = "POST";  
            myRequest.ContentType = "application/x-www-form-urlencoded";  
            myRequest.ContentLength = data.Length;  
            Stream newStream = myRequest.GetRequestStream();  


            newStream.Write(data, 0, data.Length);  
            newStream.Close();  

            HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse();  
            StreamReader reader = new StreamReader(myResponse.GetResponseStream(), Encoding.Default);  
            string content = reader.ReadToEnd();  
            txtResult.Text = content;  
        }  </pre> 
 

 如果公司设置了代理,可以这样:
    try

                {

                    string postData = "";

                ASCIIEncoding encoding = new ASCIIEncoding();  
                byte[] data = encoding.GetBytes(postData);  
                WebProxy wp = new WebProxy("proxy Address");  
                wp.Credentials = new System.Net.NetworkCredential("username", "password","domain");  




                HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(url);  
                myRequest.Proxy = wp;   

                myRequest.Method = "POST";  
                myRequest.ContentType = "application/x-www-form-urlencoded";  
                myRequest.ContentLength = data.Length;  
                Stream newStream = myRequest.GetRequestStream();  


                newStream.Write(data, 0, data.Length);  
                newStream.Close();  


                HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse();  
                StreamReader reader = new StreamReader(myResponse.GetResponseStream(), Encoding.Default);  
                string content = reader.ReadToEnd();  
                return content;  
            }  

            catch(Exception ex){  
                return string.Empty;  
            }  

 
</pre> 

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档