Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Post a file to remote server using HTTPWEBREQUEST

Post a file to remote server using HTTPWEBREQUEST

Scheduled Pinned Locked Moved C#
sysadmin
7 Posts 3 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • V Offline
    V Offline
    vanikanc
    wrote on last edited by
    #1

    Hello, Below is a piece of code, where in I am trying to post a file from my pc to remote development server. For some reason it does not seem to be working. I don't see the file on the remote server.

        private void button2\_Click(object sender, EventArgs e)
        {
            FileStream filetoupload = new FileStream("C:\\\\projects\\\\testfile.csv", FileMode.Open);
            Uri address = new Uri("http://devsrvr/hints/");
    
            HttpWebRequest webreq = HttpWebRequest.Create(address) as HttpWebRequest;
    
            // set type to post
            webreq.Method = "POST";
            webreq.ContentType = "application/x-www-form-urlencoded";
            webreq.Timeout = 15 \* 1000;  
            // data we want to send, which is pretty much a file
                      
            byte\[\] bytedata = UTF8Encoding.UTF8.GetBytes(filetoupload.ToString());
            webreq.ContentLength = bytedata.Length;
    
            try
            {
                using (Stream poststream = webreq.GetRequestStream())
                {
                    poststream.Write(bytedata, 0, bytedata.Length);
                }
                MessageBox.Show("done!!");
            }
            catch (Exception ex)
            {
                ex.InnerException.ToString();
                ex.Source.ToString();
                ex.Message.ToString();
            }
        }
    
    L P 2 Replies Last reply
    0
    • V vanikanc

      Hello, Below is a piece of code, where in I am trying to post a file from my pc to remote development server. For some reason it does not seem to be working. I don't see the file on the remote server.

          private void button2\_Click(object sender, EventArgs e)
          {
              FileStream filetoupload = new FileStream("C:\\\\projects\\\\testfile.csv", FileMode.Open);
              Uri address = new Uri("http://devsrvr/hints/");
      
              HttpWebRequest webreq = HttpWebRequest.Create(address) as HttpWebRequest;
      
              // set type to post
              webreq.Method = "POST";
              webreq.ContentType = "application/x-www-form-urlencoded";
              webreq.Timeout = 15 \* 1000;  
              // data we want to send, which is pretty much a file
                        
              byte\[\] bytedata = UTF8Encoding.UTF8.GetBytes(filetoupload.ToString());
              webreq.ContentLength = bytedata.Length;
      
              try
              {
                  using (Stream poststream = webreq.GetRequestStream())
                  {
                      poststream.Write(bytedata, 0, bytedata.Length);
                  }
                  MessageBox.Show("done!!");
              }
              catch (Exception ex)
              {
                  ex.InnerException.ToString();
                  ex.Source.ToString();
                  ex.Message.ToString();
              }
          }
      
      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      Change your catch to something like below and try again;

              catch (Exception ex)
              {
                  MessageBox.Show(ex.ToString());
              }
      

      Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^]

      V 1 Reply Last reply
      0
      • L Lost User

        Change your catch to something like below and try again;

                catch (Exception ex)
                {
                    MessageBox.Show(ex.ToString());
                }
        

        Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^]

        V Offline
        V Offline
        vanikanc
        wrote on last edited by
        #3

        It is not going into the catch part at all. There are no errors in the program.

        L 1 Reply Last reply
        0
        • V vanikanc

          It is not going into the catch part at all. There are no errors in the program.

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          vanikanc wrote:

          There are no errors in the program.

          There is, even though it doesn't throw an exception. Consider below line;

          byte[] bytedata = UTF8Encoding.UTF8.GetBytes(filetoupload.ToString());

          "filetoupload.ToString()" would result in "System.IO.FileStream". And that'd be passed to the "GetBytes"-function. Try something like below;

          byte[] bytedata = File.ReadAllBytes("C:\\projects\\testfile.csv");

          Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^]

          V 1 Reply Last reply
          0
          • V vanikanc

            Hello, Below is a piece of code, where in I am trying to post a file from my pc to remote development server. For some reason it does not seem to be working. I don't see the file on the remote server.

                private void button2\_Click(object sender, EventArgs e)
                {
                    FileStream filetoupload = new FileStream("C:\\\\projects\\\\testfile.csv", FileMode.Open);
                    Uri address = new Uri("http://devsrvr/hints/");
            
                    HttpWebRequest webreq = HttpWebRequest.Create(address) as HttpWebRequest;
            
                    // set type to post
                    webreq.Method = "POST";
                    webreq.ContentType = "application/x-www-form-urlencoded";
                    webreq.Timeout = 15 \* 1000;  
                    // data we want to send, which is pretty much a file
                              
                    byte\[\] bytedata = UTF8Encoding.UTF8.GetBytes(filetoupload.ToString());
                    webreq.ContentLength = bytedata.Length;
            
                    try
                    {
                        using (Stream poststream = webreq.GetRequestStream())
                        {
                            poststream.Write(bytedata, 0, bytedata.Length);
                        }
                        MessageBox.Show("done!!");
                    }
                    catch (Exception ex)
                    {
                        ex.InnerException.ToString();
                        ex.Source.ToString();
                        ex.Message.ToString();
                    }
                }
            
            P Offline
            P Offline
            Pete OHanlon
            wrote on last edited by
            #5

            I'm not surprised it's not working. As a small point, could you please post code that stands a chance of compiling. There isn't a HttpWebRequest.Create - it's WebRequest.Create. Your problem here is that you are just reading the data into a stream, which you then dispose of. What do you think the stream at the server end is going to do? You actually need something there to take that stream and write it out to a file. That's the part you're missing here.

            I was brought up to respect my elders. I don't respect many people nowadays.
            CodeStash - Online Snippet Management | My blog | MoXAML PowerToys | Mole 2010 - debugging made easier

            V 1 Reply Last reply
            0
            • P Pete OHanlon

              I'm not surprised it's not working. As a small point, could you please post code that stands a chance of compiling. There isn't a HttpWebRequest.Create - it's WebRequest.Create. Your problem here is that you are just reading the data into a stream, which you then dispose of. What do you think the stream at the server end is going to do? You actually need something there to take that stream and write it out to a file. That's the part you're missing here.

              I was brought up to respect my elders. I don't respect many people nowadays.
              CodeStash - Online Snippet Management | My blog | MoXAML PowerToys | Mole 2010 - debugging made easier

              V Offline
              V Offline
              vanikanc
              wrote on last edited by
              #6

              Actually, using HttpWebRequest.Create I was able to download a file from remote server to a specified location. Now, I want to be able to upload a file to a remote server using

              HttpWebRequest.Create(

              . The code works using WebClient class, to upload and download files, but there is not much error trapping that could be coded for. On research the better option was to use

              HttpWebRequest

              . Having an issue trying to post a file

              1 Reply Last reply
              0
              • L Lost User

                vanikanc wrote:

                There are no errors in the program.

                There is, even though it doesn't throw an exception. Consider below line;

                byte[] bytedata = UTF8Encoding.UTF8.GetBytes(filetoupload.ToString());

                "filetoupload.ToString()" would result in "System.IO.FileStream". And that'd be passed to the "GetBytes"-function. Try something like below;

                byte[] bytedata = File.ReadAllBytes("C:\\projects\\testfile.csv");

                Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^]

                V Offline
                V Offline
                vanikanc
                wrote on last edited by
                #7

                I tried what you suggested, that did not work. The program was not responding. Thank you for suggesting. I will continue to dig deeper.

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups