C# 发HTTP请求

本文介绍了一种使用 C# 发送 HTTP POST 请求的方法,并展示了如何处理响应数据。此外,还提供了通过 HttpClient 类发送 GET 和 POST 请求的示例代码。

记录一下,调试使用


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;
        }





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


 

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;
            }

使用HTTP CLIENT的方式


public string Get(string url)
        {
            try
            {
                using (var httpClient = new HttpClient())
                {
                    var response = httpClient.GetStringAsync(new Uri(url)).Result;
                    return response;
                }
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }

        public string Post<T>(string url, T obj)
        {
            try
            {
                using (var client = new HttpClient())
                {
                    var content = new StringContent(JsonConvert.SerializeObject(obj));
                    content.Headers.ContentType = new MediaTypeHeaderValue("application/json");

                    var response = client.PostAsync(url, content).Result;
                    return response.Content.ReadAsStringAsync().Result;
                }
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值