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