C#常用的HttpGet HttpPost HttpDownload方法类
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C#常用的HttpGet HttpPost HttpDownload方法类相关的知识,希望对你有一定的参考价值。
自己写了几个常用的HTTP方法,贴出来方便自己查找。
using System.Text;
using System.Net;
using System.IO;
namespace Test
{
public class HttpMethod
{
public static string HttpGet(string URL)
{
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(URL);
//req.Proxy = new WebProxy(ProxyString, true); //true means no proxy
WebResponse resp = null;
req.Timeout = 2000;
try
{
resp = (HttpWebResponse)req.GetResponse();
}
catch
{
req.Abort();
req = null;
return "timeout";
}
StreamReader sr = new StreamReader(resp.GetResponseStream(), Encoding.Default);
string sReturn = sr.ReadToEnd().Trim();
resp.Close(); sr.Close();
return sReturn;
}
public static string HttpPost(string URL, string Parameters)
{
byte[] bytes = Encoding.Default.GetBytes(Parameters);
ServicePointManager.DefaultConnectionLimit = 200;
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(URL);
//req.Proxy = new WebProxy(ProxyString, true);
req.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.8) Gecko/20051111 Firefox/1.5";
req.Accept = "text/xml,application/xml,application/xhtml+xml,text/html";
req.KeepAlive = false;
//req.Referer = string.Format("https://10.116.24.197");
req.ContentType = "application/x-www-form-urlencoded";
req.Method = "POST";
req.ContentLength = bytes.Length;
req.Timeout = 3000;
Stream os = null;
try
{
os = req.GetRequestStream();
}
catch
{
req.Abort();
req = null;
return "timeout";
}
os.Write(bytes, 0, bytes.Length); //Push it out there
os.Close();
try
{
HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
if (resp == null) return null;
StreamReader sr = new StreamReader(resp.GetResponseStream(), Encoding.UTF8);
string sReturn = sr.ReadToEnd().Trim();
req.Abort();
req = null;
resp.Close();
resp = null;
sr.Close();
return sReturn;
}
catch
{
req.Abort();
req = null;
return "timeout";
}
}
public static void HttpDownloadFile(string url, string path)
{
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
WebResponse resp = null;
req.Timeout = 3000;
try
{
resp = (HttpWebResponse)req.GetResponse();
}
catch
{
req.Abort();
req = null;
return;
}
Stream respStm = resp.GetResponseStream();
int end = path.LastIndexOf(@"\");
string dir = path.Substring(0, end);
if (!Directory.Exists(dir))
Directory.CreateDirectory(dir);
Stream stream = new FileStream(path, FileMode.Create);
byte[] bArr = new byte[1024];
int size = respStm.Read(bArr, 0, (int)bArr.Length);
while (size > 0)
{
stream.Write(bArr, 0, size);
size = respStm.Read(bArr, 0, (int)bArr.Length);
}
stream.Close();
respStm.Close();
}
}
Example:
HttpMethod.HttpGet("http://10.116.24.197/test.php?data=This a Test");
HttpMethod.HttpPost("http://10.116.24.197/test", "data=This a Test");
HttpMethod.HttpDownloadFile("http://10.116.24.197/test/test.txt", @"D:\TestDownload\test.txt");
}
以上是关于C#常用的HttpGet HttpPost HttpDownload方法类的主要内容,如果未能解决你的问题,请参考以下文章
将变量从 [HttpPost] 方法传递给 [HttpGet] 方法
android中httppost和httpget方法的区别? [复制]
httpClient Post例子,Http 四种请求访问代码 HttpGet HttpPost HttpPut HttpDelete