C# get或者post获取网页内容

本文介绍了一种通过GET和POST请求获取远程网页内容的方法。利用HttpWebRequest发起HTTP请求,并通过StreamReader读取响应流来获取网页内容。支持指定编码格式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  1. ///<summary>  
  2.         /// 获取指定url的请求内容  
  3.         ///</summary>  
  4.         ///<param name="Url"></param>  
  5.         ///<param name="encode"></param>  
  6.         ///<returns></returns>  
  7.         public static string GetRemoteHtmlCodeByEncoding(string Url, string encode)  
  8.         {  
  9.             HttpWebResponse Result = null;  
  10.   
  11.             try  
  12.             {  
  13.                 HttpWebRequest Req = (HttpWebRequest) System.Net.WebRequest.Create(Url);  
  14.                 Req.Method = "get";  
  15.                 Req.ContentType = "application/x-www-form-urlencoded";  
  16.                 Req.Credentials = CredentialCache.DefaultCredentials;  
  17.                 Result = (HttpWebResponse) Req.GetResponse();  
  18.   
  19.                 StreamReader ReceiveStream = new StreamReader(Result.GetResponseStream(), Encoding.GetEncoding(encode));  
  20.                 string OutPutString;  
  21.                 try  
  22.                 {  
  23.                     OutPutString = ReceiveStream.ReadToEnd();  
  24.                 }  
  25.                 catch  
  26.                 {  
  27.                     OutPutString = string.Empty;  
  28.                 }  
  29.                 finally  
  30.                 {  
  31.                     ReceiveStream.Close();  
  32.                 }  
  33.   
  34.                 return OutPutString;  
  35.             }  
  36.             catch  
  37.             {  
  38.                 return string.Empty;  
  39.             }  
  40.             finally  
  41.             {  
  42.                 if (Result != null)  
  43.                 {  
  44.                     Result.Close();  
  45.                 }  
  46.             }  
  47.         }  


[csharp]  view plain  copy
  1. /// <summary>  
  2.         /// 以POST方式抓取远程页面内容  
  3.         /// </summary>  
  4.         /// <param name="url">请求的url</param>  
  5.         /// <param name="postData">参数列表</param>  
  6.         /// <param name="encodeType">编码类型</param>  
  7.         /// <returns></returns>  
  8.         public static string Post_Http(string url, string postData, string encodeType)  
  9.         {  
  10.             HttpWebResponse myResponse = null;  
  11.             string strResult = null;  
  12.             try  
  13.             {  
  14.                 Encoding encoding = Encoding.GetEncoding(encodeType);  
  15.                 byte[] POST = encoding.GetBytes(postData);  
  16.                 HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(url);  
  17.                 myRequest.Method = "POST";  
  18.                 myRequest.ContentType = "application/x-www-form-urlencoded";  
  19.                 myRequest.ContentLength = POST.Length;  
  20.                 Stream newStream = myRequest.GetRequestStream();  
  21.                 newStream.Write(POST, 0, POST.Length); //设置POST  
  22.                 newStream.Close();  
  23.                 // 获取结果数据  
  24.                 myResponse = (HttpWebResponse)myRequest.GetResponse();  
  25.                 StreamReader reader = new StreamReader(myResponse.GetResponseStream(), Encoding.Default);  
  26.                 strResult = reader.ReadToEnd();  
  27.             }  
  28.             catch (Exception ex)  
  29.             {  
  30.                 strResult = ex.Message;  
  31.             }  
  32.             finally  
  33.             {  
  34.                 if (myResponse != null)  
  35.                     myResponse.Close();  
  36.             }  
  37.             return strResult;  
  38.         }  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值