locked
Set request body data and File in multipart form data POST Request RRS feed

  • Question

  • Hey There ....
    I am trying set both request body data and FileStream in POST HTTP request.
    But only the file is being added in request body , the POST BODY PARAMETERS are not added

    string fileUrl=@"E:\code.txt"; string url = "http://ptsv2.com/t/PostReq/post"; // Create a http request to the server endpoint that will pick up the // file and file description. HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); string boundaryString = "------------"+DateTime.Now.Ticks.ToString("x"); // Set the http request header \\ request.Method = WebRequestMethods.Http.Post; request.ContentType = "multipart/form-data;boundary=" + boundaryString; request.KeepAlive = true; request.Credentials =CredentialCache.DefaultCredentials; // Use a MemoryStream to form the post data request, // so that we can get the content-length attribute. MemoryStream postDataStream = new MemoryStream(); StreamWriter postDataWriter = new StreamWriter(postDataStream); // Include the file in the post data postDataWriter.Write("\r\n--" + boundaryString + "\r\n"); postDataWriter.Write("Content-Dis-data;"+ "name=\"{0}\";"+ "filename=\"{1}\""+"\r\nContent-Type: {2}\r\n\r\n", "myFile", Path.GetFileName(fileUrl), Path.GetExtension(fileUrl)); postDataWriter.Flush(); // Read the file FileStream fileStream = new FileStream(fileUrl, FileMode.Open, FileAccess.Read,FileShare.ReadWrite); byte[] buffer = new byte[1024]; int bytesRead = 0; while ((bytesRead = fileStream.Read(buffer, 0, buffer.Length)) != 0) { postDataStream.Write(buffer, 0, bytesRead); } fileStream.Close(); postDataWriter.Write("\r\n--" + boundaryString + "--\r\n"); postDataWriter.Flush(); // Dump the post data from the memory stream to the request stream using (Stream stream = request.GetRequestStream()) {

    string data="username=hello&pwd=test123";

    byte[] postData = Encoding.ASCII.GetBytes(data);

    stream.write(postData,0,postData.Length);

    postDataStream.WriteTo(stream); } postDataStream.Close(); WebResponse response = request.GetResponse(); StreamReader responseReader = new StreamReader(response.GetResponseStream()); string replyFromServer = responseReader.ReadToEnd(); MessageBox.Show(replyFromServer);

    Everytime I run the solution only the file is uploaded to server , not the post request body ,
    Can someone help me to figure this problem ??

    Tuesday, October 9, 2018 6:14 AM

Answers

  • I'd try asking for help over here.

    https://forums.asp.net/

     

     



    Regards, Dave Patrick ....
    Microsoft Certified Professional
    Microsoft MVP [Windows Server] Datacenter Management

    Disclaimer: This posting is provided "AS IS" with no warranties or guarantees, and confers no rights.

    Wednesday, October 10, 2018 12:48 PM

All replies

  • Hi hariharan,

    This forum(Microsoft Office for Developers) is for issues related to Microsoft Office and your issue is related to Web development, I will move your thread to Where is the Forum For…? so someone could tell you where to go ask your question.
    Thanks for understanding.

    Best Regards,
    Bruce 


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread.



    Wednesday, October 10, 2018 9:24 AM
  • Wednesday, October 10, 2018 9:37 AM
  • I'd try asking for help over here.

    https://forums.asp.net/

     

     



    Regards, Dave Patrick ....
    Microsoft Certified Professional
    Microsoft MVP [Windows Server] Datacenter Management

    Disclaimer: This posting is provided "AS IS" with no warranties or guarantees, and confers no rights.

    Wednesday, October 10, 2018 12:48 PM