c# 简单httpGet、httpPost方法以及http提交Form表单

httpGet和httpPost

public static class HttpHelper
{
	/// 
	/// post
	/// 
	/// 请求地址
	/// 参数
	/// string返回
	public static string PostMethod(string postUrl, string paras)
	{
		string result = "";
		string resultStr = string.Empty;
		if (string.IsNullOrWhiteSpace(postUrl))
		{
			resultStr = "发送地址为空";
		}
		else
		{
			string targetUrl = postUrl.Trim().ToString();
			try
			{
				UTF8Encoding encoding = new UTF8Encoding();
				byte[] postData = encoding.GetBytes(paras);
				HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(targetUrl);
				myRequest.Method = "POST";
				myRequest.ContentType = "application/json;charset=UTF-8";
				myRequest.ContentLength = postData.Length;
				Stream newStream = myRequest.GetRequestStream();
				newStream.Write(postData, 0, postData.Length);
				newStream.Flush();
				newStream.Close();
				HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse();
				StreamReader reader = new StreamReader(myResponse.GetResponseStream(), Encoding.UTF8);
				result = reader.ReadToEnd();
			}
			catch (Exception ex)
			{
				result = ex.Message;
			}
		}
		return result;
	}

	/// 
	/// get请求方式
	/// 
	/// 请求地址
	/// string返回
	public static string GetMethod(string getUrl)
	{
		string result = "";
		if (string.IsNullOrWhiteSpace(getUrl))
		{
			result = "发送地址不能为空";
		}
		else
		{
			string targetUrl = getUrl.Trim().ToString();
			try
			{
				HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(targetUrl);
				myRequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)";
				myRequest.Method = "GET";
				myRequest.Timeout = 30 * 60 * 1000;
				HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse();
				Stream newStream = myResponse.GetResponseStream();
				StreamReader reader = new StreamReader(newStream, Encoding.UTF8);
				result = reader.ReadToEnd();
			}
			catch (Exception ex)
			{
				result = ex.Message;
			}
		}
		return result;
	}
}

c# 提交FormData表单数据

注意:UserAgent 必须设置

public static string PostFormMethod(string url, T value)
{
	var modelType = typeof(T);
	string boundary = "--------------------------" + DateTime.Now.Ticks.ToString("x");
	string boundary2 = "--" + boundary;
	HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
	request.UserAgent = "My Platform";//重要,重要,重要!!!必须要设置的,不设置会报错
	request.Method = "POST";
	request.ContentType = $"multipart/form-data; boundary=---{boundary}";
	StringBuilder postDataStr = new StringBuilder();
	foreach (var item in modelType.GetProperties())
	{
		postDataStr.Append(boundary2 + "\r\n");
		postDataStr.Append("Content-Disposition: form-data; name=\"" + item.Name + "\"" + "\r\n\r\n");
		postDataStr.Append((item.GetValue(value)?.ToString() ?? "") + "\r\n");
	}
	postDataStr.Append(boundary2 + "--" + "\r\n");

	Stream myRequestStream = request.GetRequestStream();
	StreamWriter myStreamWriter = new StreamWriter(myRequestStream, System.Text.Encoding.UTF8);
	myStreamWriter.Write(postDataStr.ToString());
	myStreamWriter.Close();

	HttpWebResponse response = (HttpWebResponse)request.GetResponse();
	Stream myResponseStream = response.GetResponseStream();
	StreamReader myStreamReader = new StreamReader(myResponseStream, System.Text.Encoding.UTF8);
	string retString = myStreamReader.ReadToEnd();
	myStreamReader.Close();
	myResponseStream.Close();

	return retString;
}

你可能感兴趣的:(c#)