You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
63 lines
2.0 KiB
63 lines
2.0 KiB
using System;
|
|
using System.IO;
|
|
using System.Net;
|
|
|
|
namespace FangYar.Common
|
|
{
|
|
/// <summary>
|
|
/// Http请求工具类
|
|
/// </summary>
|
|
public class HttpRequestUtil
|
|
{
|
|
|
|
/// <summary>
|
|
/// 根据网址获取数据
|
|
/// </summary>
|
|
/// <param name="contentUrl"></param>
|
|
/// <returns></returns>
|
|
public static string GetPostUrl(string contentUrl)
|
|
{
|
|
try
|
|
{
|
|
HttpWebRequest reqContent = (HttpWebRequest)WebRequest.Create(contentUrl);
|
|
reqContent.Method = "POST";
|
|
reqContent.AllowAutoRedirect = false;//服务端重定向。一般设置false
|
|
reqContent.ContentType = "application/x-www-form-urlencoded";//数据一般设置这个值,除非是文件上传
|
|
HttpWebResponse respContent = (HttpWebResponse)reqContent.GetResponse();
|
|
string html = new StreamReader(respContent.GetResponseStream()).ReadToEnd();
|
|
return html;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return ex + "";
|
|
}
|
|
|
|
}
|
|
/// <summary>
|
|
/// 根据网址获取数据
|
|
/// </summary>
|
|
/// <param name="contentUrl"></param>
|
|
/// <returns></returns>
|
|
public static string GetUrl(string contentUrl)
|
|
{
|
|
try
|
|
{
|
|
HttpWebRequest reqContent = (HttpWebRequest)WebRequest.Create(contentUrl);
|
|
reqContent.Method = "GET";
|
|
reqContent.AllowAutoRedirect = false;//服务端重定向。一般设置false
|
|
reqContent.ContentType = "application/x-www-form-urlencoded";//数据一般设置这个值,除非是文件上传
|
|
HttpWebResponse respContent = (HttpWebResponse)reqContent.GetResponse();
|
|
string html = new StreamReader(respContent.GetResponseStream()).ReadToEnd();
|
|
return html;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return ex + "";
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
}
|
|
|