Announcement

Collapse
No announcement yet.

Hochladen von zwei XML Dokumenten mittels HttpWebRequest und Multipart Request

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Hochladen von zwei XML Dokumenten mittels HttpWebRequest und Multipart Request

    Hallo!

    Ich habe folgendes Problem:

    Für eine Anfrage an ein Framework muss ich einen MultipartRequest mit 2 Dateien senden.
    Die erste Datei ist ein XML Dokument dass ich als XDokument in dem Programm erstelle und das verschiedene Parameter für die Verarbeitung enthält.
    Die zweite Datei ist ein XML Dokument, das für die Berechnung der Antwort benötigt wird.

    Ich habe versucht, mir einen solchen MultipartRequest zusammenzubauen, nach Beispielen die ich im Internet gefunden habe. Leider habe ich kein Beispiel für das Senden von mehreren Dateien gefunden.

    Im Moment bekomme ich beim zweiten Durchlauf der Whileschleife beim schreiben der Datei aus dem GraphStream in den RequestStream eine Exception (System.Net.Sockets.SocketException wurde nicht behandelt.
    Message="Eine vorhandene Verbindung wurde vom Remotehost geschlossen")
    (ich markiere die Stelle noch im Code).

    Kann mir jemand sagen ob die Vorgehensweise so richtig ist und warum ich diese Fehlermeldung bekomme?

    Code:
    HttpWebRequest HttpRequest =
                            (HttpWebRequest)HttpWebRequest.Create(_RoutingFrameworkURL);
                    
                    string Boundary = "----------" + DateTime.Now.Ticks.ToString("x");
    
                    HttpRequest.ContentType = string.Format("multipart/form-data;boundary={0}", Boundary);
                    HttpRequest.Method = "POST";
    
                    HttpRequest.KeepAlive = true;
    
                    HttpRequest.Credentials = System.Net.CredentialCache.DefaultCredentials;
    
                    // Get the boundry in bytes
                    byte[] BoundaryBytes = System.Text.Encoding.ASCII.GetBytes("\r\n--" + Boundary + "\r\n");
    
                    // Get the header for the file upload
    
    
    
                    string HeaderTemplate = "Content-Disposition: form-data; name=\"{0}\";filename=\"{1}\"\r\n Content-Type: application/octet-stream\r\n\r\n";
    
                    // Add the first filename to the header
                    StringBuilder Header = new StringBuilder();
                    Header.Append(Boundary);
                    Header.Append(string.Format(HeaderTemplate, "xmlFile", _doc));
    
                    // Add the second filename to the header
                    Header.Append(Boundary);
                    Header.Append(string.Format(HeaderTemplate, "xmlGraphFile", _XmlPathGraph));
    
                    //convert the header to a byte array
                    byte[] Headerbytes = System.Text.Encoding.UTF8.GetBytes(Header.ToString());
    
                    //convert first document to byte array
                    System.Text.ASCIIEncoding enc = new System.Text.ASCIIEncoding();
                    byte[] docInByte = enc.GetBytes(_doc.ToString());
    
                    //open Filestream for Graphdocument
                    FileStream GraphStream = new FileStream(_XmlPathGraph, FileMode.Open, FileAccess.Read);
    
                    HttpRequest.ContentLength = docInByte.Length + (int)GraphStream.Length + Headerbytes.Length + BoundaryBytes.Length + 2;
    
                    //get output stream
                    Stream RequestStream = HttpRequest.GetRequestStream();
    
                    // Write the header including the filename.
                    RequestStream.Write(Headerbytes, 0, Headerbytes.Length);
    
                    //write the first document (requestdocument)
                    RequestStream.Write(docInByte, 0, docInByte.Length);
    
                    //write Boundary to separate the 2 documents??
                    RequestStream.Write(BoundaryBytes, 0, BoundaryBytes.Length);
    
                    // Use 4096 for the buffer
                    byte[] buffer = new Byte[checked((uint)Math.Min(4096, (int)GraphStream.Length))]; ;
    
                    int bytesRead = 0;
                    // Loop through whole file uploading parts in a stream.
                    while ((bytesRead = GraphStream.Read(buffer, 0, buffer.Length)) != 0)
                    {
                        RequestStream.Write(buffer, 0, bytesRead);
                        //RequestStream.Flush();
                    }
    
    
                    BoundaryBytes = System.Text.Encoding.ASCII.GetBytes("\r\n--" + Boundary + "--\r\n");
    
    
                    // Write out the trailing boundry
                    RequestStream.Write(BoundaryBytes, 0, BoundaryBytes.Length);
    
                    // Close the request and file stream
                    RequestStream.Close();
                    GraphStream.Close();
    
                    HttpWebResponse HttpResponse =
                        (HttpWebResponse)HttpRequest.GetResponse();
    
                    Stream ResponseStream = HttpResponse.GetResponseStream();
    
    
                    StreamReader ResponseReader = new StreamReader(ResponseStream);
    
                    string ResponseString = ResponseReader.ReadToEnd();
    
                    Cursor.Current = Cursors.Default;
                    HttpResponse.Close();
                    ResponseStream.Close();

  • #2
    http://entwickler-forum.de/showthread.php?t=53247
    Christian

    Comment

    Working...
    X