Ejemplo n.º 1
0
        public static Encoding GetEncoder(ENCODING_TYPES type)
        {
            switch (type)
            {
            case ENCODING_TYPES.Ascii:
                return(Encoding.ASCII);

            case ENCODING_TYPES.BigEndianUnicode:
                return(Encoding.BigEndianUnicode);

            case ENCODING_TYPES.Unicode:
                return(Encoding.Unicode);

            case ENCODING_TYPES.Utf32:
                return(Encoding.UTF32);

            case ENCODING_TYPES.Utf7:
                return(Encoding.UTF7);

            case ENCODING_TYPES.Utf8:
                return(Encoding.UTF8);

            default:
                return(Encoding.UTF8);
            }
        }
Ejemplo n.º 2
0
// ----------------------------------------------

        public bool post(ref Response Response, string Url, ArrayList Parameters, Hashtable Files, FILE_COMPRESSION_METHODS FileCompressionMethods, ENCODING_TYPES EncType, REQUEST_METHODS ReqMethod, int Timeout, string UserAgent, string Referer, CookieContainer Cookies, CredentialCache CredentialCache)
        {
            return post(ref Response, Url, Parameters, Files, UserAgent, EncType, ReqMethod, Timeout, Cookies, CredentialCache, FileCompressionMethods, Referer);
        }
Ejemplo n.º 3
0
// ----------------------------------------------

        private bool post(ref Response Response, string Url, ArrayList Parameters, Hashtable Files, string UserAgent, ENCODING_TYPES EncType, REQUEST_METHODS ReqMethod, int Timeout, CookieContainer Cookies, CredentialCache CredentialCache, FILE_COMPRESSION_METHODS FileCompressionMethods, string Referer)
        {
            if (!object.Equals(null, Url)) Info("URL: " + Url);
            if (!object.Equals(null, Files)) Info("Files: " + Files.Count.ToString());
            if (!object.Equals(null, EncType)) Info("Encode Type: " + EncType.ToString());
            if (!object.Equals(null, ReqMethod)) Info("Request Method: " + ReqMethod.ToString());
            if (!object.Equals(null, Referer) && !object.Equals(String.Empty, Referer)) Info("Referer: " + Referer);
            //Info(req.Headers.ToString());
            //Info(s2);

            System.Net.ServicePointManager.Expect100Continue = false;
            //string newLine = "\r\n";

            string boundary = Guid.NewGuid().ToString().Replace("-", "");
            MemoryStream postData = new MemoryStream();
            string urlEncoded = "";

            switch (EncType)
            {
                case ENCODING_TYPES.APP_X_WWW_FORM_URLENCODED:
                    urlEncoded = Url + "?" + getPostDataUrlEncoded(Parameters, Url, boundary);
                    break;
                case ENCODING_TYPES.MULTI_FORM_DATA:
                    postData = getPostData(Files, Parameters, Url, boundary, FileCompressionMethods);
                    break;
            }

            if (EncType == ENCODING_TYPES.APP_X_WWW_FORM_URLENCODED)
            {
                Url = urlEncoded;
            }

            HttpWebRequest req = (new MyWebRequest().GetWebRequest2(new System.Uri(Url)) as HttpWebRequest);
            switch (EncType)
            {
                case ENCODING_TYPES.APP_X_WWW_FORM_URLENCODED:
                    req.ContentType = "application/x-www-form-urlencoded;";
                    break;
                case ENCODING_TYPES.MULTI_FORM_DATA:
                    req.ContentType = "multipart/form-data; boundary=" + boundary;
                    break;
            }
            switch (ReqMethod)
            {
                case REQUEST_METHODS.GET:
                    req.Method = "GET";
                    break;
                case REQUEST_METHODS.POST:
                    req.Method = "POST";
                    break;
            }
            if (!object.Equals(null, Referer))
            {
                req.Referer = Referer;
            }
            req.UnsafeAuthenticatedConnectionSharing = true;
            req.CookieContainer = Cookies;

            if (!object.Equals(null, Cookies))
            {
                req.CookieContainer = Cookies;
            }
            if (!object.Equals(null, CredentialCache))
            {
                req.Credentials = CredentialCache;
            }
            req.UserAgent = UserAgent;
            req.AllowWriteStreamBuffering = false;
            req.Accept = "application/x-shockwave-flash,text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5";
            req.ContentLength = postData.Length;
            req.Timeout = Timeout;
            if (ReqMethod == REQUEST_METHODS.POST)
            {
                try
				{
					Stream oRequestStream = req.GetRequestStream(); // CRASH OCCURS HERE IF NO NETWORK AVAILABLE!

    	            postData.Seek(0, SeekOrigin.Begin);

	                int bytesSent = 0;
                	int totalBytesSent = 0;
            	    int totalOutBytes = (int)postData.Length;
        	        byte[] buffer = new Byte[checked((uint)Math.Min(4096, (int)postData.Length))];
    	            while ((bytesSent = postData.Read(buffer, 0, buffer.Length)) != 0)
	                {
                	    oRequestStream.Write(buffer, 0, bytesSent);
            	        totalBytesSent += bytesSent;
        	            fireSendProgressEvent(totalBytesSent, (int)postData.Length);
    	            }
	                oRequestStream.Close();
				}
				catch (Exception e)
				{
					Info("GETREQUESTSTREAM: " + e.Message);
					return false;
				}
            }
            Info("Request Sent.  ( " + postData.Length.ToString("N0") + " Bytes )");

            HttpWebResponse lHttpWebResponse;
            Stream lHttpWebResponseStream;
            byte[] byteBuffer = new byte[1024];
            int bytesRead;
            try
            {
                lHttpWebResponse = (HttpWebResponse)req.GetResponse();

                Response.CharacterSet = lHttpWebResponse.CharacterSet;
                Response.ContentEncoding = lHttpWebResponse.ContentEncoding;
                Response.ContentType = lHttpWebResponse.ContentType;
                Response.Cookies = lHttpWebResponse.Cookies;
                Response.Headers = lHttpWebResponse.Headers;
                Response.IsFromCache = lHttpWebResponse.IsFromCache;
                Response.IsMutuallyAuthenticated = lHttpWebResponse.IsMutuallyAuthenticated;
                Response.LastModified = lHttpWebResponse.LastModified;
                Response.Method = lHttpWebResponse.Method;
                Response.ProtocolVersion = lHttpWebResponse.ProtocolVersion;
                Response.ResponseUri = lHttpWebResponse.ResponseUri;
                Response.Server = lHttpWebResponse.Server;
                Response.StatusCode = lHttpWebResponse.StatusCode;
                Response.StatusDescription = lHttpWebResponse.StatusDescription;

                lHttpWebResponseStream = req.GetResponse().GetResponseStream();
                Int32 ContentLength = Convert.ToInt32(lHttpWebResponse.ContentLength);
                int progress = 0;
                Response.Content = new MemoryStream();
                do
                {
                    bytesRead = lHttpWebResponseStream.Read(byteBuffer, 0, 1024);
                    Response.Content.Write(byteBuffer, 0, bytesRead);
                    progress += bytesRead;

                    if (progress <= ContentLength)
                    {
                        fireReceiveProgressEvent(progress, ContentLength);
                    }
                    else
                    {
                        fireReceiveProgressEvent(ContentLength, ContentLength);
                    }
                } while (bytesRead > 0);
                lHttpWebResponseStream.Close();
                postData.Close();
                lHttpWebResponse.Close();
                Info("Response Received.  ( " + ContentLength.ToString("N0") + " Bytes )");
                return true;
            }
            catch (Exception e)
            {
                Info(e.Message);
                return false;
            }

        }
Ejemplo n.º 4
0
// ----------------------------------------------

        public bool post(ref Response Response, string Url, ArrayList Parameters, Hashtable Files, FILE_COMPRESSION_METHODS FileCompressionMethods, ENCODING_TYPES EncType, REQUEST_METHODS ReqMethod, int Timeout)
        {
            return post(ref Response, Url, Parameters, Files, this.UserAgent, EncType, ReqMethod, Timeout, this.CookieJar, this.CredentialCache, FileCompressionMethods, "");
        }