Example of HTTPS sftp file upload?

Last post 05-23-2008 8:16 by roundy72. 12 replies.
Page 1 of 1 (13 items)
Sort Posts: Previous Next
  • 03-03-2008 10:59

    Example of HTTPS sftp file upload?

    I'm looking for code on how to implement file upload to a HTTPS server  https://sftp.########.com/

     

    I've been successful using the opennetcf ftp lib for normal ftp to this site, but i need to do it with https.

     

    Right now I have:

    //for testing i accept all certificates...  

    System.Net.ServicePointManager.CertificatePolicy = new AcceptAllCertificatePolicy();


                    HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(uri);
                    httpWebRequest.Credentials = credentials;       
                    HttpWebResponse httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse();

     Beyond this, i'm unsure what to do..

     
    for FTP i would do:

    Stream httpRequestStream = httpWebRequest.GetRequestStream(); ///But this throws an error..

    responseReader = new StreamReader(httpRequestStream);
     

  • 03-04-2008 14:48 In reply to

    Re: Example of HTTPS sftp file upload?

    As I'm learning more.. it looks like i'd either like to do a POST or a PUT method to my web server.. which will have a php script to handle the post or put..

     I have managed a normal post, but some id tags like    http://site.com?userid=####&pass=#####  for example..  but what i'm looking for it how to send whole files.

     

    THanks,

    Scott
     

  • 03-11-2008 8:40 In reply to

    • Neil
    • OpenNETCF Staff
    • Top 10 Contributor
    • Joined on 07-30-2007
    • North Wales
    • Posts 1,152

    Re: Example of HTTPS sftp file upload?

    Try something like this:

    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);
    request.Credentials = credentials;       
    request.ContentType = "multipart/form-data";
    request.Method = "POST";
    request.KeepAlive = true;
    
    using(StreamWriter writer = new StreamWriter(request.GetRequestStream()))
    {
        using(FileStream fs = new FileStream(name_of_my_file, FileMode.Open, FileAccess.Read, FileShare.Read))
        {
            request.ContentLength = fs.Length;
    
            byte[] buffer = new byte[1024];
    
            int read = 0;
            while ( (read = fs.Read(buffer, 0, buffer.Length)) != 0 )
            {
                writer.Write(buffer, 0, read);
            }
        }
    }
    
    HttpWebResponse response = (HttpWebResponse)request.GetResponse();
    .
    .
    .

    There are plenty of examples of this on the web if you search Google.


  • 03-12-2008 15:30 In reply to

    Re: Example of HTTPS sftp file upload?

    Thanks. I found some stuff similar and have written the code.

     

    I can send without problem... and catch it on my Apache Server.

     

    Question: Is there a way to declare the name of this file, so that I can get it on the Apache Server?

     
    For instance, if this were HTML then we would do <... type=file, name=filename ...> in the code, is that possible with httpwebrequest?

     

    It's a shame that WebClient isn't an avalible class on .Net Compact Framework

     

     

    Thanks,
    Scott

  • 03-13-2008 5:45 In reply to

    • Neil
    • OpenNETCF Staff
    • Top 10 Contributor
    • Joined on 07-30-2007
    • North Wales
    • Posts 1,152

    Re: Example of HTTPS sftp file upload?

    You need to specify the Content-Disposition header if you also want to send the file name to the server. See below:

    string boundary = String.Format("----------{0}", DateTime.Now.Ticks.ToString("x"));
    string fieldName = "fileToUpload";
    string fileName = "name_of_my_file";
    string fileContentType = "application/octet-stream";
    
    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);
    request.Credentials = credentials;       
    request.ContentType = String.Format("multipart/form-data; boundary={0}", boundary);
    request.Method = "POST";
    request.KeepAlive = true;
    
    using(StreamWriter writer = new StreamWriter(request.GetRequestStream()))
    {
    	byte[] postHeader = Encoding.ASCII.GetBytes(String.Format("Content-Disposition: form-data; name=\"{0}\"; filename=\"{1}\" Content-Type: {2}\r\n\r\n",fieldName, fileName, fileContentType));
    
        byte[] boundaryBytes = Encoding.ASCII.GetBytes(String.Format("\r\n{0}\r\n", boundary));
    
        using(FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.Read))
        {
            request.ContentLength = postHeader.Length + fs.Length + boundaryBytes.Length;
    
            byte[] buffer = new byte[1024];
    
            int read = 0;
            while ( (read = fs.Read(buffer, 0, buffer.Length)) != 0 )
            {
                writer.Write(buffer, 0, read);
            }
        }
    
    	writer.Write(boundaryBytes, 0, boundaryBytes.Length);
    }
    
    HttpWebResponse response = (HttpWebResponse)request.GetResponse();
    .
    .
    .

  • 03-13-2008 11:26 In reply to

    Re: Example of HTTPS sftp file upload?

     So when i do this, I'm not seeing anything on the php side via a phpinfo call.

     It sees the Content Type with the added boundry, but doesn't even create a temp file...

     I'm not sure it likes the

    ; boundary={0}", boundary);  added to the ContentType.
     
    I used code similar to yours, and added in sending the postHeader before sending the file
    writer.Write(postHeader, 0, postHeader.Length); 


    php normally grabs any files and store it as a temp.. but it doesn't seem to even recognize a file was sent.

  • 03-14-2008 13:35 In reply to

    Re: Example of HTTPS sftp file upload?

    I have everything worked out now. Thanks. 

  • 04-14-2008 16:55 In reply to

    Re: Example of HTTPS sftp file upload?

    Neil, I was hoping I could ask you one last question on this topic. I was hoping to send some POST data along with my Files.. Seems like my Content Type choice is allowing me one or the other, but not both. Here is a snippit of the code, any help is greatly appreciated.  (multipart/form-data I get the Files correctly but no variables vs. application/x-www-form-urlencoded I get the POST variables correctly but no files)

     

                                string reqBuffer = "variable1=" someVar1 + "&variable2=" + someVar2;
                                byte[] reqBufferBytes = Encoding.ASCII.GetBytes(reqBuffer);
                                length += reqBufferBytes.Length; //most of the length is calculated earlier, this is the final thing added to it

                                myHttpWebRequest = (HttpWebRequest)WebRequest.Create(myReceiverUrl);
                                myHttpWebRequest.ContentType = "multipart/form-data; boundary=" + boundary;
                                myHttpWebRequest.Method = "POST";
                                myHttpWebRequest.KeepAlive = true;

                                myHttpWebRequest.ContentLength = length;

                                    //Open the stream to the server
                                    requestStream = myHttpWebRequest.GetRequestStream();

                                    //Open with the POST ITEMS before files are sent
                                    requestStream.Write(reqBufferBytes, 0, reqBufferBytes.Length);

                                    for (int i = 0; i < fileCount; i++) //For all the files
                                    {
                                        requestStream.Write(boundarybytesopen, 0, boundarybytesopen.Length);

                                        byte[] headerbytes = System.Text.Encoding.UTF8.GetBytes(header[i]);
                                        requestStream.Write(headerbytes, 0, headerbytes.Length);

                                        fileStream = new FileStream(files[i], FileMode.Open, FileAccess.Read);
                                        byte[] buffer = new byte[4096];

                                        int bytesRead = 0;
                                        // Loop through whole file uploading parts in a stream.
                                        while ((bytesRead = fileStream.Read(buffer, 0, buffer.Length)) != 0)
                                        {
                                            requestStream.Write(buffer, 0, bytesRead);
                                            requestStream.Flush();
                                        }

                                        fileStream.Close();

                                        //Finish the filestructure
                                        requestStream.Write(boundarybytesclose, 0, boundarybytesclose.Length);
                                    }

                                    requestStream.Close();

                    myHttpWebRequest.Abort();

  • 04-18-2008 8:36 In reply to

    • Neil
    • OpenNETCF Staff
    • Top 10 Contributor
    • Joined on 07-30-2007
    • North Wales
    • Posts 1,152

    Re: Example of HTTPS sftp file upload?

    You need to enclose your "POST ITEMS" with a boundary.

    //  Open with the POST ITEMS before files are sent
    requestStream.Write(boundarybytesopen, 0, boundarybytesopen.Length);
    requestStream.Write(reqBufferBytes, 0, reqBufferBytes.Length);
    requestStream.Write(boundarybytesclose, 0, boundarybytesclose.Length);
    

  • 04-18-2008 10:29 In reply to

    Re: Example of HTTPS sftp file upload?

    Great, I got it working...  sorta...

    I get a response:

    {"This request requires buffering of data for authentication or redirection to be successful."}

     

    It's an apache server, so there's no IIS authentication..

     

                                myHttpWebRequest = (HttpWebRequest)HttpWebRequest.Create(myReceiverUrl);
                                myHttpWebRequest.ContentType = "multipart/form-data; boundary=" + boundary;
                                myHttpWebRequest.Method = "POST";
                                myHttpWebRequest.KeepAlive = true;
                                myHttpWebRequest.Credentials = credentials;
                                //myHttpWebRequest.PreAuthenticate = true;
                                myHttpWebRequest.AllowWriteStreamBuffering = false;
                                myHttpWebRequest.Timeout = 10000;
                                myHttpWebRequest.ContentLength = length;

                               //Open the stream to the server
                              requestStream = myHttpWebRequest.GetRequestStream();

     

            -----All the data is sent----

     

                              //All uploading is complete, close the stream
                              requestStream.Close();

                              HttpWebResponse httpWebResponse = (HttpWebResponse)myHttpWebRequest.GetResponse();
                              XmlDocument xmlDoc = new XmlDocument();
                              xmlDoc.Load(httpWebResponse.GetResponseStream());
                              httpWebResponse.Close();
                              httpWebResponse = null;

     

    On HttpWebResponse httpWebResponse = (HttpWebResponse)myHttpWebRequest.GetResponse(); I get that error:

    {"This request requires buffering of data for authentication or redirection to be successful."}

  • 04-18-2008 11:18 In reply to

    Re: Example of HTTPS sftp file upload?

     Scratch that.. i figured it out! :o)   It was a problem with my Content Header... not related to sending.

     Well.. basically.. you can't call a redirect on your server side, because it tries to send it back to your handheld.. and thats bad..
     

  • 05-22-2008 14:33 In reply to

    Re: Example of HTTPS sftp file upload?

    does anybody have an entire project to post?  i'm trying to use the sample project from OpenNETCF.Net.Ftp.zip - it seems to establish the connection, but errors out when i try to "upload file".

    uploading to an ftp server is the main thing i'm trying to accomplish. 

    the reason i say it establishes connection is because i've setup a local ftp server, and i can see the login go through on the ftp server's status window, but it (again) errors out when i try to send a file.  and i never see "Connected" show up in the list for the program on the device.

    btw - this is the same case when trying to connect to a remote ftp server - never see "Connected".

    anyone got any advice?

     

     

  • 05-23-2008 8:16 In reply to

    Re: Example of HTTPS sftp file upload?

    so i tweaked some of the methods and i now reference the .dll from a VB project.  i got it working, but whether or not i execute from the c# project or my vb project, the connection sometimes requires 3-4 tries to successfully upload.  does anyone else have similar issues?

Page 1 of 1 (13 items)