none
HttpWebRequest.GetResponse just stops RRS feed

  • Question

  • when I run this function, it gets to Console.WriteLine("0"); then just stops. It outputs 0 then just freezes. Then after about 2 minutes, it just times out. I am fairly new to c#, I have no idea why it does this. I am calling this function from another public static function, i'm not sure if that has anything to do with it though.

           public static void HttpUploadFile(string file, string url)
            {
                Console.WriteLine(string.Format("Uploading {0} to {1}", file, url));
                string boundary = "---------------------------" + DateTime.Now.Ticks.ToString("x");
    
                Console.WriteLine("-1");
    
                HttpWebRequest wr = (HttpWebRequest)WebRequest.Create(url);
                //setting headers
                wr.ContentType = "multipart/form-data; boundary=" + boundary;
                wr.Method = "POST";
                wr.KeepAlive = true;
                wr.Credentials = cred;
    
                Console.WriteLine("0");
    
                Stream rs = wr.GetRequestStream();
    
                Console.WriteLine("400");
    
                string formItem = "\r\n--" + boundary + "\r\n" + "Content-Dis-data; name=\"dir_dest\"\r\n\r\n/var/www/html/Files/Program Files/" + "\r\n--" + boundary + "\r\n" + "Content-Dis-data; name=\"action\"\r\n\r\n10" + "\r\n--" + boundary + "\r\n" + "Content-Dis-data; name=\"upfiles[]\"; filename=\"" + file + "\"\r\nContent-Type: application/octet-stream\r\n\r\n";
                byte[] formItemBytes = System.Text.Encoding.UTF8.GetBytes(formItem);
                rs.Write(formItemBytes, 0, formItemBytes.Length);
    
                Console.WriteLine("1");
    
                FileStream fileStream = new FileStream(file, FileMode.Open, FileAccess.Read);
                byte[] buffer = new byte[4096];
                int bytesRead = 0;
                while ((bytesRead = fileStream.Read(buffer, 0, buffer.Length)) != 0)
                {
                    rs.Write(buffer, 0, bytesRead);
                }
                fileStream.Close();
    
                Console.WriteLine("2");
    
                byte[] trailer = System.Text.Encoding.ASCII.GetBytes("\r\n--" + boundary + "--\r\n");
                rs.Write(trailer, 0, trailer.Length);
                rs.Close();
    
                Console.WriteLine("3");
    
                WebResponse wresp = null;
                try
                {
                    wresp = wr.GetResponse();
                    Stream stream2 = wresp.GetResponseStream();
                    StreamReader reader2 = new StreamReader(stream2);
                    Console.WriteLine(string.Format("File uploaded, server response is: {0}", reader2.ReadToEnd()));
                }
                catch (Exception ex)
                {
                    Console.WriteLine("Error uploading file \r\n" + ex);
                    if (wresp != null)
                    {
                        wresp.Close();
                        wresp = null;
                    }
                }
                finally
                {
                    wr = null;
                }
            }

    Sunday, September 9, 2018 9:11 PM

All replies

  • If it takes more than 2 minutes to get the stream, then I suspect that your URL is not correct.   Keep in mind that this code is communicating with a server, so if there are problems there, then it could take some time.


    Bruce Eitman
    Senior Enginer
    Bruce.Eitman AT Synopsys DOT com
    My BLOG http://geekswithblogs.net/bruceeitman
    I work for
    Synopsys

    Monday, September 10, 2018 1:09 PM
    Moderator
  • The url goes to my apache server that I have running on a pi, is there a setting in apache that could be limiting the amount of requests?
    Saturday, September 15, 2018 1:41 AM
  • Great question to ask in an Apache forum.

    Sticking with things that we know about, can your device communicate with your Pi?  Have you test that yet?


    Bruce Eitman
    Senior Enginer
    Bruce.Eitman AT Synopsys DOT com
    My BLOG http://geekswithblogs.net/bruceeitman
    I work for
    Synopsys

    Monday, September 17, 2018 12:35 PM
    Moderator