none
Transfering txt file from one machine to a another machine over internet RRS feed

  • Question

  • Hi frnds,

    I want to transfer a text file from my machine to a different machine over internet.

    I have written  following code but it is giving me error:

    HTTP Error 405 Method not allowed

    public string UploadTextFile(string Entity_Id, string filedata)
            {
                WebProxy myProxy = new WebProxy();
                try
                {
                    string uri = "http://207.201.162.20:5299/";
                    //if (cmd.Length > 0)
                    //{
                    //    uri = uri + "?cmd=" + cmd;
                    //}
                    byte[] bytes = Encoding.UTF8.GetBytes(body);
                    WebRequest request = WebRequest.Create(uri);
                    //myProxy = (WebProxy)request.Proxy;
                    //request.Proxy = myProxy;
                    request.Method = "POST";
                    request.ContentType = "text/plain; charset=UTF-8";
                    request.ContentLength = bytes.Length;
                    Stream requestStream = request.GetRequestStream();
                    requestStream.Write(bytes, 0, bytes.Length);
                    requestStream.Close();
                    WebResponse response = request.GetResponse();
                    StreamReader reader = new StreamReader(response.GetResponseStream());
                    string str2 = reader.ReadToEnd();
                    reader.Close();
                    response.Close();
                    //if ((str2.Length > 0) && (str2[0] == '!'))
                    //{
                    //    return str2;
                    //}
                    return "";
                }
                catch (Exception exception)
                {
                    return exception.ToString();
                }
            }


    SaiVaibhav@Acumen


    • Edited by vaibhavsaral Friday, February 8, 2013 2:16 PM
    • Moved by Mike Feng Tuesday, February 12, 2013 9:14 AM
    Friday, February 8, 2013 2:15 PM

Answers

  • The WebRequest dosn't accept "POST".  Only the response can use "POST".  Either remove the line below (the default is GET) or change POST to GET.

    From : request.Method = "POST";

    To : request.Method = "GET";  (or eliminate)


    jdweng

    Friday, February 8, 2013 2:50 PM
  • I don't know what application is running on the server where you are transfering the text file.  Uusally people use FTP to transfer the files.  WebRedquest requires a webpage that is designed to accept the file you are transfering.  So you would need to develope a custom webpage.  FTP is a standard application that uses HTTP that is available on most PCs and Server.  IN C# you would use the FTPWebRequest Class.

    See webpage below

    http://www.codeproject.com/Articles/343913/Simple-FTP-library-in-Csharp


    jdweng

    Tuesday, February 12, 2013 11:01 AM

All replies

  • The WebRequest dosn't accept "POST".  Only the response can use "POST".  Either remove the line below (the default is GET) or change POST to GET.

    From : request.Method = "POST";

    To : request.Method = "GET";  (or eliminate)


    jdweng

    Friday, February 8, 2013 2:50 PM
  • Thanks jdweng,

    But can u tell me another way to do this.


    SaiVaibhav@Acumen

    Tuesday, February 12, 2013 9:17 AM
  • I don't know what application is running on the server where you are transfering the text file.  Uusally people use FTP to transfer the files.  WebRedquest requires a webpage that is designed to accept the file you are transfering.  So you would need to develope a custom webpage.  FTP is a standard application that uses HTTP that is available on most PCs and Server.  IN C# you would use the FTPWebRequest Class.

    See webpage below

    http://www.codeproject.com/Articles/343913/Simple-FTP-library-in-Csharp


    jdweng

    Tuesday, February 12, 2013 11:01 AM