示例#1
0
        public static SignUpResponseDto SendSignUp(string signUpData)
        {
            HttpWebRequest request = HttpWebRequest.CreateHttp(SERVER_SIGN_UP_URL);

            request.Method      = WebRequestMethods.Http.Post;
            request.ContentType = JSON_FROM_TYPE;

            using (StreamWriter strw = new StreamWriter(request.GetRequestStream()))
                strw.WriteLine($"{signUpData}");

            try
            {
                using (StreamReader str = new StreamReader(request.GetResponse().GetResponseStream()))
                {
                    string responseJson = str.ReadToEnd();
                    return(JsonConvert.DeserializeObject <SignUpResponseDto>(responseJson));
                }
            }
            catch (WebException ex)
            {
                if (ex.Response == null)
                {
                    return(new SignUpResponseDto("failed", ex.Message));
                }

                using (StreamReader str = new StreamReader(ex.Response?.GetResponseStream()))
                {
                    string errorResponseJson = str?.ReadToEnd();
                    if (errorResponseJson == null)
                    {
                        return(new SignUpResponseDto("failed", "unknown error"));
                    }
                    return(JsonConvert.DeserializeObject <SignUpResponseDto>(errorResponseJson));
                }
            }
        }
示例#2
0
        internal static async Task <IEnumerable <Tuple <string, string> > > GetImagesFromUri(string title, Uri uri)
        {
            var href   = uri.OriginalString.Split('?')[0];
            var groups = hashRe.Match(href).Groups;

            if (groups.Count > 2 && string.IsNullOrWhiteSpace(groups[2].Value))
            {
                var hash = groups[1].Value;
                if (hash.StartsWith("m"))
                {
                    var    apiURL  = "http://min.us/api/GetItems/" + hash;
                    var    request = HttpWebRequest.CreateHttp(apiURL);
                    string jsonResult;
                    using (var response = (await SimpleHttpService.GetResponseAsync(request)))
                    {
                        jsonResult = await Task <string> .Run(() =>
                        {
                            using (var sr = new StreamReader(response.GetResponseStream()))
                            {
                                return(sr.ReadToEnd());
                            }
                        });
                    }
                    dynamic result = JsonConvert.DeserializeObject(jsonResult);
                    return(new Tuple <string, string>[] { Tuple.Create(title, (string)result.src) });
                }
                else
                {
                    return(Enumerable.Empty <Tuple <string, string> >());
                }
            }
            else
            {
                return(Enumerable.Empty <Tuple <string, string> >());
            }
        }
示例#3
0
        public static string Get(string url, string headerName, string headerValue, bool isLogToConsole)
        {
            string httpGet = "";

            if (String.IsNullOrEmpty(url) == false)
            {
                HttpWebRequest httpWebRequest = HttpWebRequest.CreateHttp(url);
                httpWebRequest.Method    = "GET";
                httpWebRequest.UserAgent = UserAgent;

                if ((String.IsNullOrEmpty(headerName) == false) && (String.IsNullOrEmpty(headerValue) == false))
                {
                    httpWebRequest.Headers.Add(headerName, headerValue);
                }

                if (isLogToConsole == true)
                {
                    OutputStreams.WriteLine($"Downloading from {url}...");
                }

                using (WebResponse httpWebResponse = httpWebRequest.GetResponseAsync().Result)
                {
                    using (StreamReader streamReader = new StreamReader(httpWebResponse.GetResponseStream()))
                    {
                        httpGet = streamReader.ReadToEnd().Replace("\r", Environment.NewLine);
                    }
                }

                if (isLogToConsole == true)
                {
                    OutputStreams.WriteLine("Finished downloading");
                }
            }

            return(httpGet);
        }
示例#4
0
        public static T Get <T>(string url, Dictionary <string, string> args)
        {
            var request = HttpWebRequest.CreateHttp(url);

            request.Method      = "POST";
            request.ContentType = "application/json";

            var parameters = JsonConvert.SerializeObject(args);

            using (var writer = new StreamWriter(request.GetRequestStream()))
            {
                writer.Write(parameters.ToString());
            }

            var response = request.GetResponse();
            var json     = string.Empty;

            using (var reader = new StreamReader(response.GetResponseStream()))
            {
                json = reader.ReadToEnd();
            }

            return(JsonConvert.DeserializeObject <T>(json));
        }
示例#5
0
文件: WebUser.cs 项目: BFine9/XamApp
        public async Task <UserList> GetUsers()
        {
            var request = HttpWebRequest.CreateHttp("https://internshiptaskuserslist.azurewebsites.net/api/users?code=gbgu4CbgdAlsS0xIVaNkckK4vTd0qIFNxaQYzIHLaqyomquJwuy/ig==");

            request.Method      = WebRequestMethods.Http.Get;
            request.ContentType = "application/json; charset=utf-8";
            await Task.Factory.FromAsync <WebResponse>(request.BeginGetResponse, request.EndGetResponse, null).ContinueWith(task =>
            {
                var response = (HttpWebResponse)task.Result;
                if (response.StatusCode == HttpStatusCode.OK)
                {
                    StreamReader responseReader = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
                    string responseData         = responseReader.ReadToEnd();
                    json = responseData.ToString();
                    responseReader.Close();
                    if (!String.IsNullOrEmpty(json))
                    {
                        List = JsonConvert.DeserializeObject <UserList>(json);
                    }
                }
            });

            return(List);
        }
示例#6
0
        private string GetDetailedDescription()
        {
            var url = Config.PropertyDetailedDescriptionApiUrlFormat
                      .UseFormat(Property.DeclaringType.FullName, Property.Name);
            var request = HttpWebRequest.CreateHttp(url);

            request.ReadWriteTimeout = Config.WebRequestTimeout;
            request.Timeout          = Config.WebRequestTimeout;
            request.MediaType        = "text/html";

            string result = null;

            using (var response = request.GetResponse())
                using (var stream = response.GetResponseStream())
                    using (var reader = new StreamReader(stream))
                        result = reader.ReadToEnd();

            if (result.Length > Config.PropertyMaxDetailedDescriptionLength)
            {
                throw new InvalidDataException("Text too long.");
            }

            return(result);
        }
示例#7
0
        public static string RedirectPath(string url)
        {
            StringBuilder sb       = new StringBuilder();
            string        location = string.Copy(url);

            while (!string.IsNullOrWhiteSpace(location))
            {
                sb.AppendLine(location); // you can also use 'Append'
                HttpWebRequest request = HttpWebRequest.CreateHttp(location);
                request.AllowAutoRedirect = false;
                using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
                {
                    location = response.GetResponseHeader("Location");
                    if (location.Contains("%3D") && location.Contains("%26"))
                    {
                        var leftIndex  = location.IndexOf("%3D");
                        var rightIndex = location.IndexOf("%26");
                        var appId      = location.Substring(leftIndex + 3, rightIndex - leftIndex - 3);
                        Console.WriteLine($"AppId:{appId}");
                    }
                }
            }
            return(sb.ToString());
        }
示例#8
0
        static void CrackPasswordForZTHBlog()
        {
            string url        = "http://zhu.tianhua.me/wp-login.php?action=postpass";
            string postString = "post_password=aaa&Submit=Submit";

            byte[] postData    = Encoding.UTF8.GetBytes(postString);
            string htmlContent = null;

            #region 使用WebClient类
            //WebClient webClient = new WebClient();
            //webClient.Headers.Add("Content-Type", "application/x-www-form-urlencoded");
            //byte[] responseData = webClient.UploadData(url, "POST", postData);
            //string srcString = Encoding.UTF8.GetString(responseData);
            //htmlContent = Encoding.UTF8.GetString(responseData);
            //webClient.Dispose();
            #endregion

            #region 使用HttpWebRequest类
            HttpWebRequest request = HttpWebRequest.CreateHttp(url);
            request.Referer       = "http://zhu.tianhua.me/archives/8329";
            request.ContentType   = "application/x-www-form-urlencoded";
            request.Method        = "POST";
            request.ContentLength = postData.Length;
            Stream requestStream = request.GetRequestStream();
            requestStream.Write(postData, 0, postData.Length);
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            Console.WriteLine(response.StatusCode);
            StreamReader reader = new StreamReader(new BufferedStream(response.GetResponseStream(), 4 * 200 * 1024));
            htmlContent = reader.ReadToEnd();
            response.Close();
            #endregion

            StreamWriter writer = new StreamWriter("zth.html");
            writer.Write(htmlContent);
            writer.Close();
        }
示例#9
0
文件: Server.cs 项目: Sache/WebComm
        public static byte[] WebRequester(string url, string postData)
        {
            if (!String.IsNullOrEmpty(url))
            {
                //Web Request
                HttpWebRequest request = HttpWebRequest.CreateHttp(url);
                request.Method = "POST";
                byte[] byteArray = Encoding.UTF8.GetBytes(postData);
                request.ContentType   = "application/x-www-form-urlencoded";
                request.ContentLength = byteArray.Length;
                Stream dataStream = request.GetRequestStream();
                dataStream.Write(byteArray, 0, byteArray.Length);
                // Close the Stream object.
                dataStream.Close();

                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                using (StreamReader responseStream = new StreamReader(response.GetResponseStream()))
                {
                    byte[] bytes = Encoding.UTF8.GetBytes(responseStream.ReadToEnd());
                    return(bytes);
                };
            }
            return(null);
        }
示例#10
0
        public JArray GetAllUserGroupList(ClientContext ctx, string ID)
        {
            try {
                RetrieveAccessToken(ctx);

                var Url = ctx.Url + "/_api/Web/GetUserById(" + ID + ")/Groups";

                HttpWebRequest request = HttpWebRequest.CreateHttp(Url);
                request.Accept = "application/json;odata=verbose";
                request.Headers.Add("Authorization", accessToken);
                Stream       webStream      = request.GetResponse().GetResponseStream();
                StreamReader responseReader = new StreamReader(webStream);
                string       response       = responseReader.ReadToEnd();
                JObject      jobj           = JObject.Parse(response);
                JArray       jarr           = (JArray)jobj["d"]["results"];


                return(jarr);
            }
            catch (Exception ex)
            {
                throw new Exception(string.Format("An error occured while reading data. GUID: {0}", ex.ToString()));
            }
        }
示例#11
0
        public static async Task <byte[]> GetFeaturedImage(string url)
        {
            byte[] output;

            try
            {
                // download the page contents as a string
                var request = HttpWebRequest.CreateHttp(url);
                request.Accept = "text/html";

                var result = await request.GetResponseAsync();

                using (var reader = new StreamReader(result.GetResponseStream()))
                {
                    string          pageContents = reader.ReadToEnd();
                    MatchCollection matches      = Regex.Matches(pageContents, @"<img ([^>]+)>");

                    // build a list of all img tags
                    IList <ImageTag> imageTags = new List <ImageTag>();
                    for (int i = 0; i < matches.Count && i < AppSettings.MaxImagesToLoad; i++)
                    {
                        imageTags.Add(await ImageTag.GetFromUrl(matches[i].Value, url));
                    }

                    var featured = imageTags.OrderByDescending(t => t.Width * t.Height).First();
                    output = featured.ImageBytes;
                }
            }
            catch
            {
                string fileName = Path.Combine(_webRootPath, AppSettings.DefaultPreviewImage);
                output = File.ReadAllBytes(fileName);
            }

            return(output);
        }
示例#12
0
        public string GetWebRequest(Uri address)
        {
            //HttpWebResponse resp = new HttpWebResponse();

            var req = HttpWebRequest.CreateHttp(address);

            req.ContinueTimeout = this._timeout;

            var resp   = req.GetResponseAsync().Result;
            var stream = resp.GetResponseStream();

            byte[] b = new byte[stream.Length];
            stream.Read(b, 0, (int)stream.Length);

            var result = System.Text.Encoding.UTF8.GetString(b);

            return(result);

            ////  resp.Result

            //var result = base.GetWebRequest(address);
            //result.Timeout = this._timeout;
            //return result;
        }
示例#13
0
 public void LoadData(ContentProviderResultCallback resultCallback, string name)
 {
     httpWebRequest = HttpWebRequest.CreateHttp("http://www.itemis.de/applause/people/de/" + name.URLEncode() + ".xml");
     httpWebRequest.BeginGetResponse(new AsyncCallback(FetchData), resultCallback);
 }
示例#14
0
        public static async Task <PushHttpResponse> RequestAsync(PushHttpRequest request)
        {
            var httpRequest = HttpWebRequest.CreateHttp(request.Url);

            httpRequest.Proxy = null;

            httpRequest.Headers = request.Headers;

            if (!string.IsNullOrEmpty(request.Body))
            {
                var requestStream = await httpRequest.GetRequestStreamAsync();

                var requestBody = request.Encoding.GetBytes(request.Body);

                await requestStream.WriteAsync(requestBody, 0, requestBody.Length);
            }

            HttpWebResponse httpResponse;
            Stream          responseStream;

            try
            {
                httpResponse = await httpRequest.GetResponseAsync() as HttpWebResponse;

                responseStream = httpResponse.GetResponseStream();
            }
            catch (WebException webEx)
            {
                httpResponse = webEx.Response as HttpWebResponse;

                responseStream = httpResponse.GetResponseStream();
            }

            var body = string.Empty;

            using (var sr = new StreamReader(responseStream))
                body = await sr.ReadToEndAsync();

            var responseEncoding = Encoding.ASCII;

            try
            {
                responseEncoding = Encoding.GetEncoding(httpResponse.ContentEncoding);
            }
            catch
            {
            }

            var response = new PushHttpResponse
            {
                Body         = body,
                Headers      = httpResponse.Headers,
                Uri          = httpResponse.ResponseUri,
                Encoding     = responseEncoding,
                LastModified = httpResponse.LastModified,
                StatusCode   = httpResponse.StatusCode
            };

            httpResponse.Close();
            httpResponse.Dispose();

            return(response);
        }
示例#15
0
        /// <summary>
        /// 发起web请求
        /// </summary>
        /// <param name="method">请求方式:GET,POST</param>
        /// <param name="url">请求地址</param>
        /// <param name="contentType"></param>
        /// <param name="postData">请求方式为POST时,传入请求数据</param>
        /// <param name="cookie">如果需要cookie则传入,否则传入null即可</param>
        /// <returns></returns>
        public static string Request(Method method, string url, string contentType = null, string postData = null, Cookie cookie = null)
        {
            HttpWebRequest webRequest = null;


            string responseData = "";

            webRequest = HttpWebRequest.CreateHttp(url);

            webRequest.Method = method.ToString();
            webRequest.ServicePoint.Expect100Continue = false;
            webRequest.KeepAlive = true;


            if (cookie != null)
            {
                webRequest.CookieContainer = new CookieContainer();
                webRequest.CookieContainer.Add(cookie);
            }

            if (method == Method.POST)
            {
                if (string.IsNullOrEmpty(contentType))
                {
                    webRequest.ContentType = "application/x-www-form-urlencoded";
                }
                else
                {
                    webRequest.ContentType = contentType;
                }

                if (!string.IsNullOrEmpty(postData))
                {
                    StreamWriter requestWriter = null;

                    requestWriter = new StreamWriter(webRequest.GetRequestStream());

                    try
                    {
                        requestWriter.Write(postData);
                    }
                    catch (Exception e)
                    {
                        throw e;
                    }
                    finally
                    {
                        requestWriter.Close();
                        requestWriter = null;
                    }
                }
            }

            #region 请求头加入信息
            //webRequest.Headers.Add("staffid", "staffid1111"); //当前请求用户StaffId
            //webRequest.Headers.Add("timestamp", "timeStamp222"); //发起请求时的时间戳(单位:毫秒)
            //webRequest.Headers.Add("nonce", "nonce333"); //发起请求时的时间戳(单位:毫秒)
            //webRequest.Headers.Add("sign", "sign444"); //当前请求内容的数字签名
            #endregion
            responseData = WebResponseGet(webRequest);
            webRequest   = null;

            return(responseData);
        }
示例#16
0
        async Task <Response> ExecuteRequest <Request, Response>(Request request) where Request : IPOLiPaymentRequest where Response : IPOLiPaymentResponse
        {
            request.Validate();

            string
                jsonPayload = JsonConvert.SerializeObject(request),
                auth        = System.Convert.ToBase64String(Encoding.UTF8.GetBytes($"{this.MerchantCode}:{this.AuthenticationCode}"));

            string
                apiEndpoint,
                apiMethod = "POST";

            if (typeof(Request) == typeof(InitiateTransactionRequest))
            {
                apiEndpoint = "https://poliapi.apac.paywithpoli.com/api/v2/Transaction/Initiate";
            }
            else if (typeof(Request) == typeof(GetTransactionRequest))
            {
                apiEndpoint = $"https://poliapi.apac.paywithpoli.com/api/v2/Transaction/GetTransaction?token={(request as GetTransactionRequest).TransactionToken}";
                apiMethod   = "GET";
                jsonPayload = null;
            }
            else
            {
                throw new Exception();
            }

            HttpWebRequest httpRequest = HttpWebRequest.CreateHttp(apiEndpoint);

            httpRequest.Method = apiMethod;
            httpRequest.Headers.Add("Authorization", $"Basic {auth}");

            if (!String.IsNullOrEmpty(jsonPayload))
            {
                httpRequest.ContentType   = "application/json";
                httpRequest.ContentLength = jsonPayload.Length;

                using (Stream httpRequestStream = httpRequest.GetRequestStream())
                    using (StreamWriter sw = new StreamWriter(httpRequestStream))
                    {
                        sw.Write(jsonPayload);
                    }
            }

            string       httpResponsePayload;
            WebException webException = null;

            try
            {
                HttpWebResponse httpResponse = await httpRequest.GetResponseAsync() as HttpWebResponse;

                using (Stream httpResponseStream = httpResponse.GetResponseStream())
                    using (StreamReader sr = new StreamReader(httpResponseStream))
                    {
                        httpResponsePayload = await sr.ReadToEndAsync();
                    }
            } catch (WebException ex)
            {
                WebResponse exceptionResponse = ex.Response;
                webException = ex;
                using (Stream exceptionResponseStream = exceptionResponse.GetResponseStream())
                    using (StreamReader sr = new StreamReader(exceptionResponseStream))
                    {
                        // try to parse the exception payload, as the API should throw an error code, but still return the same type of JSON, with {success: false} and an error message
                        httpResponsePayload = await sr.ReadToEndAsync();
                    }
            } catch (Exception)
            {
                throw;
            }

            try
            {
                return(JsonConvert.DeserializeObject <Response>(httpResponsePayload));
            } catch (Exception)
            {
                if (webException != null)
                {
                    throw new Exception(httpResponsePayload, webException);
                }

                throw;
            }
        }
 public void LoadData(ContentProviderResultCallback resultCallback)
 {
     httpWebRequest = HttpWebRequest.CreateHttp(DATA_URL);
     httpWebRequest.BeginGetResponse(new AsyncCallback(FetchData), resultCallback);
 }
示例#18
0
 // send url parameters as vararg parameter? might make it easier to generate the code!
 public void LoadData(string name, ContentProviderResultCallback resultCallback)
 {
     httpWebRequest = HttpWebRequest.CreateHttp(BuildDataURL(name));
     httpWebRequest.BeginGetResponse(new AsyncCallback(FetchData), resultCallback);
 }
示例#19
0
        private static void APICall(String f_stParam, int packageId, int f_iPlanID = 0)
        {
            int serviceId             = DataProvider.GetPackageServiceId(SecurityContext.User.UserId, packageId, ResourceGroups.Filters);
            StringDictionary settings = ServerController.GetServiceSettings(serviceId);
            String           l_URL    = string.Empty;

            try
            {
                if (f_iPlanID == 0)
                {
                    l_URL = getServiceURLFromPackageId(packageId);
                }
                else
                {
                    l_URL = getServiceURL(f_iPlanID);
                }
            }
            catch (Exception ex)
            {
                throw (ex);
                //throw( new Exception("MAILCLEANER_API_404"));
            }

            if (String.IsNullOrEmpty(l_URL))
            {
                return;
            }

            // Set to use TLS1.2
            ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
            //"https://10.2.150.107/api/"
            // Create a request for the URL.
            HttpWebRequest request = HttpWebRequest.CreateHttp(l_URL + f_stParam);

            // If required by the server, set the credentials.
            request.Credentials = CredentialCache.DefaultCredentials;
            if (Utils.ParseBool(settings[MailCleanerContants.IgnoreCheckSSL], true))
            {
                request.ServerCertificateValidationCallback += (sender, certificate, chain, sslPolicyErrors) => true;
            }
            // Get the response.
            WebResponse response = request.GetResponse();
            // Display the status.
            //Console.WriteLine(((HttpWebResponse)response).StatusDescription);
            // Get the stream containing content returned by the server.
            Stream dataStream = response.GetResponseStream();
            // Open the stream using a StreamReader for easy access.
            StreamReader reader = new StreamReader(dataStream);

            // Display the content.
            //Console.WriteLine(responseFromServer);
            if (response != null)
            {
                XmlDocument xmlResult = new XmlDocument();
                // Read the content.
                String lstXMLResult = reader.ReadToEnd();
                xmlResult.LoadXml(lstXMLResult);
                String lstMessage = String.Empty;
                foreach (XmlNode lxmNode in xmlResult.ChildNodes)
                {
                    if (lxmNode.Name == "response")
                    {
                        if (lxmNode.FirstChild.Name == "message")
                        {
                            lstMessage = lxmNode.FirstChild.InnerText;
                        }
                    }
                }
                // MessageBox.Show(lstMessage);
            }
            // Clean up the streams and the response.
            reader.Close();
            response.Close();
        }
示例#20
0
        public static async Task <List <HuxiImgEntry> > GetEntries()
        {
            List <HuxiImgEntry> entries = new List <HuxiImgEntry>(24);

            HttpWebRequest request = HttpWebRequest.CreateHttp("http://huxi.cqu.edu.cn/tsnewjson/1");

            //request.Referer = "http://huxi.cqu.edu.cn/newsclass/7f0e208b91d235fd";
            request.Headers[HttpRequestHeader.Referer] = "http://huxi.cqu.edu.cn/newsclass/7f0e208b91d235fd";
            request.Accept = "application/json, text/javascript, */*; q=0.01";
            //request.UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.140 Safari/537.36 Edge/17.17134";
            request.Headers[HttpRequestHeader.UserAgent] = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.140 Safari/537.36 Edge/17.17134";

            HttpWebResponse response = null;

            try
            {
                response = await request.GetResponseAsync() as HttpWebResponse;
            }
            catch (WebException)
            {
                return(new List <HuxiImgEntry>());
            }
            StreamReader reader      = new StreamReader(response.GetResponseStream());
            string       responseStr = reader.ReadToEnd();

            Regex           entryRegex   = new Regex("\\{\"pichtml\".*?\\}");
            MatchCollection entryMatches = entryRegex.Matches(responseStr);

            foreach (Match m in entryMatches)
            {
                string matchValue = m.Value;

                Regex localRegex = new Regex(@"src=\\"".*?\\""");
                //Regex localRegex = new Regex(@"data-pic=\\"".*?\\""");

                Match  localMatch = localRegex.Match(matchValue);
                string imgUri     = localMatch.Value.Split('"')[1];
                imgUri = "http://huxi.cqu.edu.cn" + imgUri.Remove(imgUri.Length - 1);

                localRegex = new Regex(@"<p>.*?</p>");
                MatchCollection localMatches = localRegex.Matches(matchValue);
                string          title        = localMatches[0].Value;
                title = title.Replace("<p>", "");
                title = title.Replace("</p>", "");
                title.Trim();

                string content = localMatches[1].Value;
                content = content.Replace("<p>", "");
                content = content.Replace("</p>", "");
                content.Trim();

                localRegex = new Regex(@"<span class=\\""name\\"">.*?</span>");
                localMatch = localRegex.Match(matchValue);
                string author           = localMatch.Value.Split('>')[1];
                int    parenthesisIndex = author.IndexOf("(");
                if (parenthesisIndex != -1)
                {
                    author = author.Remove(parenthesisIndex);
                }
                else
                {
                    parenthesisIndex = author.IndexOf("<");
                    if (parenthesisIndex != 0)
                    {
                        author = author.Remove(parenthesisIndex);
                    }
                }
                //author = author.Replace("(图片版权归作者所有)</span", "");
                author = author.Replace("摄影:", "");
                entries.Add(new HuxiImgEntry(title, imgUri, content, author));
            }

            return(entries);
        }
示例#21
0
        public static async Task <PushHttpResponse> RequestAsync(PushHttpRequest request)
        {
#if NETSTANDARD
            WinHttpHandler httpHandler = new WinHttpHandler();
            httpHandler.SslProtocols = SslProtocols.Tls12;
            HttpClient client = new HttpClient(httpHandler);

            client.DefaultRequestHeaders.Clear();
            foreach (var headerKey in request.Headers.AllKeys)
            {
                client.DefaultRequestHeaders.TryAddWithoutValidation(headerKey, request.Headers[headerKey]);
            }

            HttpMethod method = HttpMethod.Get;
            switch (request.Method)
            {
            case "GET":
                method = HttpMethod.Get;
                break;

            case "POST":
                method = HttpMethod.Post;
                break;

            case "PUT":
                method = HttpMethod.Put;
                break;
            }

            HttpRequestMessage message = new HttpRequestMessage(method, request.Url);
            HttpContent        content = new StringContent(request.Body, request.Encoding);
            message.Content = content;

            HttpResponseMessage httpResponse = await client.SendAsync(message);

            WebHeaderCollection responseHeaderCollection = new WebHeaderCollection();
            foreach (var responseHeader in httpResponse.Headers)
            {
                responseHeaderCollection[responseHeader.Key] = responseHeader.Value.FirstOrDefault();
            }

            PushHttpResponse response = new PushHttpResponse
            {
                Body         = await httpResponse.Content.ReadAsStringAsync(),
                Headers      = responseHeaderCollection,
                Uri          = httpResponse.RequestMessage.RequestUri,
                Encoding     = Encoding.GetEncoding(httpResponse.Content.Headers.ContentEncoding.First()),
                LastModified = httpResponse.Content.Headers.LastModified == null ? DateTime.Now : httpResponse.Content.Headers.LastModified.Value.DateTime,
                StatusCode   = httpResponse.StatusCode
            };

            client.Dispose();
            return(response);
#else
            var httpRequest = HttpWebRequest.CreateHttp(request.Url);
            httpRequest.Proxy = null;

            httpRequest.Headers = request.Headers;

            if (!string.IsNullOrEmpty(request.Body))
            {
                var requestStream = await httpRequest.GetRequestStreamAsync();

                var requestBody = request.Encoding.GetBytes(request.Body);

                await requestStream.WriteAsync(requestBody, 0, requestBody.Length);
            }

            HttpWebResponse httpResponse;
            Stream          responseStream;

            try {
                httpResponse = await httpRequest.GetResponseAsync() as HttpWebResponse;

                responseStream = httpResponse.GetResponseStream();
            } catch (WebException webEx) {
                httpResponse = webEx.Response as HttpWebResponse;

                responseStream = httpResponse.GetResponseStream();
            }

            var body = string.Empty;

            using (var sr = new StreamReader(responseStream))
                body = await sr.ReadToEndAsync();

            var responseEncoding = Encoding.ASCII;
            try {
                responseEncoding = Encoding.GetEncoding(httpResponse.ContentEncoding);
            } catch {
            }

            var response = new PushHttpResponse {
                Body         = body,
                Headers      = httpResponse.Headers,
                Uri          = httpResponse.ResponseUri,
                Encoding     = responseEncoding,
                LastModified = httpResponse.LastModified,
                StatusCode   = httpResponse.StatusCode
            };

            httpResponse.Close();
            httpResponse.Dispose();

            return(response);
#endif
        }
示例#22
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="uri"></param>
        /// <param name="completeOnHeaders"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        /// <exception cref="WebClientException">Thrown when there's a WebException.</exception>
        /// <exception cref="ArgumentNullException">Thrown when <paramref name="uri"/> is null.</exception>
        /// <exception cref="ArgumentOutOfRangeException">Thrown when the specified <paramref name="timeout"/> is less than 0.</exception>
        /// <exception cref="OperationCanceledException">Thrown when cancelled by caller.</exception>
        public async Task <IWebResponseMessage> GetAsync(Uri uri, int timeout, CancellationToken cancellationToken)
        {
            if (uri == null)
            {
                throw new ArgumentNullException(nameof(uri), "uri cannot be null.");
            }
            if (timeout < 0)
            {
                throw new ArgumentOutOfRangeException(nameof(timeout), "timeout cannot be less than 0.");
            }
            if (timeout == 0)
            {
                timeout = Timeout;
            }
            var request = HttpWebRequest.CreateHttp(uri);

            if (!string.IsNullOrEmpty(UserAgent))
            {
                request.UserAgent = UserAgent;
            }
            Task cancelTask = null;

            if (timeout != 0)
            {
                request.Timeout          = timeout;
                request.ReadWriteTimeout = timeout;
                // TODO: This doesn't seem to do anything.
                //request.ContinueTimeout = Timeout;
            }
            if (cancellationToken.IsCancellationRequested)
            {
                throw new OperationCanceledException($"GetAsync canceled for Uri {uri}", cancellationToken);
            }
            try
            {
                //var getTask = request.GetResponseAsync();
                var getTask = Task.Run(() =>
                {
                    return(request.GetResponse()); // Have to use synchronous call because GetResponseAsync() doesn't respect Timeout
                });
                //TODO: Need testing for cancellation token
                if (cancellationToken.CanBeCanceled)
                {
                    cancelTask = cancellationToken.AsTask();
                    await Task.WhenAny(getTask, cancelTask).ConfigureAwait(false);

                    if (!getTask.IsCompleted) // either getTask completed or cancelTask was triggered
                    {
                        throw new OperationCanceledException($"GetAsync canceled for Uri {uri}", cancellationToken);
                    }
                    //cancelTask?.Dispose(); Can't dispose of a task that isn't Completed
                }
                var response = await getTask.ConfigureAwait(false); // either there's no cancellationToken or it's already completed

                return(new WebClientResponseWrapper(response as HttpWebResponse, request));
            }
            catch (ArgumentException ex)
            {
                if (ErrorHandling != ErrorHandling.ReturnEmptyContent)
                {
                    throw;
                }
                else
                {
                    return(new WebClientResponseWrapper(null, null, ex));
                }
            }
            catch (WebException ex)
            {
                HttpWebResponse resp           = ex.Response as HttpWebResponse;
                var             statusOverride = WebExceptionStatusToHttpStatus(ex.Status);
                // This is thrown by HttpWebRequest.GetResponseAsync(), so we can't throw the exception here or the calling code won't be able to decide how to handle it...sorta

                if (ErrorHandling == ErrorHandling.ThrowOnException)
                {
                    string    message      = string.Empty;
                    Exception retException = ex;
                    if (ex.Status == WebExceptionStatus.Timeout)
                    {
                        message      = Utilities.GetTimeoutMessage(uri);
                        retException = new TimeoutException(message);
                    }
                    else
                    {
                        message = ex.Message;
                    }
                    throw new WebClientException(message, retException, new WebClientResponseWrapper(resp, request, retException, statusOverride));
                }
                else
                {
                    //Logger?.Log(LogLevel.Error, $"Exception getting {uri?.ToString()}\n{ex.Message}\n{ex.StackTrace}");
                    return(new WebClientResponseWrapper(resp, request, ex, statusOverride));
                }
            }
            catch (OperationCanceledException ex) // Timeout, could also be caught by WebException
            {
                Exception retException = ex;
                if (ErrorHandling == ErrorHandling.ThrowOnException)
                {
                    if (cancellationToken.IsCancellationRequested)
                    {
                        throw; // Cancelled by caller, rethrow
                    }
                    else
                    {
                        retException = new TimeoutException(Utilities.GetTimeoutMessage(uri));
                        throw new WebClientException(retException.Message, retException, new WebClientResponseWrapper(null, request, retException, 408));
                    }
                }
                else
                {
                    //Logger?.Log(LogLevel.Error, $"Exception getting {uri?.ToString()}\n{ex.Message}\n{ex.StackTrace}");
                    return(new WebClientResponseWrapper(null, request, ex, 408));
                }
            }
            finally
            {
                //if (cancelTask != null)
                //    cancelTask.Dispose();
            }
        }
 public void LoadData(ContentProviderResultCallback resultCallback)
 {
     httpWebRequest = HttpWebRequest.CreateHttp("http://www.itemis.de/language=de/~xml.timeline/37606");
     httpWebRequest.BeginGetResponse(new AsyncCallback(FetchData), resultCallback);
 }
示例#24
0
        public override async Task <ResponseEntity> SendAsync(RequestEntity requestEntity)
        {
            if (requestEntity.Cookie == null)
            {
                requestEntity.Cookie = this.CookieManager;
            }
            //准备
            HttpWebRequest request = HttpWebRequest.CreateHttp(requestEntity.Url);

            request.Headers     = new WebHeaderCollection();
            request.ContentType = requestEntity.ContentType;
            request.Accept      = requestEntity.Accept;
            //request.AllowAutoRedirect = false;
            request.Connection = requestEntity.Connection;
            request.UserAgent  = requestEntity.UserAgent;
            request.Referer    = requestEntity.Referer;
            foreach (var header in requestEntity.Headers)
            {
                request.Headers.Add(header.Key, header.Value);
            }
            request.Method          = requestEntity.Method.ToString();
            request.CookieContainer = requestEntity.Cookie;

            //请求
            if (requestEntity.Method == RequestMethod.POST)
            {
                request.ContentLength = requestEntity.SendData.Length;
                request.GetRequestStream().Write(requestEntity.SendData, 0, requestEntity.SendData.Length);
                request.GetRequestStream().Close();
            }
            var             resultEntity = new ResponseEntity();
            HttpWebResponse response     = null;

            try
            {
                response = (HttpWebResponse)await request.GetResponseAsync();
            }
            catch (WebException e)
            {
                response = (HttpWebResponse)e.Response;
            }
            //结果整理

            int         readCount      = 1;
            List <byte> resultHtmlByte = new List <byte>();

            byte[] buffer = new byte[1024];
            while (readCount > 0)
            {
                readCount = await response.GetResponseStream().ReadAsync(buffer, 0, buffer.Length);

                resultHtmlByte.AddRange(buffer.Take(readCount));
            }
            resultEntity.HtmlContent  = resultHtmlByte.ToArray();
            resultEntity.StatusCode   = (int)response.StatusCode;
            resultEntity.CharacterSet = response.CharacterSet;
            resultEntity.Headers      = response.Headers;
            resultEntity.Response     = response;
            resultEntity.ResponseUrl  = response.ResponseUri;
            return(resultEntity);
        }
示例#25
0
        private static List <DownloadData> ParseSite(ShowData showData, string url, out string nextpageurl, out string firstcover, UploadCache uploadCache)
        {
            nextpageurl = "";
            firstcover  = "";

            HttpWebRequest req = HttpWebRequest.CreateHttp(url);

            req.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;
            var            resp       = req.GetResponse();
            BufferedStream buffStream = new BufferedStream(req.GetResponse().GetResponseStream());

            url = resp.ResponseUri.ToString();
            StreamReader reader = new StreamReader(buffStream);

            bool inContent           = false;
            bool inPost              = false;
            bool inPostContent       = false;
            List <DownloadData> list = new List <DownloadData>();
            Match      m;
            SeasonData seasonData = null;
            UploadData uploadData = null;

            while (!reader.EndOfStream)
            {
                String line = reader.ReadLine();
                if (line.Contains("line-height") && line.Contains("&nbsp;")) //detect: <div style="line-height:5px;height:5px;background-color:lightgrey;">&nbsp;</div>
                {
                    continue;                                                //I can only hope this will never occour in any other case..
                }
                if (inContent)
                {
                    if (inPost)
                    {
                        if (inPostContent)
                        {
                            //Debug.Assert(seasonData!=null);
                            if (seasonData == null)
                            {
                                Console.WriteLine("Warning: Invalid Html received while parsing " + showData.Name + ". Trying to continue");
                                continue;
                            }
                            if ((m = new Regex("<p>\\s*<strong>\\s*(.+?)\\s*</strong>\\s*[^/]*\\s*<br\\s?/>").Match(line)).Success)
                            {
                                //Debug.Assert(uploadData != null);
                                if (uploadData == null)
                                {
                                    Console.WriteLine("Warning: Invalid Html received while parsing " + showData.Name + ". Trying to continue");
                                    continue;
                                }
                                string title = WebUtility.HtmlDecode(m.Groups[1].Value);

                                var   downloads = new Dictionary <string, string>();
                                Regex r         = new Regex("<a\\s+href\\s*=\"([^\"]+)\".+\\s+(.+?)\\s*<");
                                while (true)
                                {
                                    line = reader.ReadLine();
                                    Match m2 = r.Match(line);
                                    if (m2.Success)
                                    {
                                        String keyOrg = WebUtility.HtmlDecode(m2.Groups[2].Value);
                                        String key    = keyOrg;
                                        while (downloads.ContainsKey(key))
                                        {
                                            Match mx  = new Regex("\\((\\d+)\\)$").Match(key);
                                            int   num = 1;
                                            if (mx.Success)
                                            {
                                                num = int.Parse(mx.Groups[1].Value);
                                            }

                                            key = keyOrg + "(" + ++num + ")";
                                        }
                                        String val = m2.Groups[1].Value;
                                        if (val != null && !String.IsNullOrWhiteSpace(val) && val.Trim().StartsWith("http://"))
                                        {
                                            downloads.Add(key, val);
                                        }
                                        else
                                        {
                                            Console.WriteLine("Warning: Invalid Download received while parsing " + showData.Name + ". Ignoring link");
                                            //ignoring invalid download
                                        }
                                    }
                                    if (line.Contains("</p>"))
                                    {
                                        break;
                                    }
                                }

                                if (title.Contains("720p"))
                                {
                                    uploadData.Format = "720p";
                                }
                                else if (title.Contains("1080p"))
                                {
                                    uploadData.Format = "1080p";
                                }
                                else if (title.Contains("720i"))
                                {
                                    uploadData.Format = "720i";
                                }
                                else if (title.Contains("1080i"))
                                {
                                    uploadData.Format = "1080i";
                                }

                                DownloadData dd = new DownloadData();
                                dd.Upload = uploadCache == null ? uploadData : uploadCache.GetUniqueUploadData(uploadData);
                                dd.Title  = title;

                                if (title.ToLower().Contains("subbed"))
                                {
                                    dd.Upload.Subbed = true;
                                }

                                foreach (var download in downloads)
                                {
                                    dd.Links.Add(download.Key, download.Value);
                                }

                                list.Add(dd);
                            }
                            else if ((m = new Regex("(?:(?:<p(?:\\s+style\\s*\\=\\s*\\\"[^\\\"]+\\\"\\s*)?>)|(?:<div\\s+class\\s*=\\s*\"info\">))\\s*(.*?(?:Dauer|Größe|Sprache|Format|Uploader).*?)\\s*(?:(?:</p>)|(?:</div>))").Match(line)).Success ||
                                     ((m = new Regex("<p\\s+style\\s*\\=\\s*\\\"[^\\\"]+\\\"\\s*>").Match(line)).Success && ((line = reader.ReadLine()) != "") && (m = new Regex("\\s*(.*?(?:Dauer|Größe|Sprache|Format|Uploader).*?)\\s*</p>").Match(line)).Success))
                            {
                                /*
                                 * Nice case:
                                 * <p><strong>Dauer:</strong> 20:00 | <strong>Größe:</strong> 175 MB | <strong>Sprache:</strong> Englisch &amp; deutsche Untertitel | <strong>Format:</strong> XviD | <strong>HQ-Cover:</strong> <a href="http://justpic.info/?s=cover&amp;id=&amp;name=&amp;keyword=VGhlIEJpZyBCYW5nIFRoZW9yeQ,,">Download</a> | <strong>Uploader:</strong> block06</p>
                                 *
                                 * Bad case: (note newline!!)
                                 * <p style="background-color: #f0f0f0;">
                                 *  <strong>Dauer:</strong> 20:00 | <strong>Größe:</strong> 175 MB | <strong>Sprache:</strong> Englisch | <strong>Format:</strong> XviD | <strong>HQ-Cover:</strong> <a href="http://justpic.info/?s=cover&#038;id=&#038;name=&#038;keyword=VGhlIEJpZyBCYW5nIFRoZW9yeQ,,">Download</a> | <strong>Uploader:</strong> block06</p>
                                 */

                                uploadData        = new UploadData();
                                uploadData.Season = seasonData;

                                String          c  = WebUtility.HtmlDecode(m.Groups[1].Value);
                                MatchCollection mc = new Regex("<strong>\\s*(.+?)\\s*</strong>\\s*(.+?)\\s*(?:\\||$)").Matches(c);
                                foreach (Match match in mc)
                                {
                                    String key   = match.Groups[1].Value.ToLower();
                                    String value = match.Groups[2].Value;
                                    if (key.Contains("dauer") || key.Contains("runtime") || key.Contains("duration"))
                                    {
                                        uploadData.Runtime = value;
                                    }
                                    else if (key.Contains("grösse") || key.Contains("größe") || key.Contains("size"))
                                    {
                                        uploadData.Size = value;
                                    }
                                    else if (key.Contains("uploader"))
                                    {
                                        uploadData.Uploader = value;
                                    }
                                    else if (key.Contains("format"))
                                    {
                                        uploadData.Format = value;
                                    }
                                    else if (key.Contains("sprache") || key.Contains("language"))
                                    {
                                        value = value.ToLower();
                                        if (value.Contains("deutsch") || value.Contains("german"))
                                        {
                                            uploadData.Language |= UploadLanguage.German;
                                        }
                                        if (value.Contains("englisch") || value.Contains("english"))
                                        {
                                            uploadData.Language |= UploadLanguage.English;
                                        }
                                        if (value.Contains("subbed"))
                                        {
                                            uploadData.Subbed = true;
                                        }
                                    }
                                }
                            }
                            else if ((m = new Regex("<p>\\s*([^<]+)\\s*</p>").Match(line)).Success)
                            {
                                if (seasonData.Description == "")
                                {
                                    seasonData.Description = WebUtility.HtmlDecode(m.Groups[1].Value);
                                }
                            }
                            else if ((m = new Regex("<p>\\s*<img\\s+src\\s*=\"([^\"]+)\".*?/>\\s*</p>").Match(line)).Success)
                            {
                                seasonData.CoverUrl = m.Groups[1].Value;
                                if (firstcover == "")
                                {
                                    firstcover = seasonData.CoverUrl;
                                }
                            }
                            else if (new Regex("</div>").Match(line).Success)
                            {
                                inPostContent = false;
                                seasonData    = null;
                                uploadData    = null;
                            }
                        }
                        else if ((m = new Regex("<h2>\\s*<a\\s+href\\s*=\"([^\"]+)\".*?>(.+?)</a>\\s*</h2>").Match(line)).Success)
                        {
                            seasonData      = new SeasonData();
                            seasonData.Show = showData;

                            seasonData.Url   = m.Groups[1].Value;
                            seasonData.Title = WebUtility.HtmlDecode(m.Groups[2].Value);
                        }
                        else if (new Regex("<div\\s+class\\s*=\\s*\"post-content\"\\s*>").Match(line).Success)
                        {
                            inPostContent = true;
                        }
                        else if (new Regex("</div>").Match(line).Success)
                        {
                            inPost = false;
                        }
                    }
                    else if (new Regex("<div\\s+class\\s*=\\s*\"post\"\\s*>").Match(line).Success)
                    {
                        inPost = true;
                    }
                    else
                    {
                        if ((m = new Regex("<span\\s+class\\s*=\\s*'page\\s+current'>\\s*(\\d+)\\s*</span>").Match(line)).Success)
                        {
                            int currentPage = int.Parse(m.Groups[1].Value);
                            int nextPage    = currentPage + 1;
                            if (new Regex("title\\s*='" + nextPage + "'").Match(line).Success)
                            {
                                if (new Regex("/page/" + currentPage + "/?$").Match(url).Success)
                                {
                                    nextpageurl = url.Replace("page/" + currentPage, "page/" + nextPage);
                                }
                                else
                                {
                                    nextpageurl = url;
                                    if (!nextpageurl.EndsWith("/"))
                                    {
                                        nextpageurl += "/";
                                    }
                                    nextpageurl += "page/" + nextPage + "/";
                                }
                            }
                        }
                        if (new Regex("</div>").Match(line).Success)
                        {
                            inContent = false;
                        }
                    }
                }
                else if (new Regex("<div\\s+id\\s*=\\s*\"content\"\\s*>").Match(line).Success)
                {
                    inContent = true;
                }
            }
            reader.Close();
            return(list);
        }
示例#26
0
        /// <summary>
        /// ASYNC - Make a request to a path on the API
        /// </summary>
        /// <param name="url">Path and query</param>
        /// <param name="baseUrl">Override the base url, null for the default BaseUrl</param>
        /// <param name="payload">Payload, can be null. For private API end points, the payload must contain a 'nonce' key set to GenerateNonce value.</param>
        /// The encoding of payload is API dependant but is typically json.</param>
        /// <param name="method">Request method or null for default</param>
        /// <returns>Raw response</returns>
        public async Task <string> MakeRequestAsync(string url, string baseUrl = null, Dictionary <string, object> payload = null, string method = null)
        {
            await new SynchronizationContextRemover();

            await api.RateLimit.WaitToProceedAsync();

            if (string.IsNullOrWhiteSpace(url))
            {
                return(null);
            }
            else if (url[0] != '/')
            {
                url = "/" + url;
            }

            string         fullUrl = (baseUrl ?? api.BaseUrl) + url;
            Uri            uri     = api.ProcessRequestUrl(new UriBuilder(fullUrl), payload);
            HttpWebRequest request = HttpWebRequest.CreateHttp(uri);

            request.Headers["Accept-Language"] = "en-US,en;q=0.5";
            request.Method      = method ?? api.RequestMethod;
            request.ContentType = api.RequestContentType;
            request.UserAgent   = BaseAPI.RequestUserAgent;
            request.CachePolicy = api.RequestCachePolicy;
            request.Timeout     = request.ReadWriteTimeout = (int)api.RequestTimeout.TotalMilliseconds;
            try
            {
                // not supported on some platforms
                request.ContinueTimeout = (int)api.RequestTimeout.TotalMilliseconds;
            }
            catch
            {
            }
            request.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;
            await api.ProcessRequestAsync(request, payload);

            HttpWebResponse response       = null;
            string          responseString = null;

            try
            {
                try
                {
                    RequestStateChanged?.Invoke(this, RequestMakerState.Begin, null);
                    response = await request.GetResponseAsync() as HttpWebResponse;

                    if (response == null)
                    {
                        throw new APIException("Unknown response from server");
                    }
                }
                catch (WebException we)
                {
                    response = we.Response as HttpWebResponse;
                    if (response == null)
                    {
                        throw new APIException(we.Message ?? "Unknown response from server");
                    }
                }
                using (Stream responseStream = response.GetResponseStream())
                {
                    responseString = new StreamReader(responseStream).ReadToEnd();
                    if (response.StatusCode != HttpStatusCode.OK)
                    {
                        // 404 maybe return empty responseString
                        if (string.IsNullOrWhiteSpace(responseString))
                        {
                            throw new APIException(string.Format("{0} - {1}",
                                                                 response.StatusCode.ConvertInvariant <int>(), response.StatusCode));
                        }
                        throw new APIException(responseString);
                    }
                    api.ProcessResponse(response);
                    RequestStateChanged?.Invoke(this, RequestMakerState.Finished, responseString);
                }
            }
            catch (Exception ex)
            {
                RequestStateChanged?.Invoke(this, RequestMakerState.Error, ex);
                throw;
            }
            finally
            {
                response?.Dispose();
            }
            return(responseString);
        }
示例#27
0
 public void DownloadStringAsync(string uri,object userToken)
 {
     _webRequest = HttpWebRequest.CreateHttp(uri);
     _webRequest.Headers[HttpRequestHeader.IfModifiedSince] = DateTime.UtcNow.ToString("yyyy/MM/dd HH:mm:ss");
     _webRequest.BeginGetResponse(RespCallback, userToken);
 }
        private void ReadIndexRequestCallback(IAsyncResult callbackResult)
        {
            Dispatcher.BeginInvoke(() => { progressBar.Value = 1; });
            HttpWebRequest  myRequest = (HttpWebRequest)callbackResult.AsyncState;
            HttpWebResponse myResponse;

            try
            {
                myResponse = (HttpWebResponse)myRequest.EndGetResponse(callbackResult);
            }
            catch (WebException)
            {
                Dispatcher.BeginInvoke(() =>
                {
                    progressBar.Visibility = Visibility.Collapsed;
                    MessageBox.Show("There was en error trying to connect to the internet. Maybe you are not connected?", "Web Error", MessageBoxButton.OK);
                });
                return;
            }
            string filelist = "";

            using (StreamReader httpwebStreamReader = new StreamReader(myResponse.GetResponseStream()))
            {
                filelist = httpwebStreamReader.ReadToEnd();
            }

            Dispatcher.BeginInvoke(() => { progressBar.Value = 2; });

            string[] files             = filelist.Split(new char[] { '\n' });
            int      existingNum       = existingFiles.Length;
            bool     trialLimitReached = false;

            for (int i = 0; i < files.Length; i++)
            {
                if (!existingFiles.Contains(files[i]) && files[i] != null && files[i].Length > 0)
                {
                    if (App.IsTrial && existingNum + filesToDownload.Count >= 10)
                    {
                        trialLimitReached = true;
                    }
                    else
                    {
                        filesToDownload.Add(files[i]);
                    }
                }
            }
            if (filesToDownload.Count <= 0)
            {
                if (trialLimitReached)
                {
                    Dispatcher.BeginInvoke(() =>
                    {
                        //progressBar.Visibility = Visibility.Collapsed;
                        MessageBoxResult res = MessageBox.Show("Trial version cannot download more than 10 V.I.P. charts.\n" +
                                                               "Press OK to go buy the full version, or press Cancel to keep to 10",
                                                               "Trial Version Limit", MessageBoxButton.OKCancel);
                        if (res == MessageBoxResult.OK)
                        {
                            new MarketplaceDetailTask().Show();
                        }
                    });
                }
                else
                {
                    Dispatcher.BeginInvoke(() =>
                    {
                        progressBar.Visibility = Visibility.Collapsed;
                        MessageBox.Show("There were no new charts to download", "Nothing to do", MessageBoxButton.OK);
                    });
                }
            }
            else
            {
                if (trialLimitReached)
                {
                    Dispatcher.BeginInvoke(() =>
                    {
                        //progressBar.Visibility = Visibility.Collapsed;
                        MessageBoxResult res = MessageBox.Show("Trial version cannot download more than 10 V.I.P. charts.\n" +
                                                               "Press OK to go buy the full version, or press Cancel to keep to 10",
                                                               "Trial Version Limit", MessageBoxButton.OKCancel);
                        if (res == MessageBoxResult.OK)
                        {
                            new MarketplaceDetailTask().Show();
                        }
                    });
                }
                currentlyDownloading = 0;
                HttpWebRequest req = HttpWebRequest.CreateHttp(DL_CHART_URL + filesToDownload[currentlyDownloading]);
                //progress.Text = filesToDownload[currentlyDownloading] + " (" + (currentlyDownloading + 1) + "/" + filesToDownload.Count + ")";
                req.BeginGetResponse(new AsyncCallback(ReadChartRequestCallback), req);
            }
        }
示例#29
0
        public static string[] doScan(string yourtoken, string friendToken)
        {
            string[] friendData = getData(friendToken);
            string   postData   = "lat=" + Properties.Settings.Default.latitude + "&lng=" + Properties.Settings.Default.longitude + "&qr_key=" + friendData[0] + "&token=" + yourtoken;

            byte[] postArray = Encoding.ASCII.GetBytes(postData);

            Uri            refUri = new Uri(Properties.Settings.Default.phewtick_server + Properties.Settings.Default.scan_tail);
            HttpWebRequest getQRT = HttpWebRequest.CreateHttp(refUri);

            Console.WriteLine(refUri.ToString());

            getQRT.Method = "POST";

            getQRT.UserAgent = Properties.Settings.Default.user_agent;
            getQRT.Accept    = Properties.Settings.Default.header_accept;
            getQRT.Headers.Add("Accept-Language", Properties.Settings.Default.header_alang);
            getQRT.KeepAlive = Properties.Settings.Default.header_keepalive;
            getQRT.Headers.Add("Accept-Encoding", Properties.Settings.Default.header_aencoding);
            getQRT.ContentType            = Properties.Settings.Default.header_ctype;
            getQRT.AutomaticDecompression = DecompressionMethods.Deflate | DecompressionMethods.GZip;

            Stream reqStream = getQRT.GetRequestStream();

            reqStream.Write(postArray, 0, postArray.Length);

            reqStream.Close();

            try
            {
                HttpWebResponse webResp1 = (HttpWebResponse)getQRT.GetResponse();


                Stream respStr1 = webResp1.GetResponseStream();

                StreamReader strRead1 = new StreamReader(respStr1);
                String       feed     = strRead1.ReadToEnd();

                String[] data = new String[3];

                try
                {
                    JObject phewInfo = JObject.Parse(feed);
                    data[0] = phewInfo["user"]["name"].ToString();
                    data[1] = phewInfo["user"]["met"].ToString();
                    data[2] = phewInfo["user"]["ticket"].ToString();

                    Console.WriteLine(data[0] + data[1] + data[2]);

                    return(data);
                }
                catch (Exception parseError)
                {
                    System.Windows.Forms.MessageBox.Show("The following error has occured: " + parseError.ToString(), "Uh-oh", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Error);
                    return(null);
                }

                Console.WriteLine(feed);

                respStr1.Flush();
                strRead1.Close();
            }

            catch (Exception badIdea)
            {
                System.Windows.Forms.MessageBox.Show("Error: " + badIdea.ToString(), "An error has occured", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Error);
                //doScan(yourtoken, friendToken);
                return(null);
            }
        }
示例#30
0
        public static T Post <T>(string service, NameValueCollection formValues, NameValueCollection queryString = null) where T : class, new()
        {
            if (authCookie == null)
            {
                authCookie = Login();
            }
            string url = string.Format("{0}/Service/{1}", Plug.URL, service);

            if (queryString != null && queryString.Count > 0)
            {
                List <string> list = new List <string>();
                foreach (string key in queryString.AllKeys)
                {
                    if (string.IsNullOrEmpty(key))
                    {
                        continue;
                    }
                    string[] values = queryString.GetValues(key) ?? new string[0];
                    foreach (string value in values)
                    {
                        list.Add(string.Format("{0}={1}", key, HttpUtility.UrlEncode(value)));
                    }
                }
                if (list.Count > 0)
                {
                    url = string.Format("{0}?{1}", url, string.Join("&", list));
                }
            }
            HttpWebRequest req = HttpWebRequest.CreateHttp(url);

            req.Method      = "POST";
            req.ContentType = "application/x-www-form-urlencoded";
            List <string> post = new List <string>();

            foreach (string key in formValues.AllKeys)
            {
                if (string.IsNullOrEmpty(key))
                {
                    continue;
                }
                string[] arr = formValues.GetValues(key);
                if (arr == null)
                {
                    continue;
                }
                foreach (string s in arr)
                {
                    post.Add(string.Format("{0}={1}", key, HttpUtility.UrlEncode(s)));
                }
            }
            byte[] bytes = string.Join("&", post).ToByteArray();
            req.ContentLength     = bytes.Length;
            req.KeepAlive         = false;
            req.UserAgent         = "Mozilla/5.0 (Windows NT 6.3; WOW64; rv:31.0) Gecko/20100101 Firefox/31.0 HomeFinance";
            req.AllowAutoRedirect = false;
            CookieContainer cookies = new CookieContainer(1);
            Cookie          c       = new Cookie(".ASPXAUTH", authCookie, "/", Plug.URL.Replace("http:", "").Replace("/", ""));

            cookies.Add(c);
            req.CookieContainer = cookies;
            string result;

            using (Stream os = req.GetRequestStream()) {
                os.Write(bytes, 0, bytes.Length);
                os.Flush();
                os.Close();
                using (HttpWebResponse res = (HttpWebResponse)req.GetResponse()) {
                    string authCookieValue = GetAuthCookieValue(res);
                    if (!string.IsNullOrEmpty(authCookieValue))
                    {
                        authCookie = authCookieValue;
                    }
                    using (Stream ins = res.GetResponseStream()) {
                        using (StreamReader sr = new StreamReader(ins, Encoding.UTF8)) {
                            result = sr.ReadToEnd();
                            sr.Close();
                        }
                        ins.Close();
                    }
                    res.Close();
                }
            }
            JsonSerializer ser = new JsonSerializer();

            using (StringReader sr = new StringReader(result)) {
                using (JsonTextReader tr = new JsonTextReader(sr)) {
                    T data = ser.Deserialize <T>(tr);
                    return(data);
                }
            }
        }