locked
WebRequest code not processing RRS feed

  • Question

  • Am I doing something wrong, I'm trying to login but nothing is happen. Meaning, no error message and no login attempt is shown on the server.

     

    private void Form1_Load(object sender, EventArgs e)
        {
    
          string strURL = "http://admin.domain.com/home/index.html?site_login_submit=1&redirect=&username=zzzz&password=zzz";
          System.Net.WebRequest req = null;
    
          try
          {
            req = WebRequest.Create(strURL);
            req.Method = "POST";
            req.Headers.Add("referer", "http://www.domain.com/");
            req.Timeout = 5000;
            WebResponse reqWebResponse = req.GetResponse();
            reqWebResponse.Close();
            linkSubmit.Text = "http://www.domain.com";
          }
          catch (Exception eq)
          {
            string sErr = "Cannot connect : " + eq.Message;
            MessageBox.Show(sErr, strURL, MessageBoxButtons.OK, MessageBoxIcon.Stop);
          }
    
    
        }
    


    Monday, August 22, 2011 11:24 PM

Answers

  • When you step through your code...does it hang at any point?

    This is what I've been using to pull a webpage down, and it's been working fine:

            var httpRequest = WebRequest.Create("{YOUR URL HERE}");
            var httpResponse = httpRequest.GetResponse();
    
            if (httpResponse == null) return;
    
            var buffer = new byte[8*1024];
            var responseStream = httpResponse.GetResponseStream();
    
            if (responseStream == null) return;
    
            var contentStringBuilder = new StringBuilder();
            var bytesRead = responseStream.Read(buffer, 0, buffer.Length);
    
            while (bytesRead > 0)
            {
              var content = Encoding.ASCII.GetString(buffer, 0, bytesRead);
    
              contentStringBuilder.Append(content);
    
              bytesRead = responseStream.Read(buffer, 0, buffer.Length);
            }
    
            var contents = contentStringBuilder.ToString();
    


    • Proposed as answer by Kevin Wiegand Tuesday, August 23, 2011 12:58 PM
    • Marked as answer by acctman Tuesday, August 23, 2011 3:27 PM
    Tuesday, August 23, 2011 1:09 AM

All replies

  • When you step through your code...does it hang at any point?

    This is what I've been using to pull a webpage down, and it's been working fine:

            var httpRequest = WebRequest.Create("{YOUR URL HERE}");
            var httpResponse = httpRequest.GetResponse();
    
            if (httpResponse == null) return;
    
            var buffer = new byte[8*1024];
            var responseStream = httpResponse.GetResponseStream();
    
            if (responseStream == null) return;
    
            var contentStringBuilder = new StringBuilder();
            var bytesRead = responseStream.Read(buffer, 0, buffer.Length);
    
            while (bytesRead > 0)
            {
              var content = Encoding.ASCII.GetString(buffer, 0, bytesRead);
    
              contentStringBuilder.Append(content);
    
              bytesRead = responseStream.Read(buffer, 0, buffer.Length);
            }
    
            var contents = contentStringBuilder.ToString();
    


    • Proposed as answer by Kevin Wiegand Tuesday, August 23, 2011 12:58 PM
    • Marked as answer by acctman Tuesday, August 23, 2011 3:27 PM
    Tuesday, August 23, 2011 1:09 AM
  • It does not hang and not errors. Basically i'm trying to use the POST method to login to my admin area. The link works if I copy and past it into a desktop browser windows with all form querystrings attached.
    Tuesday, August 23, 2011 1:51 AM
  • I would try removing the "POST" portion.  Posting to a webpage implies that you have form data you are trying to pass though...which you are not.  If you do need to use "POST", try this link that has a working example:

    http://msdn.microsoft.com/en-us/library/system.net.httpwebrequest.method.aspx

    If that isn't what you need, can you please try the snippet I posted, or modify yours to remove the "POST" operation, and add in the GetResponseStream() portion?

    Tuesday, August 23, 2011 1:56 AM
  • how would I output the response after using your code? I want to check to make sure its actually logging
    Tuesday, August 23, 2011 3:13 AM
  •  var contents = contentStringBuilder.ToString();

    The contents string will have the response, which will most likely be the HTML source of the resulting page.  You can search this for what you need.

    Tuesday, August 23, 2011 3:18 AM