加载中…
个人资料
  • 博客等级:
  • 博客积分:
  • 博客访问:
  • 关注人气:
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
正文 字体大小:

ashx一般处理程序使用HttpRequest发起HTTP请求

(2022-03-17 09:55:41)
 //Get请求方式
https://www.cnblogs.com/Images/OutliningIndicators/None.gif
    private string RequestGet(string Url)
https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif    
{
https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif         
string PageStr = string.Empty;//用于存放还回的html
https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif
        Uri url = new Uri(Url);//Uri类 提供统一资源标识符 (URI) 的对象表示形式和对 URI 各部分的轻松访问。就是处理url地址
https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif
        try
https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif        
{
https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif             HttpWebRequest httprequest 
= (HttpWebRequest)WebRequest.Create(url);//根据url地址创建HTTpWebRequest对象
https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif
            参数设置
https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif             HttpWebResponse response 
= (HttpWebResponse)httprequest.GetResponse();//使用HttpWebResponse获取请求的还回值
https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif
            Stream steam = response.GetResponseStream();//从还回对象中获取数据流
https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif
            StreamReader reader = new StreamReader(steam, Encoding.GetEncoding("gb2312"));//读取数据Encoding.GetEncoding("gb2312")指编码是gb2312,不让中文会乱码的
https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif
            PageStr = reader.ReadToEnd();
https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif             reader.Close();
https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif         }

https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif         
catch (Exception e)
https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif        
{
https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif             PageStr 
+= e.Message;
https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif         }

https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif         
return PageStr;
https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif     }


 //post请求方式
private string RequestPost(string Url,string Context)//两个参数分别是Url地址和Post过去的数据
https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif
    {
https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif         
string PageStr=string.Empty;
https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif         Uri url 
= new Uri(Url);
https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif         
byte[] reqbytes=Encoding.ASCII.GetBytes(Context);
https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif         
try
https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif        
{
https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif             HttpWebRequest req 
= (HttpWebRequest)WebRequest.Create(url);
https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif             req.Method 
= "post";
https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif             req.ContentType 
= "application/x-www-form-urlencoded";
https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif             req.ContentLength 
= reqbytes.Length;
https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif             Stream stm 
= req.GetRequestStream();
https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif             stm.Write(reqbytes, 
0reqbytes.Length);
https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif             
https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif             stm.Close();
https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif             HttpWebResponse wr 
= (HttpWebResponse)req.GetResponse();
https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif             Stream stream 
= wr.GetResponseStream();
https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif             StreamReader srd
= new StreamReader(stream,Encoding.GetEncoding("gb2312"));
https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif             PageStr 
+= srd.ReadToEnd();
https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif             stream.Close();
https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif             srd.Close();
https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif         }

https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif         
catch (Exception e)
https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif        
{
https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif             PageStr 
+= e.Message;
https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif         }

https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif         
return PageStr; 
https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif     }
//多个参数配置

 private string RequestPost(string Url, IDictionary parameters)//两个参数分别是Url地址和Post过去的数据
        {
            string PageStr = string.Empty;
            Uri url = new Uri(Url);
          //  byte[] reqbytes = Encoding.ASCII.GetBytes(Context);

            byte[] reqbytes = Encoding.UTF8.GetBytes(BuildQuery(parameters, "utf8"));
            try
            {
                HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
                req.Method = "post";
                req.ContentType = "application/x-www-form-urlencoded;charset=utf-8";
               // req.ContentType = "application/x-www-form-urlencoded";
                req.ContentLength = reqbytes.Length;
                Stream stm = req.GetRequestStream();
                stm.Write(reqbytes, 0, reqbytes.Length);

                stm.Close();
                HttpWebResponse wr = (HttpWebResponse)req.GetResponse();
                Stream stream = wr.GetResponseStream();
                StreamReader srd = new StreamReader(stream, Encoding.GetEncoding("gb2312"));
                PageStr += srd.ReadToEnd();
                stream.Close();
                srd.Close();
            }
            catch (Exception e)
            {
                PageStr += e.Message;
            }
            return PageStr;
        }


                Dictionary parameters = new Dictionary(); //参数列表
                parameters.Add("studentID", studentID);
                parameters.Add("expmID", expmID);


 //组装请求参数
        private string BuildQuery(IDictionary parameters, string encode)
        {
            StringBuilder postData = new StringBuilder();
            bool hasParam = false;
            IEnumerator> dem = parameters.GetEnumerator();
            while (dem.MoveNext())
            {
                string name = dem.Current.Key;
                string value = dem.Current.Value;
                // 忽略参数名或参数值为空的参数
                if (!string.IsNullOrEmpty(name))
                {
                    if (hasParam)
                    {
                        postData.Append("&");
                    }
                    postData.Append(name);
                    postData.Append("=");
                    if (encode == "gb2312")
                    {
                        postData.Append(HttpUtility.UrlEncode(value, Encoding.GetEncoding("gb2312")));
                    }
                    else if (encode == "utf8")
                    {
                        postData.Append(HttpUtility.UrlEncode(value, Encoding.UTF8));
                    }
                    else
                    {
                        postData.Append(value);
                    }
                    hasParam = true;
                }
            }
            return postData.ToString();
        }



假设当前页完整地址是:http://www.test.com/aaa/bbb.aspx?id=5&name=kelli

协议名----http://
域名  ---- www.test.com
站点名---aaa        System.Web.HttpContext.Current.Request.ApplicationPath
页面名(文件名) ----bbb.aspx
参数------id=5&name=kelli

1、完整url (协议名+域名+站点名+文件名+参数)
string url=Request.Url.ToString();
url= http://www.test.com/aaa/bbb.aspx?id=5&name=kelli
2、站点名+页面名+参数
string url=Request.RawUrl;(或 string url=Request.Url.PathAndQuery;)
url= /aaa/bbb.aspx?id=5&name=kelli
3、站点名+页面名
string url=HttpContext.Current.Request.Url.AbsolutePath;
(或 string url= HttpContext.Current.Request.Path;)
url= aaa/bbb.aspx
4、域名
string url=HttpContext.Current.Request.Url.Host;
url= www.test.com
5、参数
string url= HttpContext.Current.Request.Url.Query;
url= ?id=5&name=kelli
6、站点名      System.Web.HttpContext.Current.Request.ApplicationPath

url协议+域名+端口号+站点名

string url System.Web.HttpContext.Current.Request.Url.Scheme "://" +
                        System.Web.HttpContext.Current.Request.Url.Host ":" +
                        System.Web.HttpContext.Current.Request.Url.Port +
                            System.Web.HttpContext.Current.Request.ApplicationPath;

0

阅读 收藏 喜欢 打印举报/Report
  

新浪BLOG意见反馈留言板 欢迎批评指正

新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 产品答疑

新浪公司 版权所有