Exemplo n.º 1
0
        private void Send(string serializedErrorReport)
        {
            var request = _webRequestCreator.Create(_apiUri);

            request.Method      = "POST";
            request.ContentType = "application/x-www-form-urlencoded";
            request.Headers["X-BugSense-Api-Key"] = _apiKey;

            using (var stream = request.GetRequestStream())
                using (var requestStream = new StreamWriter(stream))
                {
                    requestStream.Write(serializedErrorReport);
                }

            var response = request.GetResponse();

            //var content = new StreamReader(response.GetResponseStream()).ReadToEnd();
            //Console.WriteLine(content);
            //foreach (var header in response.Headers)
            //{
            //    Console.WriteLine(header);
            //}
            // TODO: Handle response...?

            //string text = string.Empty;
            //var responseStream = response.GetResponseStream();
            //if (responseStream != null)
            //{
            //    var streamReader = new StreamReader(responseStream);
            //    text = streamReader.ReadToEnd();
            //    streamReader.Close();
            //}
        }
Exemplo n.º 2
0
	// This method will attempt to create a particular subclass of WebRequest
	// based on the scheme from the uri passed in. Currently on HttpWebRequest
	// is supported.
	public static WebRequest Create(Uri requestUri)
	{

		IWebRequestCreate theCreator = null;
		
		if(CheckUriValidity(requestUri, false))
		{

			// Check here to see if the Uri scheme exists in the
			// prefixes table and if so, then return back the
			// proper WebRequest for it
			theCreator = (prefixes[requestUri.Scheme] as IWebRequestCreate);
			
			if(theCreator!=null)
			{
				return theCreator.Create(requestUri);
			}

		// TODO: this client does not have the permission to connect to the URI or
		// the URI that the request is redirected to.
		// throw new SecurityException("requestUriString");
		}
		   
		return null;
	}
        public string Generate()
        {
            var webRequest = _webRequestCreate == null
                ? WebRequest.Create(GenerateUri)
                : _webRequestCreate.Create(GenerateUri);

            webRequest.Proxy   = WebRequest.GetSystemWebProxy();
            webRequest.Method  = "GET";
            webRequest.Timeout = 1 * 60 * 1000;

            HttpWebResponse webResponse;

            try
            {
                webResponse = (HttpWebResponse)webRequest.GetResponse();
            }
            catch (WebException ex)
            {
                if (ex.Response == null)
                {
                    throw;
                }
                webResponse = (HttpWebResponse)ex.Response;
            }

            if (webResponse.StatusCode != HttpStatusCode.OK)
            {
                throw new HttpRequestException("Failed request to API");
            }

            return(ParseResponse(webResponse));
        }
Exemplo n.º 4
0
        /// <summary>
        /// Using PackWebRequest to retrieve the photo from photo package on
        /// archive server.
        /// </summary>
        /// <param name="packageUri">
        /// Absolute Uri of the photo package on archive server.
        /// </param>
        /// <param name="partUri">
        /// Part Uri of the photo in the photo package.
        /// </param>
        /// <returns>
        /// WebResponse that has the stream of requested photo image.
        /// </returns>
        private static WebResponse PackWebRequestForPhotoPart(Uri packageUri,
                                                              Uri partUri)
        {
            Uri fullUri = PackUriHelper.Create(packageUri, partUri);

            Console.WriteLine("Pack URI= " + fullUri);

            // Create PackWebRequest.
            WebRequest request = _webRequestCreate.Create(fullUri);

            return(request.GetResponse());
        }
Exemplo n.º 5
0
        private Stream Get(string uri, string accept)
        {
            var request = (HttpWebRequest)web_request_creator.Create(new Uri(uri));

            if (accept != null)
            {
                request.Accept = accept;
            }
            request.UserAgent         = LastfmCore.UserAgent;
            request.Timeout           = 10000;
            request.KeepAlive         = false;
            request.AllowAutoRedirect = true;

            HttpWebResponse response = null;

            try {
                response = (HttpWebResponse)request.GetResponse();
            } catch (WebException e) {
                Log.DebugException(e);
                response = (HttpWebResponse)e.Response;
            }
            return(response != null?response.GetResponseStream() : null);
        }
Exemplo n.º 6
0
        public void BrowserHttp()
        {
            IWebRequestCreate wrc = WebRequestCreator.BrowserHttp;

            Assert.AreEqual(wrc.GetType().ToString(), "System.Net.Browser.BrowserHttpWebRequestCreator", "Type");

            WebRequest wr = wrc.Create(Uri);

            Assert.AreEqual(wr.GetType().ToString(), "System.Net.Browser.BrowserHttpWebRequest", "Type");

            Assert.AreSame(wrc, wr.CreatorInstance, "CreatorInstance");

            // default so we don't register it - since we can do so only one time!
            // Assert.IsTrue (WebRequest.RegisterPrefix ("http://", WebRequestCreator.BrowserHttp), "RegisterPrefix");
        }
Exemplo n.º 7
0
        public static WebRequest Create(Uri uri)
        {
            if (uri == null)
            {
                throw new ArgumentNullException("uri");
            }
            if (!uri.IsAbsoluteUri)
            {
                throw new InvalidOperationException("Uri is not absolute.");
            }

            IWebRequestCreate creator = null;

            // first we look if a domain is registred
            string scheme = uri.Scheme + Uri.SchemeDelimiter;
            string domain = scheme + uri.DnsSafeHost;

            if (!registred_prefixes.TryGetValue(domain, out creator))
            {
                // next we look if the protocol is registred (the delimiter '://' is important)
                if (!registred_prefixes.TryGetValue(scheme, out creator))
                {
                    scheme = uri.Scheme;                     // without the delimiter
                    // then we default to SL
                    switch (scheme)
                    {
                    case "http":
                    case "https":
                        creator = default_creator;
                        break;

                    default:
                        registred_prefixes.TryGetValue(scheme, out creator);
                        break;
                    }
                }
            }

            if (creator == null)
            {
                throw new NotSupportedException(string.Format("Scheme {0} not supported", scheme));
            }

            return(creator.Create(uri));
        }
Exemplo n.º 8
0
        public static WebRequest Create(Uri requestUri)
        {
            if (requestUri == null)
            {
                throw new ArgumentNullException("requestUri");
            }
            if (!requestUri.IsAbsoluteUri)
            {
                throw new InvalidOperationException("This operation is not supported for a relative URI.");
            }

            IWebRequestCreate creator = null;
            int n = -1;

            // look for the most promising match in the registred prefixes
            foreach (KeyValuePair <string, IWebRequestCreate> kvp in registred_prefixes)
            {
                string key = kvp.Key;
                if ((key.Length > n) && requestUri.AbsoluteUri.StartsWith(key))
                {
                    creator = kvp.Value;
                    n       = key.Length;
                }
            }

            // 'http:/[/]' or 'https:/[/]' needs to be registred otherwise it gets ignored
            // note that this is unlike other protocols (e.g. 'ftp') - see unit tests
            string scheme = requestUri.Scheme;

            if ((scheme == "http" && n <= 5) || (scheme == "https" && n <= 6))
            {
                creator = default_creator;
            }

            if (creator == null)
            {
                throw new NotSupportedException(string.Format("Scheme {0} not supported", scheme));
            }

            return(creator.Create(requestUri));
        }
Exemplo n.º 9
0
        public static HttpWebRequest CreateHttp(Uri requestUri)
        {
            if (requestUri == null)
            {
                throw new ArgumentNullException("requestUri");
            }
            if (!requestUri.IsAbsoluteUri)
            {
                throw new InvalidOperationException("Uri is not absolute.");
            }

            // we do not check the registred prefixes from CreateHttp and *always* use the client HTTP stack
            switch (requestUri.Scheme)
            {
            case "http":
            case "https":
                return((HttpWebRequest)client_creator.Create(requestUri));

            default:
                throw new NotSupportedException(string.Format("Scheme {0} not supported", requestUri.Scheme));
            }
        }
Exemplo n.º 10
0
        public void ClientHttp()
        {
            IWebRequestCreate wrc = WebRequestCreator.ClientHttp;

            Assert.AreEqual(wrc.GetType().ToString(), "System.Net.Browser.ClientHttpWebRequestCreator", "Type");

            WebRequest wr = wrc.Create(Uri);

            Assert.AreEqual(wr.GetType().ToString(), "System.Net.Browser.ClientHttpWebRequest", "Type");
            Assert.AreSame(wrc, wr.CreatorInstance, "CreatorInstance");

            // we use 'https' instead of 'http' to avoid messing with other tests

            // registering 'https:' is not valid
            Assert.IsFalse(WebRequest.RegisterPrefix("https:", WebRequestCreator.ClientHttp), "RegisterPrefix-https:");

            // registering 'https' is not good enough
            Assert.IsTrue(WebRequest.RegisterPrefix("https", WebRequestCreator.ClientHttp), "RegisterPrefix-https");
            wr = WebRequest.Create("https://www.mono-project.com");
            Assert.AreSame(WebRequestCreator.BrowserHttp, wr.CreatorInstance, "WebRequest.Create(string)-https");

            // you need to register 'https:/'
            Assert.IsTrue(WebRequest.RegisterPrefix("https:/", WebRequestCreator.ClientHttp), "RegisterPrefix-https:/");
            wr = WebRequest.Create("https://www.mono-project.com");
            Assert.AreSame(WebRequestCreator.ClientHttp, wr.CreatorInstance, "WebRequest.Create(string)-https");

            // or register 'https://'
            Assert.IsTrue(WebRequest.RegisterPrefix("https://", WebRequestCreator.ClientHttp), "RegisterPrefix-https://");
            wr = WebRequest.Create("https://www.mono-project.com");
            Assert.AreSame(WebRequestCreator.ClientHttp, wr.CreatorInstance, "WebRequest.Create(string)-https://");

            // we cannot register twice (e.g. go back to Browser)
            Assert.IsFalse(WebRequest.RegisterPrefix("https://", WebRequestCreator.BrowserHttp), "RegisterPrefix-https://-2");
            wr = WebRequest.Create(new Uri("https://www.mono-project.com"));
            Assert.AreSame(WebRequestCreator.ClientHttp, wr.CreatorInstance, "WebRequest.Create(Uri)-https://");

            // we cannot register twice (even with the same, Client, value)
            Assert.IsFalse(WebRequest.RegisterPrefix("https://", WebRequestCreator.ClientHttp), "RegisterPrefix-https://-2");
        }
Exemplo n.º 11
0
        public bool UrlCheck(string url)
        {
            Uri            uri     = new Uri(url);
            HttpWebRequest request = (HttpWebRequest)webRequest.Create(uri);

            Console.WriteLine("is req: " + request);
            request.Method = "HEAD";

            try
            {
                //think this does aut cleanup
                using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
                {
                    //add 449 check
                    return(response.StatusCode != HttpStatusCode.NotFound);
                }
            }
            catch (WebException)
            {
                return(false);
            }
        }
Exemplo n.º 12
0
 public void Download(string url)
 {
     var request = _web.Create(url);
 }
Exemplo n.º 13
0
 public WebRequest Create(Uri uri)
 {
     return(_decorator(_webRequestProvider.Create(uri)));
 }