Beispiel #1
0
        /// <summary>
        /// Posts data to a specified url. Note that this assumes that you have already url encoded the post data.
        /// </summary>
        /// <param name="postData">The data to post.</param>
        /// <param name="url">the url to post to.</param>
        /// <returns>Returns the result of the post.</returns>
        private PostResult PostData(string url, string postData)
        {
            PostResult     result  = new PostResult();
            HttpWebRequest request = null;

            if (WebInfo == null)
            {
                throw new ArgumentException("WebInfo cant be <null>!", "WebInfo");
            }

            if (mType == PostTypeEnum.Get & postData.Length > 0)
            {
                url += "?" + postData;
            }

            Uri uri = new Uri(url);

            request                 = (HttpWebRequest)WebRequest.Create(uri);
            request.Credentials     = CredentialCache.DefaultCredentials;
            request.CookieContainer = new CookieContainer();
            if (mIgnoreCookies == false)
            {
                request.CookieContainer.GetCookies(uri);
            }
            if (Cookies != null)
            {
                request.CookieContainer.Add(this.Cookies);
            }
            if (Proxy != null)
            {
                request.Proxy = Proxy;
            }
            if (mTimeout > 0)
            {
                request.Timeout = mTimeout;
            }

            request.UserAgent = WebInfo.Useragent;
            request.Accept    = WebInfo.RequestAccept;
            request.Headers.Add(HttpRequestHeader.AcceptLanguage, WebInfo.AcceptLanguage);
            request.Headers.Add(HttpRequestHeader.AcceptEncoding, WebInfo.AcceptEncoding);
            request.Headers.Add(HttpRequestHeader.AcceptCharset, WebInfo.AcceptCharset);
            request.Headers.Add(HttpRequestHeader.KeepAlive, WebInfo.KeepAlive.ToString());
            request.Referer   = mUrlReferer;
            request.KeepAlive = true;
            System.Net.ServicePointManager.Expect100Continue = false;             // sonst error


            request.Method = mType.ToString().ToUpper();

            if (mType == PostTypeEnum.Post)
            {
                request.ContentType   = "application/x-www-form-urlencoded";
                request.ContentLength = postData.Length;

                try {
                    if (postData.Length > 0)
                    {
                        Stream       writeStream = request.GetRequestStream();
                        UTF8Encoding encoding    = new UTF8Encoding();
                        byte[]       bytes       = encoding.GetBytes(postData);
                        writeStream.Write(bytes, 0, bytes.Length);
                        writeStream.Close();
                    }
                } catch { }
            }

            try {
                using (HttpWebResponse response = (HttpWebResponse)request.GetResponse()) {
                    result.Cookies = response.Cookies;
                    using (Stream responseStream = response.GetResponseStream()) {
                        using (StreamReader readStream = new StreamReader(responseStream, Encoding.Default)) {
                            result.ResponseString = readStream.ReadToEnd();
                            if (response.ContentEncoding == "gzip")
                            {
                                result.ResponseString = Util.GZipDecompress(result.ResponseString);
                            }
                            else if (response.ContentEncoding == "deflate")
                            {
                                throw new NotSupportedException("Deflate-compression income - not yet finished :/");
                            }
                        }
                    }
                }
            } catch (Exception e) {
                System.Diagnostics.Debug.WriteLine(e);
                result.ResponseString = string.Empty;
            }
            return(result);
        }
Beispiel #2
0
		/// <summary>
		/// Posts data to a specified url. Note that this assumes that you have already url encoded the post data.
		/// </summary>
		/// <param name="postData">The data to post.</param>
		/// <param name="url">the url to post to.</param>
		/// <returns>Returns the result of the post.</returns>
		private PostResult PostData( string url, string postData ) {
			PostResult result = new PostResult();
			HttpWebRequest request = null;
			if( WebInfo == null )
				throw new ArgumentException( "WebInfo cant be <null>!", "WebInfo" );

			if( mType == PostTypeEnum.Get & postData.Length > 0 ) 
				url += "?" + postData;

			Uri uri = new Uri( url );
			request = (HttpWebRequest)WebRequest.Create( uri );
			request.Credentials = CredentialCache.DefaultCredentials;
			request.CookieContainer = new CookieContainer();
			if( mIgnoreCookies == false )
				request.CookieContainer.GetCookies( uri );
			if( Cookies != null )
				request.CookieContainer.Add( this.Cookies );
			if( Proxy != null )
				request.Proxy = Proxy;
			if( mTimeout > 0 )
				request.Timeout = mTimeout;

			request.UserAgent = WebInfo.Useragent;
			request.Accept = WebInfo.RequestAccept;
			request.Headers.Add( HttpRequestHeader.AcceptLanguage, WebInfo.AcceptLanguage );
			request.Headers.Add( HttpRequestHeader.AcceptEncoding, WebInfo.AcceptEncoding );
			request.Headers.Add( HttpRequestHeader.AcceptCharset, WebInfo.AcceptCharset );
			request.Headers.Add( HttpRequestHeader.KeepAlive, WebInfo.KeepAlive.ToString() );
			request.Referer = mUrlReferer;
			request.KeepAlive = true;
			System.Net.ServicePointManager.Expect100Continue = false; // sonst error


			request.Method = mType.ToString().ToUpper();

			if( mType == PostTypeEnum.Post ) {
				request.ContentType = "application/x-www-form-urlencoded";
				request.ContentLength = postData.Length;

				try {
					if( postData.Length > 0 ) {
						Stream writeStream = request.GetRequestStream();
						UTF8Encoding encoding = new UTF8Encoding();
						byte[] bytes = encoding.GetBytes( postData );
						writeStream.Write( bytes, 0, bytes.Length );
						writeStream.Close();
					}
				} catch { }
			}

			try {
				using( HttpWebResponse response = (HttpWebResponse)request.GetResponse() ) {
					result.Cookies = response.Cookies;
					using( Stream responseStream = response.GetResponseStream() ) {
						using( StreamReader readStream = new StreamReader( responseStream, Encoding.Default ) ) {
							result.ResponseString = readStream.ReadToEnd();
							if( response.ContentEncoding == "gzip" )
								result.ResponseString = Util.GZipDecompress( result.ResponseString );
							else if( response.ContentEncoding == "deflate" )
								throw new NotSupportedException( "Deflate-compression income - not yet finished :/" );
						}

					}
				}
			} catch( Exception e ) {
				System.Diagnostics.Debug.WriteLine( e );
				result.ResponseString = string.Empty;
			}
			return result;
		}