public static bool WriteToResponse(this IHttpResponse httpRes, IHttpRequest httpReq, object result, byte[] bodyPrefix, byte[] bodySuffix)
        {
            if (result == null)
            {
                httpRes.EndHttpRequestWithNoContent();
                return true;
            }

            var serializationContext = new HttpRequestContext(httpReq, httpRes, result);
            var httpResult = result as IHttpResult;
            if (httpResult != null)
            {
                if (httpResult.ResponseFilter == null)
                {
                    httpResult.ResponseFilter = EndpointHost.AppHost.ContentTypeFilters;
                }
                httpResult.RequestContext = serializationContext;
                serializationContext.ResponseContentType = httpResult.ContentType ?? httpReq.ResponseContentType;
                var httpResSerializer = httpResult.ResponseFilter.GetResponseSerializer(serializationContext.ResponseContentType);
                return httpRes.WriteToResponse(httpResult, httpResSerializer, serializationContext, bodyPrefix, bodySuffix);
            }

            var serializer = EndpointHost.AppHost.ContentTypeFilters.GetResponseSerializer(httpReq.ResponseContentType);
            return httpRes.WriteToResponse(result, serializer, serializationContext, bodyPrefix, bodySuffix);
        }
        public void Can_optimize_result_with_ToOptimizedResult()
        {
            var dto = new TestDto {Name = "test"};

            var httpReq = new MockHttpRequest();
            httpReq.Headers.Add(HttpHeaders.AcceptEncoding, "gzip,deflate,sdch");
            httpReq.ResponseContentType = "text/html";
            var httpRes = new ViewTests.MockHttpResponse();

            var httpRequestContext = new HttpRequestContext(httpReq, httpRes, dto);

            var appHost = new TestAppHost();
            HtmlFormat.Register(appHost);
            EndpointHost.ContentTypeFilter = appHost.ContentTypeFilters;

            object result = httpRequestContext.ToOptimizedResult(dto);
            Assert.IsNotNull(result);
            Assert.IsTrue(result is CompressedResult);
        }
		public void PortResolver_can_inject_dependencies_in_handlers_contructor()
		{
			var requestContext = new HttpRequestContext(new GetCustomer { CustomerId = 1 }, null);

			var factory = new FactoryProvider(requestContext);			

			var resolver = new PortResolver(GetType().Assembly) {
				HandlerFactory = new CreateFromLargestConstructorTypeFactory(factory).Create
			};

			var handler = resolver.FindService(typeof(GetCustomer).Name) as GetCustomerHandler;

			Assert.That(handler, Is.Not.Null);
			Assert.That(handler.RequestContext, Is.Not.Null);

			var requestDto = ((HttpRequestContext)handler.RequestContext).Dto as GetCustomer;
			Assert.That(requestDto, Is.Not.Null);
			Assert.That(requestDto.CustomerId, Is.EqualTo(1));
		}
Esempio n. 4
0
		public void Can_send_ResponseText_test_with_StatusDescription()
		{
            var mockRequest = new MockHttpRequest { ContentType = MimeTypes.Json };
			var mockRequestContext = new HttpRequestContext(mockRequest, null, new object());
			var mockResponse = new HttpResponseMock();

			var customStatus = "Custom Status Description";

			var httpResult = new HttpResult(System.Net.HttpStatusCode.Accepted, customStatus) {
				RequestContext = mockRequestContext
			};

            var reponseWasAutoHandled = mockResponse.WriteToResponse(httpResult, MimeTypes.Html);

			Assert.That(reponseWasAutoHandled, Is.True);

			var statusDesc = mockResponse.StatusDescription;
			Assert.That(mockResponse.StatusCode, Is.EqualTo((int)System.Net.HttpStatusCode.Accepted));
			Assert.That(statusDesc, Is.EqualTo(customStatus));
		}
        private static void CanOptimizeResult(string contentType, IPlugin pluginFormat)
        {
            var dto = new TestDto {Name = "test"};

            var httpReq = new MockHttpRequest();
            httpReq.Headers.Add(HttpHeaders.AcceptEncoding, "gzip,deflate,sdch");
            httpReq.ResponseContentType = contentType;
            var httpRes = new ViewTests.MockHttpResponse();

            var httpRequestContext = new HttpRequestContext(httpReq, httpRes, dto);

            var appHost = new TestAppHost();
            if (pluginFormat != null) pluginFormat.Register(appHost);

            EndpointHost.ContentTypeFilter = appHost.ContentTypeFilters;

            object result = httpRequestContext.ToOptimizedResult(dto);
            Assert.IsNotNull(result);
            Assert.IsTrue(result is CompressedResult);
        }
        private HttpRequestContext BuildHttpRequestContext(string postalCode, string rsid, string orid, RequestSession session)
        {
            string searchProfileUrl = String.Format(this.LinkedInWebSystemConfig.SearchProfileUrl,
                                                   System.Web.HttpUtility.UrlEncode(postalCode),
                                                   rsid,
                                                   orid);
            HttpRequestContext searchProfleRequestContext = new HttpRequestContext(searchProfileUrl)
            {
                CookieContainer = session.CookieContainer,
                Referer = LinkedInWebSystemConfig.MainEntryUrl,
                KeepAlive = true,
                TimeOut = 15000
            };

            return searchProfleRequestContext;
        }
Esempio n. 7
0
        protected override async Task <HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
        {
            if (request == null)
            {
                throw Error.ArgumentNull("request");
            }

            if (_disposed)
            {
                return(request.CreateErrorResponse(HttpStatusCode.ServiceUnavailable, SRResources.HttpServerDisposed));
            }

            // The first request initializes the server
            EnsureInitialized();

            // Capture current synchronization context and add it as a parameter to the request
            SynchronizationContext context = SynchronizationContext.Current;

            if (context != null)
            {
                request.SetSynchronizationContext(context);
            }

            // Add HttpConfiguration object as a parameter to the request
            request.SetConfiguration(_configuration);

            // Ensure we have a principal, even if the host didn't give us one
            IPrincipal originalPrincipal = Thread.CurrentPrincipal;

            if (originalPrincipal == null)
            {
                Thread.CurrentPrincipal = _anonymousPrincipal;
            }

            // Ensure we have a principal on the request context (if there is a request context).
            HttpRequestContext requestContext = request.GetRequestContext();

            if (requestContext == null)
            {
                requestContext = new RequestBackedHttpRequestContext(request);

                // if the host did not set a request context we will also set it back to the request.
                request.SetRequestContext(requestContext);
            }

            try
            {
                ExceptionDispatchInfo exceptionInfo;

                try
                {
                    return(await base.SendAsync(request, cancellationToken));
                }
                catch (OperationCanceledException)
                {
                    // Propogate the canceled task without calling exception loggers or handlers.
                    throw;
                }
                catch (HttpResponseException exception)
                {
                    return(exception.Response);
                }
                catch (Exception exception)
                {
                    exceptionInfo = ExceptionDispatchInfo.Capture(exception);
                }

                Debug.Assert(exceptionInfo.SourceException != null);

                ExceptionContext exceptionContext = new ExceptionContext(exceptionInfo.SourceException,
                                                                         ExceptionCatchBlocks.HttpServer, request);
                await ExceptionLogger.LogAsync(exceptionContext, cancellationToken);

                HttpResponseMessage response = await ExceptionHandler.HandleAsync(exceptionContext,
                                                                                  cancellationToken);

                if (response == null)
                {
                    exceptionInfo.Throw();
                }

                return(response);
            }
            finally
            {
                Thread.CurrentPrincipal = originalPrincipal;
            }
        }
Esempio n. 8
0
        public WebHookRegistrationsControllerTests()
        {
            IPrincipal principal = new ClaimsPrincipal();

            _managerMock = new Mock <IWebHookManager>();
            _storeMock   = new Mock <MemoryWebHookStore> {
                CallBase = true
            };

            _userMock = new Mock <IWebHookUser>();
            _userMock.Setup(u => u.GetUserIdAsync(principal))
            .ReturnsAsync(TestUser);

            _filterProviderMock = new Mock <IWebHookFilterProvider>();
            _filterProviderMock.Setup(p => p.GetFiltersAsync())
            .ReturnsAsync(new Collection <WebHookFilter> {
                new WebHookFilter {
                    Name = FilterName
                }
            });

            _filterManager = new WebHookFilterManager(new[]
            {
                new WildcardWebHookFilterProvider(),
                _filterProviderMock.Object
            });

            _registrarMock   = new Mock <IWebHookRegistrar>();
            _idValidatorMock = new Mock <IWebHookIdValidator>();

            var services = new Dictionary <Type, object>
            {
                { typeof(IWebHookManager), _managerMock.Object },
                { typeof(IWebHookStore), _storeMock.Object },
                { typeof(IWebHookUser), _userMock.Object },
                { typeof(IWebHookFilterManager), _filterManager },
                { typeof(IWebHookRegistrar), _registrarMock.Object },
                { typeof(IWebHookIdValidator), _idValidatorMock.Object },
            };

            _config = HttpConfigurationMock.Create(services);
            _config.Routes.Add(WebHookRouteNames.FiltersGetAction, new HttpRoute());

            HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, Address);

            request.SetConfiguration(_config);
            HttpRequestContext requestContext = new HttpRequestContext()
            {
                Configuration = _config,
                Principal     = principal,
                Url           = new UrlHelper(request),
            };

            _controllerContext = new HttpControllerContext()
            {
                Configuration  = _config,
                Request        = new HttpRequestMessage(),
                RequestContext = requestContext,
            };
            _controller = new WebHookRegistrationsControllerMock();
            _controller.Initialize(_controllerContext);
        }
Esempio n. 9
0
        private Task <HttpResponseMessage> SendAsyncCore(HttpRequestMessage request, CancellationToken cancellationToken)
        {
            if (request == null)
            {
                throw Error.ArgumentNull("request");
            }

            IHttpRouteData routeData = request.GetRouteData();

            Contract.Assert(routeData != null);

            HttpControllerDescriptor httpControllerDescriptor = ControllerSelector.SelectController(request);

            if (httpControllerDescriptor == null)
            {
                return(TaskHelpers.FromResult(request.CreateErrorResponse(
                                                  HttpStatusCode.NotFound,
                                                  Error.Format(SRResources.ResourceNotFound, request.RequestUri),
                                                  SRResources.NoControllerSelected)));
            }

            IHttpController httpController = httpControllerDescriptor.CreateController(request);

            if (httpController == null)
            {
                return(TaskHelpers.FromResult(request.CreateErrorResponse(
                                                  HttpStatusCode.NotFound,
                                                  Error.Format(SRResources.ResourceNotFound, request.RequestUri),
                                                  SRResources.NoControllerCreated)));
            }

            HttpConfiguration controllerConfiguration = httpControllerDescriptor.Configuration;

            // Set the controller configuration on the request properties
            HttpConfiguration requestConfig = request.GetConfiguration();

            if (requestConfig == null)
            {
                request.SetConfiguration(controllerConfiguration);
            }
            else
            {
                if (requestConfig != controllerConfiguration)
                {
                    request.SetConfiguration(controllerConfiguration);
                }
            }

            HttpRequestContext requestContext = request.GetRequestContext();

            // if the host doesn't create the context we will fallback to creating it.
            if (requestContext == null)
            {
                requestContext = new RequestBackedHttpRequestContext(request)
                {
                    // we are caching controller configuration to support per controller configuration.
                    Configuration = controllerConfiguration,
                };

                // if the host did not set a request context we will also set it back to the request.
                request.SetRequestContext(requestContext);
            }

            // Create context
            HttpControllerContext controllerContext = new HttpControllerContext(requestContext, request,
                                                                                httpControllerDescriptor, httpController);

            return(httpController.ExecuteAsync(controllerContext, cancellationToken));
        }
Esempio n. 10
0
 /// <inheritdoc />
 public abstract Task <HttpResponseMessage> ReceiveAsync(string id, HttpRequestContext context, HttpRequestMessage request);
 public Task <HttpResponseMessage> ReceiveAsync(string receiver, HttpRequestContext context, HttpRequestMessage request)
 {
     throw new NotImplementedException();
 }
        public static void HandleError(Exception ex, HttpListenerContext context)
        {
            try
            {
                var errorResponse = new ErrorResponse
                {
                    ResponseStatus = new ResponseStatus
                    {
                        ErrorCode = ex.GetType().Name,
                        Message = ex.Message,
                        StackTrace = ex.StackTrace,
                    }
                };

                var operationName = context.Request.GetOperationName();
                var httpReq = new ListenerRequest(operationName, context.Request);
                var httpRes = new ListenerResponse(context.Response);
                var requestCtx = new HttpRequestContext(httpReq, httpRes, errorResponse);
                var contentType = requestCtx.ResponseContentType;

                var serializer = HostContext.ContentTypes.GetResponseSerializer(contentType);
                if (serializer == null)
                {
                    contentType = HostContext.Config.DefaultContentType;
                    serializer = HostContext.ContentTypes.GetResponseSerializer(contentType);
                }

                httpRes.StatusCode = 500;
                httpRes.ContentType = contentType;

                serializer(requestCtx, errorResponse, httpRes);

                httpRes.Close();
            }
            catch (Exception errorEx)
            {
                var error = "Error this.ProcessRequest(context)(Exception while writing error to the response): [{0}]: {1}"
                            .Fmt(errorEx.GetType().Name, errorEx.Message);
                Log.ErrorFormat(error);
            }
        }
Esempio n. 13
0
 public ApiVisibilityContractResolver([NotNull] HttpRequestContext requestContext)
 {
     RequestContext = requestContext;
 }
        /// <inheritdoc />
        public override async Task <HttpResponseMessage> ReceiveAsync(string id, HttpRequestContext context, HttpRequestMessage request)
        {
            if (id == null)
            {
                throw new ArgumentNullException(nameof(id));
            }
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }
            if (request == null)
            {
                throw new ArgumentNullException(nameof(request));
            }

            if (request.Method == HttpMethod.Post)
            {
                EnsureSecureConnection(request);

                // Read the request entity body
                var data = await ReadAsXmlAsync(request);

                var notifications = new SalesforceNotifications(data);

                // Ensure that the organization ID matches the expected value.
                var orgId  = GetShortOrgId(notifications.OrganizationId);
                var secret = await GetReceiverConfig(request, ReceiverConfigName, id, 15, 18);

                var secretKey = GetShortOrgId(secret);
                if (!WebHookReceiver.SecretEqual(orgId, secretKey))
                {
                    var message = string.Format(CultureInfo.CurrentCulture, SalesforceReceiverResources.Receiver_BadValue, "OrganizationId");
                    context.Configuration.DependencyResolver.GetLogger().Error(message);
                    var fault     = string.Format(CultureInfo.InvariantCulture, ReadResource("Microsoft.AspNet.WebHooks.Messages.FaultResponse.xml"), message);
                    var invalidId = GetXmlResponse(request, HttpStatusCode.BadRequest, fault);
                    return(invalidId);
                }

                // Get the action
                var action = notifications.ActionId;
                if (string.IsNullOrEmpty(action))
                {
                    var message = string.Format(CultureInfo.CurrentCulture, SalesforceReceiverResources.Receiver_BadBody, "ActionId");
                    context.Configuration.DependencyResolver.GetLogger().Error(message);
                    var fault   = string.Format(CultureInfo.InvariantCulture, ReadResource("Microsoft.AspNet.WebHooks.Messages.FaultResponse.xml"), message);
                    var badType = GetXmlResponse(request, HttpStatusCode.BadRequest, fault);
                    return(badType);
                }

                // Call registered handlers
                var response = await ExecuteWebHookAsync(id, context, request, new string[] { action }, notifications);

                // Add SOAP response content if not already present or isn't XML. Ignore current (e.g. JSON) content.
                if (response?.Content == null || !response.Content.IsXml())
                {
                    // Ignore redirects because SOAP 1.1 doesn't mention them and they're corner cases in SOAP.
                    var statusCode = response?.StatusCode ?? HttpStatusCode.OK;
                    if (statusCode >= (HttpStatusCode)200 && statusCode < (HttpStatusCode)300)
                    {
                        var success = ReadResource("Microsoft.AspNet.WebHooks.Messages.NotificationResponse.xml");
                        response = GetXmlResponse(request, statusCode, success);
                    }
                    else
                    {
                        // Move failure information into a SOAP fault response. Fault contains code soapenv:Client and
                        // that must be transmitted with HTTP status 400, Bad Request according to SOAP 1.2 (mixing
                        // that sensible choice into this SOAP 1.1 implementation).
                        var resource    = ReadResource("Microsoft.AspNet.WebHooks.Messages.FaultResponse.xml");
                        var faultString = string.Format(
                            CultureInfo.CurrentCulture,
                            SalesforceReceiverResources.Receiver_HandlerFailed,
                            statusCode,
                            response.ReasonPhrase);

                        var failure = string.Format(CultureInfo.InvariantCulture, resource, faultString);
                        response = GetXmlResponse(request, HttpStatusCode.BadRequest, failure);
                    }
                }

                return(response);
            }
            else
            {
                return(CreateBadMethodResponse(request));
            }
        }
Esempio n. 15
0
        /// <inheritdoc />
        public override async Task <HttpResponseMessage> ReceiveAsync(string id, HttpRequestContext context, HttpRequestMessage request)
        {
            if (id == null)
            {
                throw new ArgumentNullException(nameof(id));
            }
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }
            if (request == null)
            {
                throw new ArgumentNullException(nameof(request));
            }

            if (request.Method == HttpMethod.Post)
            {
                IDependencyResolver resolver = context.Configuration.DependencyResolver;
                SettingsDictionary  settings = resolver.GetSettings();

                // Read the request entity body
                JObject data = await ReadAsJsonAsync(request);

                // Get notification ID
                string notificationId = data.Value <string>("id");
                if (string.IsNullOrEmpty(notificationId))
                {
                    string msg = string.Format(CultureInfo.CurrentCulture, StripeReceiverResources.Receiver_BadBody, "id");
                    resolver.GetLogger().Error(msg);
                    HttpResponseMessage badId = request.CreateErrorResponse(HttpStatusCode.BadRequest, msg);
                    return(badId);
                }

                // Check whether this is a test event
                bool testEvent = IsTestEvent(notificationId);

                // Check to see if we have been configured to use the WebHook as-is or validate using HTTP GET
                if (settings.IsTrue(DirectWebHook))
                {
                    // Ensure that we use https and have a valid code parameter
                    await EnsureValidCode(request, id);
                }
                else if (!testEvent)
                {
                    // For no-test events we go back to Stripe and get the authoritative data for this WebHook.
                    data = await GetEventDataAsync(request, id, notificationId);
                }

                // See if we should call handler in case of a test event or stop here.
                if (testEvent)
                {
                    if (settings.IsTrue(PassThroughTestEvents))
                    {
                        resolver.GetLogger().Info(StripeReceiverResources.Receiver_TestEvent_Process);
                    }
                    else
                    {
                        resolver.GetLogger().Info(StripeReceiverResources.Receiver_TestEvent);
                        return(request.CreateResponse());
                    }
                }

                // Call registered handlers
                string action = data.Value <string>("type");
                return(await ExecuteWebHookAsync(id, context, request, new string[] { action }, data));
            }
            else
            {
                return(CreateBadMethodResponse(request));
            }
        }
Esempio n. 16
0
        // Handle the processing of a request in here.
        private void ListenerCallback(IAsyncResult asyncResult)
        {
            var listener = asyncResult.AsyncState as HttpListener;
            HttpListenerContext context = null;

            if (listener == null)
            {
                return;
            }

            try
            {
                if (!IsListening)
                {
                    Log.DebugFormat("Ignoring ListenerCallback() as HttpListener is no longer listening");
                    return;
                }
                // The EndGetContext() method, as with all Begin/End asynchronous methods in the .NET Framework,
                // blocks until there is a request to be processed or some type of data is available.
                context = listener.EndGetContext(asyncResult);
            }
            catch (Exception ex)
            {
                // You will get an exception when httpListener.Stop() is called
                // because there will be a thread stopped waiting on the .EndGetContext()
                // method, and again, that is just the way most Begin/End asynchronous
                // methods of the .NET Framework work.
                var errMsg = ex + ": " + IsListening;
                Log.Warn(errMsg);
                return;
            }
            finally
            {
                // Once we know we have a request (or exception), we signal the other thread
                // so that it calls the BeginGetContext() (or possibly exits if we're not
                // listening any more) method to start handling the next incoming request
                // while we continue to process this request on a different thread.
                ListenForNextRequest.Set();
            }

            if (context == null)
            {
                return;
            }

            Log.InfoFormat("{0} Request : {1}", context.Request.UserHostAddress, context.Request.RawUrl);

            //System.Diagnostics.Debug.WriteLine("Start: " + requestNumber + " at " + DateTime.UtcNow);
            //var request = context.Request;

            //if (request.HasEntityBody)

            RaiseReceiveWebRequest(context);

            try
            {
                this.ProcessRequest(context);
            }
            catch (Exception ex)
            {
                var error = string.Format("Error this.ProcessRequest(context): [{0}]: {1}", ex.GetType().Name, ex.Message);
                Log.ErrorFormat(error);

                try
                {
                    var errorResponse = new ErrorResponse
                    {
                        ResponseStatus = new ResponseStatus
                        {
                            ErrorCode  = ex.GetType().Name,
                            Message    = ex.Message,
                            StackTrace = ex.StackTrace,
                        }
                    };

                    var operationName = context.Request.GetOperationName();
                    var httpReq       = new HttpListenerRequestWrapper(operationName, context.Request);
                    var httpRes       = new HttpListenerResponseWrapper(context.Response);
                    var requestCtx    = new HttpRequestContext(httpReq, httpRes, errorResponse);
                    var contentType   = requestCtx.ResponseContentType;

                    var serializer = EndpointHost.ContentTypes.GetResponseSerializer(contentType);
                    if (serializer == null)
                    {
                        contentType = EndpointHost.Config.DefaultContentType;
                        serializer  = EndpointHost.ContentTypes.GetResponseSerializer(contentType);
                    }

                    httpRes.StatusCode  = 500;
                    httpRes.ContentType = contentType;

                    serializer(requestCtx, errorResponse, httpRes);

                    httpRes.Close();
                }
                catch (Exception errorEx)
                {
                    error = string.Format("Error this.ProcessRequest(context)(Exception while writing error to the response): [{0}]: {1}", errorEx.GetType().Name, errorEx.Message);
                    Log.ErrorFormat(error);
                }
            }

            //System.Diagnostics.Debug.WriteLine("End: " + requestNumber + " at " + DateTime.UtcNow);
        }
Esempio n. 17
0
        public static void TryDispose(HttpRequestContext requestContext)
        {
            EnsureRequestContext(requestContext);

            TryDisposeRepository(requestContext.Configuration.Properties);
        }
Esempio n. 18
0
 private static bool IsSelfAuthorized(this HttpRequestContext context, Guid endpointId)
 {
     return(endpointId.ToString() == context.Principal.Identity.Name);
 }
Esempio n. 19
0
 public override Task <HttpResponse> HandleRequest(HttpRequestContext request)
 {
     return(Task.FromResult(new HttpResponse {
         BodyString = request.Request.BodyString, StatusCode = HttpMessage.StatusCodes.OK
     }));
 }
        public void GetProfileIdsFromCollege()
        {
            string searchBasehUrl = "http://www.linkedin.com/college/peers?start={0}&count={1}&edu-school=11398&edu-school-start={2}&edu-school-end={2}&edu-grad={3}&incl-users-with-no-edu-dates=true&hideconn=false&facet[]=location,us:0 ";

            string baseUrl = "http://www.linkedin.com/college/peers?start=450&count=50&edu-school=11398&edu-school-start=1939&edu-school-end=2012&edu-start=1939&edu-end=2020&incl-users-with-no-edu-dates=false&hideconn=false&facet[]=location,us:0";

            if (!Login())
            {
                return;
            }

            int count = 1940;
            List<int> queryList = new List<int>();
            Dictionary<int, int> queryDic = new Dictionary<int, int>();

            while (count <= 2020)
            {
                queryList.Add(count);
                count += 1;
            }

            Parallel.ForEach(queryList, (query) =>
            {
                int queryTotalCount = 0;
                int start = 0;
                var errorType = ErrorType.None;
                while (errorType != ErrorType.NonResult)
                {
                    try
                    {
                        int eachCount = 0;
                        string result = "";
                        Console.WriteLine("LinkedIn College Search Initailizaition ,Year:{0},Start:{1}", query, start);
                        var searchUrl = string.Format(searchBasehUrl, start, 12, query,query);
                        start += 12;
                        HttpRequestContext requestContext = new HttpRequestContext(searchUrl)
                        {
                            CookieContainer = this.CurrentSession.CookieContainer,
                            Referer = LinkedInWebSystemConfig.MainEntryUrl,
                            KeepAlive = true,
                            TimeOut = 15000
                        };
                        HttpRequestUtils.RequestHtmlPage(requestContext, out result);
                        errorType = LinkedInAnalyzer.AnalysisCollegeProfile(result, out eachCount);
                        queryTotalCount += eachCount;
                      //  Thread.Sleep(50000);
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine("LinkedIn College Search Initailizaition Exception, Error:{0}", ex.Message);
                    }
                }
                Console.WriteLine("Year:{0}, TotalCount:{1}", query, queryTotalCount);
                queryDic.Add(query, queryTotalCount);
            });

            LinkedInAnalyzer.SaveProfileFromCollege();
            ProviderConfiguration.SerializeConfigurationEntityToXml<ProfileConfiguration>(ConfigurationType.ProfileFromCollege, LinkedInAnalyzer.ProfileConfigurationFromCollege);
            string path = string.Format(string.Format(@"{0}\{1}\{2}", Environment.CurrentDirectory, "config", "queryResult.text"));
            if (File.Exists(path))
            {
                File.Delete(path);
            }
            StreamWriter writer = new StreamWriter(path);
            var keyList = queryDic.Keys.ToList();
            keyList.Sort();
            foreach (var key in keyList)
            {
                writer.WriteLine(string.Format("{0}  :  {1}", key, queryDic[key]));
            }
            writer.Flush();
            writer.Close();

            CheckProfileStatus();
        }
        public bool Login(bool saveSession, out string message)
        {
            Console.WriteLine("登出LinkedIn...");
            this.CurrentSession = new RequestSession(this);
            string result;
            message = "";

            HttpRequestContext requestContext = new HttpRequestContext(this.LinkedInWebSystemConfig.MainEntryUrl)
            {
                Referer = LinkedInWebSystemConfig.MainEntryUrl,
                KeepAlive = true,
                TimeOut = 50000
            };
            HttpRequestUtils.RequestHtmlPage(requestContext, out result);
            CurrentSession.CookieContainer = requestContext.CookieContainer;

            result = string.Empty;
            HttpRequestContext loginRequestContext = new HttpRequestContext(this.LinkedInWebSystemConfig.LoginUrl)
            {
                Referer = LinkedInWebSystemConfig.MainEntryUrl,
                KeepAlive = true,
                TimeOut = 50000
            };
            HttpRequestUtils.RequestHtmlPage(loginRequestContext, out result);
            CurrentSession.CookieContainer = loginRequestContext.CookieContainer;

            string csrfToken = string.Empty;
            string sourceAlias = string.Empty;

            csrfToken = this.GetCSRFToken(result);
            sourceAlias = this.GetSourceAlias(result);

            // string viewStateValue = string.Empty;
            //viewStateValue = this.GetViewState(result);

            Console.WriteLine("登陆LinkedIn...");
            string postData = String.Format(this.LinkedInWebSystemConfig.LoginPostDataFormat,
                                            HttpUtility.UrlEncode(this.LinkedInWebSystemConfig.LoginParameters["UserName"].Value),
                                            HttpUtility.UrlEncode(this.LinkedInWebSystemConfig.LoginParameters["Password"].Value),
                                            csrfToken,
                                            sourceAlias);
            HttpPostRequestContext postRequestContext = new HttpPostRequestContext(this.LinkedInWebSystemConfig.LoginSubmitUrl, postData)
            {
                CookieContainer = CurrentSession.CookieContainer,
                Referer = LinkedInWebSystemConfig.MainEntryUrl,
                KeepAlive = true,
                TimeOut = 50000
            };

            string loginResult;

            HttpRequestUtils.RequestPostHtmlPage(postRequestContext, out loginResult);

            return true;
        }
Esempio n. 22
0
        public async Task Test_ThrottlingHttpHandler_SuccessfulRetry()
        {
            IHttpExtensionHandler extensionHandler = Substitute.For <IHttpExtensionHandler>();

            extensionHandler.SendAsync(
                new HttpRequestMessage(HttpMethod.Get, new Uri("http://localhost")),
                default(CancellationToken),
                null).ReturnsForAnyArgs(
                (callInfo) =>
            {
                return(Task.FromResult(new HttpResponseMessage((HttpStatusCode)429)));
            },
                (callInfo) =>
            {
                return(Task.FromResult(new HttpResponseMessage(HttpStatusCode.ServiceUnavailable)));
            },
                (callInfo) =>
            {
                return(Task.FromResult(new HttpResponseMessage(HttpStatusCode.OK)));
            });

            IAuthorizationProvider authorizationProvider = Substitute.For <IAuthorizationProvider>();

            authorizationProvider.GetAuthenticationHeader().Returns(new AuthenticationHeaderValue("Bearer", "123"));

            HttpRequestContext context = new HttpRequestContext();

            context.HttpExtensionHandler  = extensionHandler;
            context.AuthorizationProvider = authorizationProvider;

            using (HttpRequest get = HttpRequest.Get(new Uri("http://localhost"), context))
            {
                HttpResponse response = await get.GetHttpResponseAsync();

                IEnumerable <string> header = response.ResponseHeaders.GetValues("X-RetryAttempt-HttpThrottlingHandler");
                Assert.IsNotNull(
                    header);

                foreach (string s in header)
                {
                    int i = 0;
                    Assert.IsTrue(
                        int.TryParse(s, out i));

                    Assert.AreEqual(
                        2,
                        i);
                }

                header = response.ResponseHeaders.GetValues("X-TotalDelayApplied-HttpThrottlingHandler");
                Assert.IsNotNull(
                    header);

                foreach (string s in header)
                {
                    int i = 0;
                    Assert.IsTrue(
                        int.TryParse(s, out i));

                    Assert.AreEqual(
                        10,
                        i);
                }
            }
        }
 public WebApiRequestContext(HttpRequestContext requestContext)
 {
     User = new PrincipalUser(requestContext.Principal);
 }
Esempio n. 24
0
        public sealed override Task <HttpResponse> HandleRequest(HttpRequestContext request)
        {
            var typeConverter = TypeDescriptor.GetConverter(typeof(T));

            return(this.HandleRequest((T)typeConverter.ConvertFrom(request)));
        }
Esempio n. 25
0
        /// <summary>
        /// Processes the WebHook request by calling all registered <see cref="IWebHookHandler"/> instances.
        /// </summary>
        /// <param name="id">A (potentially empty) ID of a particular configuration for this <see cref="IWebHookReceiver"/>. This
        /// allows an <see cref="IWebHookReceiver"/> to support multiple WebHooks with individual configurations.</param>
        /// <param name="context">The <see cref="HttpRequestContext"/> for this WebHook invocation.</param>
        /// <param name="request">The <see cref="HttpRequestMessage"/> for this WebHook invocation.</param>
        /// <param name="actions">The collection of actions associated with this WebHook invocation.</param>
        /// <param name="data">Optional data associated with this WebHook invocation.</param>
        protected virtual async Task <HttpResponseMessage> ExecuteWebHookAsync(string id, HttpRequestContext context, HttpRequestMessage request, IEnumerable <string> actions, object data)
        {
            if (id == null)
            {
                throw new ArgumentNullException("id");
            }
            if (context == null)
            {
                throw new ArgumentNullException("context");
            }
            if (request == null)
            {
                throw new ArgumentNullException("request");
            }
            if (actions == null)
            {
                actions = new string[0];
            }

            // Execute handlers. Note that we wait for them to complete before
            // we return. This means that we don't send back the final response
            // before all handlers have executed. As a result we expect handlers
            // to be fairly quick in what they process. If a handler sets the
            // Response property on the context then the execution is stopped
            // and that response returned. If a handler throws an exception then
            // the execution of handlers is also stopped.
            WebHookHandlerContext handlerContext = new WebHookHandlerContext(actions)
            {
                Id             = id,
                Data           = data,
                Request        = request,
                RequestContext = context,
            };

            IEnumerable <IWebHookHandler> handlers = context.Configuration.DependencyResolver.GetHandlers();

            foreach (IWebHookHandler handler in handlers)
            {
                // Only call handlers with matching receiver name (or no receiver name in which case they support all receivers)
                if (handler.Receiver != null && !string.Equals(Name, handler.Receiver, StringComparison.OrdinalIgnoreCase))
                {
                    continue;
                }

                await handler.ExecuteAsync(Name, handlerContext);

                // Check if response has been set and if so stop the processing.
                if (handlerContext.Response != null)
                {
                    return(handlerContext.Response);
                }
            }
            return(request.CreateResponse());
        }
Esempio n. 26
0
        /// <inheritdoc />
        public override async Task <HttpResponseMessage> ReceiveAsync(string id, HttpRequestContext context, HttpRequestMessage request)
        {
            if (id == null)
            {
                throw new ArgumentNullException(nameof(id));
            }
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }
            if (request == null)
            {
                throw new ArgumentNullException(nameof(request));
            }

            if (request.Method == HttpMethod.Post)
            {
                EnsureSecureConnection(request);

                // Read the request entity body
                NameValueCollection data = await ReadAsFormDataAsync(request);

                // Verify that the token is correct
                string token     = data[TokenParameter];
                string secretKey = await GetReceiverConfig(request, Name, id, SecretMinLength, SecretMaxLength);

                if (!WebHookReceiver.SecretEqual(token, secretKey))
                {
                    string msg = string.Format(CultureInfo.CurrentCulture, SlackReceiverResources.Receiver_BadToken, TokenParameter);
                    context.Configuration.DependencyResolver.GetLogger().Error(msg);
                    HttpResponseMessage invalidCode = request.CreateErrorResponse(HttpStatusCode.BadRequest, msg);
                    return(invalidCode);
                }

                // Get the action by looking for either trigger_word or command parameter
                string action = data[TriggerParameter];
                if (!string.IsNullOrEmpty(action))
                {
                    // Get the subtext by removing the trigger word
                    string text = data[TextParameter];
                    data[SubtextParameter] = GetSubtext(action, text);
                }
                else
                {
                    action = data[CommandParameter];
                }

                if (string.IsNullOrEmpty(action))
                {
                    string msg = string.Format(CultureInfo.CurrentCulture, SlackReceiverResources.Receiver_BadBody, CommandParameter, TriggerParameter);
                    context.Configuration.DependencyResolver.GetLogger().Error(msg);
                    HttpResponseMessage badType = request.CreateErrorResponse(HttpStatusCode.BadRequest, msg);
                    return(badType);
                }

                // Call registered handlers
                return(await ExecuteWebHookAsync(id, context, request, new string[] { action }, data));
            }
            else
            {
                return(CreateBadMethodResponse(request));
            }
        }
Esempio n. 27
0
        private int server_BrowseMetadata(Action action, String object_id, String filter, Int32 starting_index, Int32 requested_count, String sort_criteria, HttpRequestContext context)
        {
            Console.WriteLine("BrowseMetadata: " + object_id);
            if (object_id == "0") 
            {
                var root = new MediaContainer();
                root.Title = "Root";
                root.ObjectID = "0";
                root.ParentID = "-1";
                root.Class = new ObjectClass("object.container.storageFolder", "");

                var didl = Didl.header + root.ToDidl(filter) + Didl.footer;
                action.SetArgumentValue("Result", didl);
                action.SetArgumentValue("NumberReturned", "1");
                action.SetArgumentValue("TotalMatches", "1");

                // update ID may be wrong here, it should be the one of the container?
                // TODO: We need to keep track of the overall updateID of the CDS
                action.SetArgumentValue("UpdateId", "1");

                return 0;
            }
            else if (object_id == "1")
            {
                var item = new MediaItem();
                item.Title = "Item";
                item.ObjectID = "1";
                item.ParentID = "0";
                item.Class = new ObjectClass("object.item.audioItem.musicTrack", "");

                var resource = new MediaResource();
                resource.ProtoInfo = ProtocolInfo.GetProtocolInfoFromMimeType("audio/mp3", true, context);

                // get list of ips and make sure the ip the request came from is used for the first resource returned
                // this ensures that clients which look only at the first resource will be able to reach the item
                List<String> ips = UPnP.GetIpAddresses(true);
                String localIP = context.LocalAddress.ip;
                if (localIP != "0.0.0.0")
                {
                    ips.Remove(localIP);
                    ips.Insert(0, localIP);
                }

                // iterate through all ips and create a resource for each
                foreach (String ip in ips)
                {
                    resource.URI = new Uri("http://" + ip + ":" + context.LocalAddress.port + "/test/test.mp3").ToString();
                    item.AddResource(resource);
                }
                                
                var didl = Didl.header + item.ToDidl(filter) + Didl.footer;
                action.SetArgumentValue("Result", didl);
                action.SetArgumentValue("NumberReturned", "1");
                action.SetArgumentValue("TotalMatches", "1");

                // update ID may be wrong here, it should be the one of the container?
                // TODO: We need to keep track of the overall updateID of the CDS
                action.SetArgumentValue("UpdateId", "1");

                return 0;
            }

            return -1;
        }
 public new Task <HttpResponseMessage> ExecuteWebHookAsync(string id, HttpRequestContext context, HttpRequestMessage request, IEnumerable <string> actions, object data)
 {
     return(base.ExecuteWebHookAsync(id, context, request, actions, data));
 }
Esempio n. 29
0
 public abstract Task <HttpResponse> HandleRequest(HttpRequestContext request);
Esempio n. 30
0
        /// <inheritdoc />
        public override async Task <HttpResponseMessage> ReceiveAsync(string id, HttpRequestContext context, HttpRequestMessage request)
        {
            if (id == null)
            {
                throw new ArgumentNullException(nameof(id));
            }
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }
            if (request == null)
            {
                throw new ArgumentNullException(nameof(request));
            }

            if (request.Method == HttpMethod.Post)
            {
                EnsureSecureConnection(request);

                // Read the request entity body
                var data = await ReadAsXmlAsync(request);

                var notifications = new SalesforceNotifications(data);

                // Ensure that the organization ID matches the expected value.
                var orgId  = GetShortOrgId(notifications.OrganizationId);
                var secret = await GetReceiverConfig(request, ReceiverConfigName, id, 15, 18);

                var secretKey = GetShortOrgId(secret);
                if (!WebHookReceiver.SecretEqual(orgId, secretKey))
                {
                    var message = string.Format(CultureInfo.CurrentCulture, SalesforceReceiverResources.Receiver_BadValue, "OrganizationId");
                    context.Configuration.DependencyResolver.GetLogger().Error(message);
                    var fault     = string.Format(CultureInfo.InvariantCulture, ReadResource("Microsoft.AspNet.WebHooks.Messages.FaultResponse.xml"), message);
                    var invalidId = GetXmlResponse(request, HttpStatusCode.BadRequest, fault);
                    return(invalidId);
                }

                // Get the action
                var action = notifications.ActionId;
                if (string.IsNullOrEmpty(action))
                {
                    var message = string.Format(CultureInfo.CurrentCulture, SalesforceReceiverResources.Receiver_BadBody, "ActionId");
                    context.Configuration.DependencyResolver.GetLogger().Error(message);
                    var fault   = string.Format(CultureInfo.InvariantCulture, ReadResource("Microsoft.AspNet.WebHooks.Messages.FaultResponse.xml"), message);
                    var badType = GetXmlResponse(request, HttpStatusCode.BadRequest, fault);
                    return(badType);
                }

                // Call registered handlers
                var response = await ExecuteWebHookAsync(id, context, request, new string[] { action }, notifications);

                // Add SOAP response if not already present
                if (response == null || response.Content == null || !response.Content.IsXml())
                {
                    var success = ReadResource("Microsoft.AspNet.WebHooks.Messages.NotificationResponse.xml");
                    response = GetXmlResponse(request, HttpStatusCode.OK, success);
                }
                return(response);
            }
            else
            {
                return(CreateBadMethodResponse(request));
            }
        }
Esempio n. 31
0
        private int server_ProcessFileRequest(HttpRequestContext context, HttpResponse response)
        {
            Uri uri = context.Request.URI;
            if (uri.AbsolutePath == "/test/test.mp3")
            {
                MediaServer.SetResponseFilePath(context, response, "C:\\Test.mp3");
                return 0;
            }

            return -1;
        }
Esempio n. 32
0
        /// <inheritdoc />
        public override async Task <HttpResponseMessage> ReceiveAsync(
            string id,
            HttpRequestContext context,
            HttpRequestMessage request)
        {
            if (id == null)
            {
                throw new ArgumentNullException(nameof(id));
            }
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }
            if (request == null)
            {
                throw new ArgumentNullException(nameof(request));
            }

            if (request.Method == HttpMethod.Post)
            {
                var valid = await VerifySignature(request, id);

                if (!valid)
                {
                    return(CreateBadSignatureResponse(request, SignatureHeaderName));
                }

                // Read the request entity body
                var data = await ReadAsJsonAsync(request);

                // Get notification ID
                var notificationId = data.Value <string>("id");
                var resolver       = context.Configuration.DependencyResolver;
                if (string.IsNullOrEmpty(notificationId))
                {
                    var message = string.Format(
                        CultureInfo.CurrentCulture,
                        StripeReceiverResources.Receiver_BadBody,
                        "id");
                    resolver.GetLogger().Error(message);
                    var badId = request.CreateErrorResponse(HttpStatusCode.BadRequest, message);
                    return(badId);
                }

                // Check whether this is a test event
                var testEvent = IsTestEvent(notificationId);

                // See if we should call handler in case of a test event or stop here.
                if (testEvent)
                {
                    var settings = resolver.GetSettings();
                    if (settings.IsTrue(PassThroughTestEvents))
                    {
                        resolver.GetLogger().Info(StripeReceiverResources.Receiver_TestEvent_Process);
                    }
                    else
                    {
                        resolver.GetLogger().Info(StripeReceiverResources.Receiver_TestEvent);
                        return(request.CreateResponse());
                    }
                }

                // Call registered handlers
                var action = data.Value <string>("type");
                return(await ExecuteWebHookAsync(id, context, request, new string[] { action }, data));
            }
            else
            {
                return(CreateBadMethodResponse(request));
            }
        }
Esempio n. 33
0
        private int server_BrowseMetadata(Action action, String object_id, String filter, Int32 starting_index, Int32 requested_count, String sort_criteria, HttpRequestContext context)
        {
            Console.WriteLine("BrowseMetadata: " + object_id);
            if (object_id == "0")
            {
                var root = new MediaContainer();
                root.Title    = "Root";
                root.ObjectID = "0";
                root.ParentID = "-1";
                root.Class    = new ObjectClass("object.container.storageFolder", "");

                var didl = Didl.header + root.ToDidl(filter) + Didl.footer;
                action.SetArgumentValue("Result", didl);
                action.SetArgumentValue("NumberReturned", "1");
                action.SetArgumentValue("TotalMatches", "1");

                // update ID may be wrong here, it should be the one of the container?
                // TODO: We need to keep track of the overall updateID of the CDS
                action.SetArgumentValue("UpdateId", "1");

                return(0);
            }
            else if (object_id == "1")
            {
                var item = new MediaItem();
                item.Title    = "Item";
                item.ObjectID = "1";
                item.ParentID = "0";
                item.Class    = new ObjectClass("object.item.audioItem.musicTrack", "");

                var resource = new MediaResource();
                resource.ProtoInfo = ProtocolInfo.GetProtocolInfoFromMimeType("audio/mp3", true, context);

                // get list of ips and make sure the ip the request came from is used for the first resource returned
                // this ensures that clients which look only at the first resource will be able to reach the item
                List <String> ips     = UPnP.GetIpAddresses(true);
                String        localIP = context.LocalAddress.ip;
                if (localIP != "0.0.0.0")
                {
                    ips.Remove(localIP);
                    ips.Insert(0, localIP);
                }

                // iterate through all ips and create a resource for each
                foreach (String ip in ips)
                {
                    resource.URI = new Uri("http://" + ip + ":" + context.LocalAddress.port + "/test/test.mp3").ToString();
                    item.AddResource(resource);
                }

                var didl = Didl.header + item.ToDidl(filter) + Didl.footer;
                action.SetArgumentValue("Result", didl);
                action.SetArgumentValue("NumberReturned", "1");
                action.SetArgumentValue("TotalMatches", "1");

                // update ID may be wrong here, it should be the one of the container?
                // TODO: We need to keep track of the overall updateID of the CDS
                action.SetArgumentValue("UpdateId", "1");

                return(0);
            }

            return(-1);
        }
Esempio n. 34
0
 public static QBitNinjaConfiguration GetConfiguration(this HttpRequestContext ctx)
 {
     return(((QBitNinjaDependencyResolver)ctx.Configuration.DependencyResolver).Get <QBitNinjaConfiguration>());
 }
 public Task Unregister(ApiServices services, HttpRequestContext context,
                        string deviceId)
 {
     // This is where you can hook into registration deletion.
     return(Task.FromResult(true));
 }
 private static BatchHttpRequestContext CreateProductUnderTest(HttpRequestContext batchContext)
 {
     return(new BatchHttpRequestContext(batchContext));
 }
        public void GetProfileFromPeopleYouMayKnow()
        {
            //string name2 = "";
            //var dd = LinkedInAnalyzer.AnalysisProfileFromPeopleYouMayKnow(File.ReadAllText(@"D:\重要文件\src\src\Boleplus.Client\BolePlus.Client.LinkedIn\1.txt"), ref name2);
            if (!Login())
            {
                return;
            }

            var searchBaseUrl = "http://www.linkedin.com/pymk/pcard?mid={0}";

            LinkedInAnalyzer.ProfileConfigurationFromPeopleYouMayKnow = ProviderConfiguration.GetConfigurationEntity<ProfileConfiguration>(ConfigurationType.ProfileFromCollege);

            if (LinkedInAnalyzer.ProfileConfigurationFromPeopleYouMayKnow != null && LinkedInAnalyzer.ProfileConfigurationFromPeopleYouMayKnow.ProfileCollection != null)
            {
                int searchTotalCount = 0;
                int errorCount = 1;
                ErrorType type = ErrorType.None;
                Parallel.ForEach(LinkedInAnalyzer.ProfileConfigurationFromPeopleYouMayKnow.ProfileCollection, (profile, loopState) =>
                {

                    if (loopState.IsStopped)
                    {
                        loopState.Stop();
                        return;
                    }
                    if (searchTotalCount >= 700)
                    {
                        loopState.Stop();
                        return;
                    }
                    searchTotalCount++;
                    try
                    {
                        if (type == ErrorType.Capture || errorCount >= 20 || searchTotalCount >= 700)
                        {
                            Console.WriteLine("获取数据被抓,赶快终止!!!!!!");

                            loopState.Stop();
                            return;
                        }
                        if (profile.Checked)
                        {
                            return;
                        }
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex.StackTrace);
                        return;
                    }
                    Thread.Sleep(20000);
                    int tryCount = 1;
                    while (tryCount++ <= 2)
                    {
                        try
                        {

                            string result;
                            var searchUrl = string.Format(searchBaseUrl, profile.Id);
                            HttpRequestContext requestContext = new HttpRequestContext(searchUrl)
                            {
                                CookieContainer = this.CurrentSession.CookieContainer,
                                Referer = LinkedInWebSystemConfig.MainEntryUrl,
                                KeepAlive = true,
                                TimeOut = 50000
                            };
                            HttpRequestUtils.RequestHtmlPage(requestContext, out result);
                            string name = "";
                            var viewProfileUrl = LinkedInAnalyzer.AnalysisProfileFromPeopleYouMayKnow(result, ref name);
                            if (string.IsNullOrEmpty(viewProfileUrl))
                            {
                                continue;
                            }
                            profile.Checked = true;
                            profile.Name = name;
                            profile.ViewProfileUrl = viewProfileUrl;
                            Console.WriteLine("获取人才数据,ProfileId:{0},ProfileName:{1}", profile.Id, profile.Name);
                            Thread.Sleep(40000);
                        }
                        catch (WebException ex)
                        {
                            errorCount++;
                            Console.WriteLine("获取数据失败,ProfileId:{0},Error:{1}", profile.Id, ex.Message);
                            if (ex.Response != null)
                            {
                                var response = ex.Response as HttpWebResponse;
                                if (response != null)
                                {
                                    if (response.StatusCode == HttpStatusCode.Found)
                                    {
                                        type = ErrorType.Capture;
                                    }
                                }
                            }
                            break;
                        }
                        catch (Exception ex)
                        {
                            Console.WriteLine("获取数据失败,ProfileId:{0},Error:{1}", profile.Id, ex.Message);
                            break;
                        }

                        break;
                    }

                });

                int tr2yCount = 1;
                while (tr2yCount++ <= 2)
                {
                    try
                    {
                        LinkedInAnalyzer.SaveProfileFromPeopleYouMayKnow();
                        break;
                    }
                    catch (Exception ex)
                    {

                    }
                }

            }
        }
 public static HttpRequestContext SetUrl(this HttpRequestContext httpRequestContext, string url)
 {
     httpRequestContext.SetupInfo.Url = url;
     return(httpRequestContext);
 }
        private void InitialCurrentSession(IWebProxy proxy = null)
        {
            string address = proxy != null ? ((WebProxy)proxy).Address.ToString() : "";

            try
            {
                Console.WriteLine("初始化Current Session: 代理:{0}", address);

                LinkedInWebConfig webConfig = (LinkedInWebConfig)this.LinkedInWebSystemConfig;
                string result = "";
                HttpRequestContext requestContext = new HttpRequestContext(webConfig.MainEntryUrl)
                {
                    Referer = LinkedInWebSystemConfig.MainEntryUrl,
                    KeepAlive = true,
                    TimeOut = 2000
                };
                HttpRequestUtils.RequestHtmlPage(requestContext, out result, proxy);
                CurrentSession.CookieContainer = requestContext.CookieContainer;

                Console.WriteLine("初始化Current Session成功,代理: {0}", address);
            }
            catch (Exception ex)
            {

                Console.WriteLine("初始化Current Session失败,Proxy: {1},Error: {0}", ex.Message, address);
                throw;
            }
        }
 public static HttpRequestContext SetVerb(this HttpRequestContext httpRequestContext, string httpVerb)
 {
     httpRequestContext.SetupInfo.HttpVerb = httpVerb;
     return(httpRequestContext);
 }
        public void GetProfiles()
        {
            var searchProfileConfig = ProviderConfiguration.GetConfigurationEntity<ProfileConfiguration>(this.GetConfigurationPath("ProfileView"));
            var storedDirPath = string.Format(@"{0}\{1}", Environment.CurrentDirectory, "LinkedIn");
            if (!Directory.Exists(storedDirPath))
            {
                Directory.CreateDirectory(storedDirPath);
            }
            if (!Login())
            {
                return;
            }

            int errorCount = 1;
            int searchTotalCount = 0;
            Parallel.ForEach(searchProfileConfig.ProfileCollection, (profile, loopState) =>
            {
                if (loopState.IsStopped)
                {
                    loopState.Stop();
                    return;
                }
                if (errorCount >= 20 || searchTotalCount >= 700)
                {
                    loopState.Stop();
                    return;
                }
                if (profile.HasViewed)
                {
                    return;
                }
                Thread.Sleep(30000);
                int tryCount = 1;
                if (string.Empty.Equals(profile.ViewProfileUrl))
                {
                    return;
                }

                profile.ViewProfileUrl = profile.ViewProfileUrl.Replace("&amp;", "&");

                string result = string.Empty;

                while (tryCount++ <= 2)
                {
                    var erroType = ErrorType.None;
                    try
                    {
                        searchTotalCount++;
                        Console.WriteLine("LinkedIn 搜索人才  Id:{0},尝试次数: {1}", profile.Id, tryCount - 1);

                        HttpRequestContext requestContext = new HttpRequestContext(profile.ViewProfileUrl)
                        {
                            CookieContainer = this.CurrentSession.CookieContainer,
                            Referer = LinkedInWebSystemConfig.MainEntryUrl,
                            KeepAlive = true,
                            TimeOut = 60000
                        };
                        HttpRequestUtils.RequestHtmlPage(requestContext, out result);
                        File.WriteAllText(string.Format(@"{0}\{1}\{2}.txt", Environment.CurrentDirectory, "LinkedIn", profile.Id), result);
                        Thread.Sleep(60000);
                        break;
                    }
                    catch (WebException ex)
                    {
                        //    errorCount++;
                        Console.WriteLine("LinkedIn 搜索人才  Id:{0},尝试次数: {1}", profile.Id, tryCount);
                        if (ex.Response != null)
                        {
                            var response = ex.Response as HttpWebResponse;
                            if (response != null)
                            {
                                if (response.StatusCode == HttpStatusCode.Found)
                                {
                                    //  errorCount = 50;
                                }
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        // errorCount++;
                        Console.WriteLine("LinkedIn 搜索人才  Id:{0},尝试次数: {1}", profile.Id, tryCount);

                    }
                    finally
                    {
                        if (tryCount > 2)
                        {
                            // errorCount++;
                            Console.WriteLine("LinkedIn 搜索人才失败,Id:{0}", profile.Id);
                        }
                    }
                    break;
                }

            });
        }
 public static HttpRequestContext SetToken(this HttpRequestContext httpRequestContext, string token)
 {
     httpRequestContext.SetupInfo.Token = token;
     return(httpRequestContext);
 }
        public RequestSession Login(WebProxy proxy = null)
        {
            string address = proxy != null ? proxy.Address.ToString() : "";
            Console.WriteLine("登出LinkedIn...Proxy: {0}", address);
            var session = new RequestSession(this);
            string result;

            HttpRequestContext requestContext = new HttpRequestContext(this.LinkedInWebSystemConfig.MainEntryUrl)
            {
                Referer = LinkedInWebSystemConfig.MainEntryUrl,
                KeepAlive = true,
                TimeOut = 10000
            };
            HttpRequestUtils.RequestHtmlPage(requestContext, out result, proxy);
            session.CookieContainer = requestContext.CookieContainer;

            result = string.Empty;
            HttpRequestContext loginRequestContext = new HttpRequestContext(this.LinkedInWebSystemConfig.LoginUrl)
            {
                Referer = LinkedInWebSystemConfig.MainEntryUrl,
                KeepAlive = true,
                TimeOut = 10000
            };
            HttpRequestUtils.RequestHtmlPage(loginRequestContext, out result, proxy);
            session.CookieContainer = loginRequestContext.CookieContainer;

            string csrfToken = string.Empty;
            string sourceAlias = string.Empty;

            csrfToken = this.GetCSRFToken(result);
            sourceAlias = this.GetSourceAlias(result);

            // string viewStateValue = string.Empty;
            //viewStateValue = this.GetViewState(result);

            Console.WriteLine("登录LinkedIn...Proxy: {0}", address);
            string postData = String.Format(this.LinkedInWebSystemConfig.LoginPostDataFormat,
                                            HttpUtility.UrlEncode(this.LinkedInWebSystemConfig.LoginParameters["UserName"].Value),
                                            HttpUtility.UrlEncode(this.LinkedInWebSystemConfig.LoginParameters["Password"].Value),
                                            csrfToken,
                                            sourceAlias);
            HttpPostRequestContext postRequestContext = new HttpPostRequestContext(this.LinkedInWebSystemConfig.LoginSubmitUrl, postData)
            {
                CookieContainer = session.CookieContainer,
                Referer = LinkedInWebSystemConfig.MainEntryUrl,
                KeepAlive = true,
                TimeOut = 2000
            };

            string loginResult;

            HttpRequestUtils.RequestPostHtmlPage(postRequestContext, out loginResult, proxy);
            Console.WriteLine("登录LinkedIn成功....Proxy: {0}", address);
            session.CookieContainer = postRequestContext.CookieContainer;
            return session;
        }
 public static HttpRequestContext SetTokenType(this HttpRequestContext httpRequestContext, HttpRequestTokenType tokenType)
 {
     httpRequestContext.SetupInfo.TokenType = tokenType;
     return(httpRequestContext);
 }
Esempio n. 45
0
 public static UrlParameters CreateFrom(HttpRequestContext context)
 {
     return(new UrlParameters(context.RouteData.Values
                              .ToDictionary(x => x.Key, x => (string)x.Value)));
 }
        public static bool WriteToResponse(this IHttpResponse httpRes, IHttpRequest httpReq, object result)
        {
            if (result == null) return true;

            var serializationContext = new HttpRequestContext(httpReq, result);
            var httpResult = result as IHttpResult;
            if (httpResult != null)
            {
                if (httpResult.ResponseFilter == null)
                {
                    httpResult.ResponseFilter = EndpointHost.AppHost.ContentTypeFilters;
                }
                httpResult.RequestContext = serializationContext;
                var httpResSerializer = httpResult.ResponseFilter.GetStreamSerializer(httpReq.ResponseContentType);
                return httpRes.WriteToResponse(httpResult, httpResSerializer, serializationContext);
            }

            var serializer = EndpointHost.AppHost.ContentTypeFilters.GetStreamSerializer(httpReq.ResponseContentType);
            return httpRes.WriteToResponse(result, serializer, serializationContext);
        }