HttpUtil tool and javahttputil Tool

Source: Internet
Author: User

HttpUtil tool and javahttputil Tool

Code:

Using System; using System. collections. generic; using System. IO; using System. linq; using System. net; using System. text; namespace SunCreate. combatPlatform. server. comLib {// <summary> /// upload and download an object over Http // </summary> public class HttpUtil {# region cookie sets private static CookieContainer m_Cookie = new CookieContainer (); public static void SetHttpCookie (CookieContainer cookie) {m_Cookie = cookie;} # endregion # region HttpDownloadFile download file public static MemoryStream HttpDownloadFile (string url) {// set the parameter HttpWebRequest request = WebRequest. create (url) as HttpWebRequest; request. method = "GET"; request. cookieContainer = m_Cookie; // send a request and obtain the response data HttpWebResponse response = request. getResponse () as HttpWebResponse; // wait until request. the GetResponse () program starts to send the Post request Stream responseStream = response to the target webpage. getResponseStream (); // create the write stream MemoryStream stream = new MemoryStream (); byte [] bArr = new byte [1024]; int size = responseStream. read (bArr, 0, (int) bArr. length); while (size> 0) {stream. write (bArr, 0, size); size = responseStream. read (bArr, 0, (int) bArr. length);} stream. seek (0, SeekOrigin. begin); responseStream. close (); return stream ;} # endregion # region HttpUploadFile Upload File // <summary> // Http Upload File /// </summary> public static string HttpUploadFile (string url, byte [] bArr, string fileName) {// set the parameter HttpWebRequest request = WebRequest. create (url) as HttpWebRequest; CookieContainer cookieContainer = new CookieContainer (); request. cookieContainer = cookieContainer; request. allowAutoRedirect = true; request. method = "POST"; string boundary = DateTime. now. ticks. toString ("X"); // random separator request. contentType = "text/plain; charset = UTF-8"; request. cookieContainer = m_Cookie; Stream postStream = request. getRequestStream (); postStream. write (bArr, 0, bArr. length); postStream. close (); // send the request and obtain the response data HttpWebResponse response = request. getResponse () as HttpWebResponse; // wait until request. the GetResponse () program starts to send the Post request Stream instream = response to the target webpage. getResponseStream (); StreamReader sr = new StreamReader (instream, Encoding. UTF8); // returned result webpage (html) code string content = sr. readToEnd (); return content ;}# endregion # region HttpPost /// <summary> /// HttpPost /// </summary> public static string HttpPost (string url, string data) {byte [] bArr = ASCIIEncoding. UTF8.GetBytes (data); // set the parameter HttpWebRequest = WebRequest. create (url) as HttpWebRequest; request. cookieContainer = m_Cookie; request. method = "POST"; request. contentType = "text/plain; charset = UTF-8"; request. contentLength = bArr. length; Stream postStream = request. getRequestStream (); postStream. write (bArr, 0, bArr. length); postStream. close (); // send the request and obtain the response data HttpWebResponse response = request. getResponse () as HttpWebResponse; // wait until request. the GetResponse () program starts to send the Post request Stream instream = response to the target webpage. getResponseStream (); StreamReader sr = new StreamReader (instream, Encoding. UTF8); // returned result webpage (html) code string content = sr. readToEnd (); return content ;}# endregion # region HttpPost /// <summary> /// HttpPost /// </summary> public static string HttpPost (string url) {// set the parameter HttpWebRequest request = WebRequest. create (url) as HttpWebRequest; request. cookieContainer = m_Cookie; request. method = "POST"; request. contentType = "text/plain; charset = UTF-8"; // send the request and obtain the response data HttpWebResponse response = request. getResponse () as HttpWebResponse; // wait until request. the GetResponse () program starts to send the Post request Stream instream = response to the target webpage. getResponseStream (); StreamReader sr = new StreamReader (instream, Encoding. UTF8); // returned result webpage (html) code string content = sr. readToEnd (); return content ;}# endregion # region HttpGet /// <summary> /// HttpGet /// </summary> public static string HttpGet (string url) {// set the parameter HttpWebRequest request = WebRequest. create (url) as HttpWebRequest; request. cookieContainer = m_Cookie; request. method = "GET"; request. contentType = "text/plain; charset = UTF-8"; // send the request and obtain the response data HttpWebResponse response = request. getResponse () as HttpWebResponse; // wait until request. the GetResponse () program starts to send the Post request Stream instream = response to the target webpage. getResponseStream (); StreamReader sr = new StreamReader (instream, Encoding. UTF8); // returned result webpage (html) code string content = sr. readToEnd (); return content ;}# endregion }}View Code

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.