using System;
using System.IO;
using System.Net;
using System.Text;
using System.Web;
namespace HP.Common
{
/// <summary>
/// 信息
/// </summary>
public class InfoCollect
{
/// <summary>
/// 获取内网页面内容
/// </summary>
/// <param name="url"></param>
/// <returns></returns>
public static string GetPageByInner(string url)
{
return GetPage("http://" + HttpContext.Current.Request.ServerVariables["SERVER_NAME"] + (HttpContext.Current.Request.ServerVariables["SERVER_PORT"] == "80" ? "" : ":" + HttpContext.Current.Request.ServerVariables["SERVER_PORT"]) + url);
}
/// <summary>
/// 获取公网url内容(GET)
/// </summary>
/// <param name="url"></param>
/// <returns></returns>
public static string GetPage(string url)
{
return GetPage(url, "GET", null);
}
/// <summary>
/// 获取公网url内容(Post方法)
/// </summary>
/// <param name="url"></param>
/// <param name="postData"></param>
/// <returns></returns>
public static string GetPage(string url, string postData)
{
return GetPage(url, "POST", postData);
}
/// <summary>
/// 获取公网url内容
/// </summary>
/// <param name="url"></param>
/// <param name="method"></param>
/// <param name="postData"></param>
/// <returns></returns>
private static string GetPage(string url, string method, string postData)
{
WebResponse result = null;
var resultstring = "";
try
{
var req = WebRequest.Create(url);
req.Timeout = 30000;
//POST方法
if (!string.IsNullOrEmpty(method) && method.ToUpper() == "POST")
{
req.Method = "POST";
byte[] byteArray = Encoding.UTF8.GetBytes(postData);
req.ContentType = "application/x-www-form-urlencoded";
req.ContentLength = byteArray.Length;
var dataStream = req.GetRequestStream();
dataStream.Write(byteArray, 0, byteArray.Length);
dataStream.Close();
}
result = req.GetResponse();
if (result != null)
{
var receiveStream = result.GetResponseStream();
var strEncod = result.ContentType;
StreamReader sr = null;
if (strEncod.ToLower().IndexOf("utf") != -1)
{
if (receiveStream != null) sr = new StreamReader(receiveStream, Encoding.UTF8);
}
else
{
if (receiveStream != null) sr = new StreamReader(receiveStream, Encoding.Default);
}
if (sr != null) resultstring = sr.ReadToEnd();
}
}
catch
{
throw new Exception();
}
finally
{
if (result != null)
{
result.Close();
}
}
return resultstring;
}
}
}