예제 #1
0
        private void WriteKeys(Stream requestStream, CaptchaConfig config, byte[] boundarybytes)
        {
            string formdataTemplate = "Content-Disposition: form-data; name=\"{0}\"\r\n\r\n{1}";

            NameValueCollection nvc = new NameValueCollection {
                { "key", _apiKey }
            };

            if (config != null)
            {
                nvc.Add(config.GetParameters());
            }

            foreach (string key in nvc.Keys)
            {
                requestStream.Write(boundarybytes, 0, boundarybytes.Length);
                string formitem      = string.Format(formdataTemplate, key, nvc[key]);
                byte[] formitembytes = Encoding.UTF8.GetBytes(formitem);
                requestStream.Write(formitembytes, 0, formitembytes.Length);
            }
            requestStream.Write(boundarybytes, 0, boundarybytes.Length);
        }
예제 #2
0
        /// <summary>
        /// Загрузить файл капчи
        /// </summary>
        /// <param name="fileName">уть к файлу с капчей</param>
        /// <param name="config">Параметры</param>
        /// <returns></returns>
        public string UploadCaptchaFile(string fileName, CaptchaConfig config)
        {
            string url = string.Format("{0}/in.php", host);
            NameValueCollection nvc = new NameValueCollection();

            nvc.Add("key", api_key);
            if (config != null)
            {
                nvc.Add(config.GetParameters());
            }

            string boundary = "---------------------------" + DateTime.Now.Ticks.ToString("x");

            byte[] boundarybytes = System.Text.Encoding.ASCII.GetBytes("\r\n--" + boundary + "\r\n");

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

            request.ContentType = "multipart/form-data; boundary=" + boundary;
            request.Method      = "POST";
            request.KeepAlive   = true;
            request.Credentials = System.Net.CredentialCache.DefaultCredentials;

            Stream requestStream = request.GetRequestStream();

            string formdataTemplate = "Content-Disposition: form-data; name=\"{0}\"\r\n\r\n{1}";

            foreach (string key in nvc.Keys)
            {
                requestStream.Write(boundarybytes, 0, boundarybytes.Length);
                string formitem      = string.Format(formdataTemplate, key, nvc[key]);
                byte[] formitembytes = System.Text.Encoding.UTF8.GetBytes(formitem);
                requestStream.Write(formitembytes, 0, formitembytes.Length);
            }
            requestStream.Write(boundarybytes, 0, boundarybytes.Length);

            string headerTemplate = "Content-Disposition: form-data; name=\"{0}\"; filename=\"{1}\"\r\nContent-Type: {2}\r\n\r\n";
            string header         = string.Format(headerTemplate, "file", fileName, "image/jpeg");

            byte[] headerbytes = System.Text.Encoding.UTF8.GetBytes(header);
            requestStream.Write(headerbytes, 0, headerbytes.Length);

            FileStream fileStream = new FileStream(fileName, FileMode.Open, FileAccess.Read);

            byte[] buffer    = new byte[4096];
            int    bytesRead = 0;

            while ((bytesRead = fileStream.Read(buffer, 0, buffer.Length)) != 0)
            {
                requestStream.Write(buffer, 0, bytesRead);
            }
            fileStream.Close();

            byte[] trailer = System.Text.Encoding.ASCII.GetBytes("\r\n--" + boundary + "--\r\n");
            requestStream.Write(trailer, 0, trailer.Length);
            requestStream.Close();

            using (WebResponse response = request.GetResponse())
            {
                Stream       responseStream = response.GetResponseStream();
                StreamReader responseReader = new StreamReader(responseStream);
                return(ParseAnswer(responseReader.ReadToEnd()));
            }
        }