title: How to make a POST request to a page that may redirect to a login page
body: <p>I am using a macro in Outlook VBA to submit a file via POST to a URL:</p>
  
  <pre><code>Set http = New WinHttp.WinHttpRequest
  http.Open "POST", UrlToPostTo, False    'True                                          '
  http.setRequestHeader "User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)"
  http.setRequestHeader "Content-Type", "multipart/form-data; "
  http.Send data
  </code></pre>
  
  <p>My problem is the page that will accept the request (a file upload page, in this case) is protected by authentication - the initial request for it above will return a login page instead of the page itself.</p>
  
  <p>I have tried to detect if the login page appears and if so, post the username and password as form variables (I'm hoping this is equivalent to a human typing said username and password into a page in the web browser).</p>
  
  <p>So the steps are:<br>
  * request URL (include file with post).<br>
  * Check if the reponse is the login page.<br>
  * If so, then in the same http session, submit the username and password to the URL.<br>
  * If the server now processes the original post, good, otherwise I can post it again.</p>
  
  <p>The code looks like:</p>
  
  <pre><code>' if the login page comes back, send credentials                                     '
  If (InStr(http.ResponseText, "j_password") &gt; 0) Then
  
      Dim loginData As String
      loginData = "j_username=theusername&amp;j_password=thepassword"
  
      http.Open "POST", UrlToPostTo, False
      http.setRequestHeader "User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)"
      http.setRequestHeader "Content-Type", "multipart/form-data; "
      http.Send loginData
  End If
  </code></pre>
  
  <p>But when I do this, The http.Responsetext is just the login page still (or again?). </p>
  
  <p>Any idea what I'm doing wrong? Is my plan even valid?</p>
  
  <p>(This is related to trying to solve <a href="https://stackoverflow.com/questions/851269/outlook-how-do-i-post-the-selected-message-to-a-web-server-using-vba">this problem</a> )</p>