码迷,mamicode.com
首页 > Windows程序 > 详细

C#常用的HttpGet HttpPost HttpDownload方法类

时间:2016-06-29 14:45:04      阅读:339      评论:0      收藏:0      [点我收藏+]

标签:

自己写了几个常用的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方法类

标签:

原文地址:http://www.cnblogs.com/Shirlix/p/5626724.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!