Пример #1
0
        //--------------------------------------------------------------------------------------------
        /// <summary>
        /// Post a form
        /// </summary>
        private static UploadStatus PostForm(string postUrl, string username, string password, string userAgent, string contentType, byte[] formData)
        {
            ServicePointManager.CertificatePolicy = new FormUploadCertPolicy();
               ServicePointManager.ServerCertificateValidationCallback = Validator;
               ServicePointManager.Expect100Continue = true;

               HttpWebRequest request = WebRequest.Create(postUrl) as HttpWebRequest;
               if (username != null && password != null)
               {
                    string usernamePassword = username + ":" + password;
                    CredentialCache mycache = new CredentialCache();
                    mycache.Add(new Uri(postUrl), "Basic", new NetworkCredential(username, password));
                    request.Credentials = mycache;
                    request.Headers.Add("Authorization", "Basic " + Convert.ToBase64String(new ASCIIEncoding().GetBytes(usernamePassword)));
               }
               if (request == null)
               {
                    throw new NullReferenceException("request is not a http request");
               }

               request.Method = "POST";
               request.ContentType = contentType;
               request.CookieContainer = new CookieContainer();
               request.UserAgent = userAgent;
               // We need to count how many bytes we're sending.
               request.ContentLength = formData.Length;

               using (Stream requestStream = request.GetRequestStream())
               {
                    // Push it out there
                    requestStream.Write(formData, 0, formData.Length);
                    requestStream.Close();
               }
               HttpWebResponse resp = (HttpWebResponse)request.GetResponse();
               StreamReader reader = new StreamReader(resp.GetResponseStream());
               string responseText = reader.ReadToEnd();
               UploadStatus status = new UploadStatus();
               status.StatusCode = resp.StatusCode;
               status.StatusDescription = resp.StatusDescription;
               status.ResponseText = responseText;
               return status;
        }