public void PrepareBody() { if (Body != null) { Body.Dispose(); } Body = new HttpBody(this); }
void ParseLineMultipart(byte[] lineBytes) { string line = currentMessage.ContentTransferEncoding.GetString(lineBytes, 0, lineBytes.Length - 2); if (line == currentMessage.GetBoundaryStart()) { if (currentBodyMultipart != null) { AddCurrentBodyToRequest(lineBytes.Length + 2); } currentBodyMultipart = currentMessage.CreateBody(); currentBodyMultipart.state = 0; } else if (line == currentMessage.GetBoundaryEnd()) { if (currentBodyMultipart != null) { AddCurrentBodyToRequest(lineBytes.Length + 2); } // currentBodyMultipart.streamContent.Se // currentMessage.Body.IsCompleted = true; currentMessage.IsCompleted = true; firstField = true; pendingHead = true; } else if (line == "" && currentBodyMultipart.state == 0) { currentBodyMultipart.state = 1; } else if (line != "" && currentBodyMultipart.state == 0) { HttpField field = new HttpField(); field.Parse(line); currentBodyMultipart.Fields.Add(field.Name, field); } }
void AddCurrentBodyToRequest(int totalBytesToCut) { currentBodyMultipart.state = 2; currentBodyMultipart.ContentStream.SetLength(currentBodyMultipart.ContentStream.Length - totalBytesToCut); currentMessage.BodyParts.Add(currentBodyMultipart); if (currentBodyMultipart.IsFile) { currentMessage.Files.Add(currentBodyMultipart.Fields[HttpConst.ContentDisposition].Parameters["Name"].Value, currentBodyMultipart.File); } else { currentMessage.Form.Add(currentBodyMultipart.Fields[0].Parameters["Name"].Value, currentBodyMultipart.GetPlainText()); } currentBodyMultipart.Dispose(); currentBodyMultipart = null; }
/// <summary> /// Antes de enviar la petición se tiene que llamar a este método /// para serializar la cabecera. /// ** El body del mensaje lo serializa la clase HttpBody /// </summary> public void PrepareSend() { HeadStream = (Stream) new MemoryStream(); var headWriter = new StreamWriter(HeadStream, Encoding.ASCII); // serializamos los valores del formulario if (Form.Count > 0 && Body == null) { Method = MessageTypes.POST; Body = new HttpBody(this); int x = 0; for (x = 0; x < Form.Count; x++) { var strVal = HttpUtility.UrlEncode(Form.Get(x)); Body.AddText("{0}={1}&", Form.GetKey(x), strVal); } Body.ContentType = HttpConst.FormUrlEncoded; } headWriter.WriteLine("{0} {1} {2}", Method, Url, HttpConst.HttpVersion); headWriter.WriteLine("{0}: {1}", HttpConst.UserAgent, WebConfig.ServerVersion); headWriter.WriteLine("{0}: {1}", HttpConst.Host, Host); if (Accept.Count > 0) { headWriter.WriteLine("{0}: {1}", HttpConst.Accept, Accept.Join(",")); } if (AcceptCharset.Count > 0) { headWriter.WriteLine("{0}: {1}", HttpConst.AcceptCharset, AcceptCharset.Join(",")); } if (AcceptLanguage.Count > 0) { headWriter.WriteLine("{0}: {1}", HttpConst.AcceptLanguage, AcceptLanguage.Join(",")); } if (AcceptEncoding.Count > 0) { headWriter.WriteLine("{0}: {1}", HttpConst.AcceptEncoding, AcceptEncoding.Join(",")); } if (Body != null && Body.HasContent) { if (Body.ContentEncoding != ContentEncodings.NORMAL) { string cenc = ""; switch (Body.ContentEncoding) { case ContentEncodings.GZIP: cenc = "gzip"; break; case ContentEncodings.DEFLATE: cenc = "deflate"; break; } headWriter.WriteLine("Content-Encoding: {0}", cenc); } if (Body.ContentEncoding == ContentEncodings.GZIP) { MemoryStream body = new MemoryStream(); GZip.Compress(Body.ContentStream, body); Body.ContentStream = body; } headWriter.Write("Content-Type: {0}", Body.ContentType); if (Body.ContentType.StartsWith("text") && Body.CharSet.Length > 0) { headWriter.Write("; charset={0}", Body.CharSet); } headWriter.WriteLine(); if (Body.ContentDisposition != null && Body.ContentDisposition.Length > 0) { headWriter.WriteLine("Content-Disposition: {0}", Body.ContentDisposition); } headWriter.WriteLine("Content-Length: {0}", Body.Length); } else { headWriter.WriteLine("Content-Length: {0}", 0); } foreach (object item in Head.Fields.Values) { var field = (HttpField)item; switch (field.Name) { default: headWriter.WriteLine("{0}: {1}", field.Name, field.Value); break; } } headWriter.WriteLine(); headWriter.Flush(); ConsoleWritePlainText(); }
public HttpBody CreateBody() { Body = new HttpBody(this); return(Body); }