Exemplo n.º 1
0
 public void AddToRequest(System.Net.HttpWebRequest request)
 {
     byte[] data = GetBytes();
     using (var stream = request.GetRequestStream())
     {
         stream.Write(data, 0, data.Length);
     }
 }
Exemplo n.º 2
0
        public override void PrepareTaskRequest(System.Net.HttpWebRequest request)
        {
            request.Method = "POST";
            request.Proxy.Credentials = System.Net.CredentialCache.DefaultCredentials;
            request.ContentType = "application/x-www-form-urlencoded";

            request.Referer = RequestBaseUrl;
            byte[] bytes = Encoding.ASCII.GetBytes(BuildRequestData(this.requestRegisterData, this.person));
            request.Timeout = 1200000;
            request.ContentLength = bytes.Length;

            var stream = request.GetRequestStream();
            stream.Write(bytes, 0, bytes.Length);
        }
 private static void CopyRequestStream(HttpContext context, System.Net.HttpWebRequest req)
 {
     // Copy request content
     if (req.ContentLength > 0)
     {
         using (var reqStream = req.GetRequestStream())
         {
             var reqBytes = new byte[context.Request.InputStream.Length];
             context.Request.InputStream.Read(reqBytes, 0, reqBytes.Length);
             reqStream.Write(reqBytes, 0, reqBytes.Length);
         }
     }
 }
Exemplo n.º 4
0
 /// <summary>
 /// Sends an assembled <c>request</c>.
 /// </summary>
 /// <returns>
 /// Any interesting properties and, depending on whether <c>output</c> is set, an attached stream.
 /// If <c>output</c> is provided, it is used instead. the or writing the result to that
 /// provided, seeking back to 0 in either case.
 /// </returns>
 /// <param name='request'>
 /// Request.
 /// </param>
 /// <param name='output'>
 /// Output.
 /// </param>
 /// <param name='timeout'>
 /// Timeout.
 /// </param>
 /// <exception cref="Exceptions.ProtocolError">
 /// This, or one of this children, is thrown if a problem occurs while communicating with the server.
 /// </exception>
 /// <exception cref="Exceptions.URLError">
 /// Thrown if the server cannot be contacted.
 /// </exception>
 internal static Response SendRequest(System.Net.HttpWebRequest request, System.IO.Stream output=null, float timeout=10.0f)
 {
     try{
         request.GetRequestStream().Close();
         Response r = new Response();
         using(System.Net.HttpWebResponse response = (System.Net.HttpWebResponse)request.GetResponse()){
             using(System.IO.Stream stream = response.GetResponseStream()){
                 r.Properties = new System.Collections.Generic.Dictionary<string, object>();
                 string applied_compression = response.Headers.Get(Communication.HEADER_APPLIED_COMPRESSION);
                 if(applied_compression != null){
                     r.Properties.Add(Communication.PROPERTY_APPLIED_COMPRESSION, Compression.ResolveCompressionFormat(applied_compression));
                 }else{
                     r.Properties.Add(Communication.PROPERTY_APPLIED_COMPRESSION, null);
                 }
                 r.Properties.Add(Communication.PROPERTY_CONTENT_TYPE, response.ContentType);
                 if(output != null){
                     stream.CopyTo(output);
                     r.Properties.Add(Communication.PROPERTY_CONTENT_LENGTH, output.Length);
                     output.Seek(0, System.IO.SeekOrigin.Begin);
                     r.Data = output;
                 }else{
                     r.Data = new System.IO.MemoryStream();
                     stream.CopyTo(r.Data);
                     r.Data.Seek(0, System.IO.SeekOrigin.Begin);
                     r.Properties.Add(Communication.PROPERTY_CONTENT_LENGTH, response.ContentLength);
                 }
             }
         }
         return r;
     }catch(System.Net.WebException e){
         if(e.Status == System.Net.WebExceptionStatus.ProtocolError && e.Response != null){
             System.Net.HttpWebResponse response = (System.Net.HttpWebResponse)e.Response;
             if(response.StatusCode == System.Net.HttpStatusCode.Forbidden){
                 throw new Exceptions.NotAuthorisedError("The requested operation could not be performed because an invalid key was provided");
             }else if(response.StatusCode == System.Net.HttpStatusCode.NotFound){
                 throw new Exceptions.NotFoundError("The requested resource was not retrievable; it may have been deleted or not yet defined");
             }else if(response.StatusCode == System.Net.HttpStatusCode.Conflict){
                 throw new Exceptions.InvalidRecordError("The uploaded request is structurally flawed and cannot be processed");
             }else if(response.StatusCode == System.Net.HttpStatusCode.PreconditionFailed){
                 throw new Exceptions.InvalidHeadersError("One or more of the headers supplied (likely Content-Length) was rejected by the server");
             }else if(response.StatusCode == System.Net.HttpStatusCode.ServiceUnavailable){
                 throw new Exceptions.TemporaryFailureError("The server was unable to process the request");
             }
             throw new Exceptions.ProtocolError("Unable to send message; code: " + response.StatusCode, e);
         }else if(e.Status == System.Net.WebExceptionStatus.ConnectFailure || e.Status == System.Net.WebExceptionStatus.NameResolutionFailure){
             throw new Exceptions.URLError("Unable to send message: " + e.Message, e);
         }
         throw;
     }
 }
Exemplo n.º 5
0
 public RecurringBillInformation PerformRecurringBill(string json, System.Net.HttpWebRequest request)
 {
     RecurringBillInformation responseObject = new RecurringBillInformation();
     using (var streamWriter = new StreamWriter(request.GetRequestStream()))
     {
         streamWriter.Write(json);
         streamWriter.Flush();
         streamWriter.Close();
     }
     var result = doPost(request, _url);
     responseObject = JsonConvert.DeserializeObject<RecurringBillInformation>(result);
     responseObject.rowData = result;
     return responseObject;
 }
Exemplo n.º 6
0
 /// <summary>
 /// Writes the payload for the POST operation to the outbound
 /// HttpWebRequest.
 /// </summary>
 /// <param name="target">The target.</param>
 /// <returns></returns>
 private bool preRequestHandler(System.Net.HttpWebRequest target)
 {
     string payload = AssemblePostBackPayload();
     byte[] buff = Encoding.ASCII.GetBytes(payload.ToCharArray());
     target.ContentLength = buff.Length;
     target.ContentType = "application/x-www-form-urlencoded";
     System.IO.Stream reqStream = target.GetRequestStream();
     reqStream.Write(buff, 0, buff.Length);
     return true;
 }