예제 #1
0
        public static async Task <string> CookiedPostForm(string url, string referrer, KeyValuePair <string, string>[] keyValues)
        {
            //HttpResponseMessage response = null;
            try
            {
                HttpRequestMessage req = new HttpRequestMessage(HttpMethod.Post, new Uri(url));

                HttpFormUrlEncodedContent cont = new HttpFormUrlEncodedContent(keyValues);

                req.Content = cont;

                req.Headers.Referer = new Uri(referrer);

                HttpResponseMessage hc = await client.SendRequestAsync(req);

                //await ShowMessageDialog("响应", hc.Content.ToString());

                req.Dispose();

                return(hc.Content.ToString());
            }
            catch (COMException e)
            {
                return("Cannot get response");
            }
        }
예제 #2
0
        public static async Task <bool> CookiedGetReward(string queryId)
        {
            if (isCookieValid == false)
            {
                await ShowMessageDialog("领取功能需要设置Cookie", "领取操作不仅需要悬赏令ID, 还需要您的BDUSS. 请先设置Cookie.");

                return(false);
            }
            HttpResponseMessage response = null;

            try
            {
                string url = "https://jingyan.baidu.com/patchapi/claimQuery?queryId="
                             + queryId + "&token=" + newMainBdStoken + "&timestamp=" + newMainBdstt;
                HttpRequestMessage req = new HttpRequestMessage(HttpMethod.Post, new Uri(url));
                req.Headers.Referer = new Uri("https://jingyan.baidu.com/patch");
                // ----- not working -----
                //KeyValuePair<string, string>[] urlParams = {
                //    new KeyValuePair<string, string>("queryId", queryId),
                //    new KeyValuePair<string, string>("token", newMainBdStoken),
                //    new KeyValuePair<string, string>("timestamp", newMainBdstt)
                //};
                //req.Content = new FormUrlEncodedContent(urlParams) as IHttpContent;
                response = await client.SendRequestAsync(req);

                string respstr      = response.Content.ToString().Replace(" ", "");
                bool   isGetSucceed = false;
                if (respstr.IndexOf("\"errno\":0") >= 0)
                {
                    respstr      = "成功领取。请在 个人中心->悬赏经验->已领取 查看。";
                    isGetSucceed = true;
                }
                else if (respstr.IndexOf("\"errno\":302") >= 0)
                {
                    respstr = "你可能已经领取过经验。领取不成功(302)。";
                    //isGetSucceed = true;
                }
                else if (respstr.IndexOf("\"errno\":2") >= 0)
                {
                    respstr = "身份验证失败,如果确定Cookie设定有效,可告知开发者 wang1223989563。错误码:2";
                }
                else
                {
                    respstr = "未知错误类型 (非302或2错误。可告知开发者 wang1223989563) \n错误信息: " + respstr;
                }
                await ShowMessageDialog("领取结果", respstr);

                req.Dispose();
                return(isGetSucceed);
            }
            catch (COMException e)
            {
                await ShowDetailedError("领取未成功", e);

                return(false);
            }
        }
예제 #3
0
        private static async Task <BitmapImage> GetMainSubStep_CookielessGetPic(string picUrl)
        {
            Uri myUri = new Uri(picUrl);
            HttpRequestMessage req = new HttpRequestMessage(HttpMethod.Get, myUri);

            req.Headers.Host = new HostName(myUri.Host);
            req.Headers.Cookie.Clear();
            HttpResponseMessage response = await client.SendRequestAsync(req);

            if (response == null)
            {
                return(null);
            }
            //HttpResponseMessage response
            if (response.StatusCode != HttpStatusCode.Ok)
            {
                return(null);
            }
            IHttpContent icont = response.Content;

            IBuffer buffer = await icont.ReadAsBufferAsync();


            IRandomAccessStream irStream = new InMemoryRandomAccessStream();
            BitmapImage         img      = new BitmapImage();

            await irStream.WriteAsync(buffer);

            irStream.Seek(0);

            await img.SetSourceAsync(irStream);

            irStream.Dispose();
            icont.Dispose();
            response.Dispose();
            req.Dispose();

            return(img);
        }
예제 #4
0
        //prepared private
        public static async Task <string> CookiedGetUrl(string url, string referrer)
        {
            HttpResponseMessage response = null;

            try
            {
                HttpRequestMessage req = new HttpRequestMessage(HttpMethod.Get, new Uri(url));
                req.Headers.Referer = new Uri(referrer);
                response            = await client.SendRequestAsync(req);

                req.Dispose();
            }
            catch (COMException e)
            {
                if ((uint)e.HResult == 0x80072f76)
                {
                    await ShowMessageDialog("可能是被验证码阻挡。",
                                            "错误代码:" + String.Format("{0:x8}", e.HResult) + "\n错误类型:" + e.GetType() + "\n错误信息:" +
                                            e.Message + "\n"
                                            + "如果是验证码问题,请不要立即点确定。请先打开浏览器,输入验证码。");

                    await ShowMessageDialog("提示", "如果已经输入验证码,可以确认继续;\n如果发现Cookie已失效,继续并稍后以重新设置Cookie");
                }
                try
                {
                    HttpRequestMessage req = new HttpRequestMessage(HttpMethod.Get, new Uri(url));
                    req.Headers.Referer = new Uri(referrer);
                    response            = await client.SendRequestAsync(req);

                    req.Dispose();
                }
                catch (COMException e2)
                {
                    if ((uint)e2.HResult == 0x80072f76)
                    {
                        await ShowMessageDialog("错误未解决。",
                                                "错误类型:" + e2.GetType() + "\n错误信息:" + e2.Message + "\n持续网络故障。为避免故障扩大,程序将结束。可检查网络/jingyan.baidu.com,稍后再启动。");

                        Application.Current.Exit();
                    }
                    if ((uint)e2.HResult == 0x80072efd)
                    {
                        await ShowMessageDialog("错误未解决。80072efd,系统网络故障",
                                                "错误类型:" + e2.GetType() + "\n错误信息:" + e2.Message + "\n请百度80072efd寻找解决办法。该故障和系统网络设置有关。可能是被国产安全软件乱优化造成。\n持续网络故障。为避免故障扩大,程序将结束。可检查网络,稍后再启动。");

                        Application.Current.Exit();
                    }
                    client.Dispose();
                    await ShowMessageDialog("未知故障。可能是网络问题", "错误类型:" + e2.GetType() + "\n错误信息:" + e2.Message);

                    throw new COMException(e2.Message);
                }
            }

            if (response == null)
            {
                return("RESPONSE NULL");
            }

            if (response.StatusCode != HttpStatusCode.Ok)
            {
                return(response.StatusCode.ToString());
            }
            IHttpContent icont   = response.Content;
            IInputStream istream = await icont.ReadAsInputStreamAsync();

            StreamReader reader  = new StreamReader(istream.AsStreamForRead(), Encoding.UTF8);
            string       content = reader.ReadToEnd();

            reader.Dispose();
            istream.Dispose();
            icont.Dispose();
            response.Dispose();

            return(content);
        }