title: Internal server error (500) httpwebrequest
body: <blockquote>
    <p>System.Net.WebException: The remote
    server returned an error: (500)
    Internal Server Error. at
    System.Net.HttpWebRequest.GetResponse()
    at
    refill.btnRequestRefill_Click(Object
    sender, EventArgs e)</p>
  </blockquote>
  
  <p>I get this error when trying to submit a form. Here is my code:</p>
  
  <pre><code>Protected Sub btnRequestRefill_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnRequestRefill.Click
  
      Try
          Dim url As String = "www.domainname.com"
  
          url += "lastName=" &amp; LastName.Text
          url += "&amp;birthDate=" &amp; Birthdate.Text
          url += "&amp;RxNumbers=" &amp; RxNumber.Text
          url += "&amp;deliveryMethod=" &amp; deliverymethod.SelectedValue
          url += "&amp;phone=" &amp; PhoneNumber.Text
          url += "&amp;email=" &amp; Email.Text
  
          Dim myrequest As HttpWebRequest = TryCast(WebRequest.Create(url), HttpWebRequest)
  
          Dim docresponse As WebResponse = myrequest.GetResponse()
          Dim responsestream As Stream = docresponse.GetResponseStream()
          Dim objXMLDoc As New XmlDocument()
          Dim colElements As XmlNodeList
          Dim colElements2 As XmlNodeList
          Dim mywebclient As New WebClient()
          Dim a As Object
          Dim listingscount As Integer = 0
  
          objXMLDoc.Load(responsestream)
          colElements = objXMLDoc.GetElementsByTagName("script")
  
          For Each objnode As XmlNode In colElements
              Dim attCol As XmlAttributeCollection = objnode.Attributes
  
              If objnode.Name = "script" Then
                  If attCol(0).Value = "false" Then
                      colElements2 = objXMLDoc.GetElementsByTagName("rxResponse")
                      For Each objnode2 As XmlNode In colElements2
                          Response.Write("&lt;br/&gt;" &amp; objnode2.InnerText &amp; "&lt;br/&gt;")
                      Next
                  Else
                      Response.Write("&lt;br/&gt;yay it when through&lt;br/&gt;")
  
                      Dim redir, mailto, mailfrom, subject, item, body, cc, bcc, message, html, template, usetemplate, testmode
                      subject = "Refill Submit - test"
  
                      usetemplate = False
  
                      Dim msg = Server.CreateObject("CDO.Message")
                      msg.subject = subject
                      msg.to = "name@domainname.com,name@domainname.com,name@domainname.com"
                      msg.from = "name@domainname.com"
  
                      For Each item In Request.Form
                          Select Case item
                              Case "redirect", "mailto", "cc", "bcc", "subject", "message", "template", "html", "testmode", "__EVENTTARGET", "__EVENTARGUMENT", "__VIEWSTATE", "__EVENTVALIDATION", "btnRequestRefill","__LASTFOCUS"
                              Case Else
                                  If Not usetemplate Then
                                      If item &lt;&gt; "mailfrom" Then body = body &amp; item &amp; ": " &amp; Request.Form(item) &amp; vbCrLf &amp; vbCrLf
                                  Else
                                      body = Replace(body, "[$" &amp; item &amp; "$]", Replace(Request.Form(item), vbCrLf, "&lt;br&gt;"))
                                  End If
                          End Select
                      Next
  
                      If usetemplate Then 'remove any leftover placeholders
                          Dim rx As New Regex("\[\$.*\$\]")
                          body = rx.Replace(body, "")
                      End If
  
                      If usetemplate And LCase(Request.Form("html")) = "yes" Then
                          msg.htmlbody = body
                      Else
                          msg.textbody = body
                      End If
  
                      msg.send()
                      msg = Nothing
                  End If
              Else
                  Response.Write("&lt;br/&gt;" &amp; "not script" &amp; "&lt;br/&gt;")
              End If
              'Response.Write("&lt;br/&gt;" &amp; objnode.Name &amp; ": " &amp; objnode.InnerText &amp; "&lt;br/&gt;")
          Next
      Catch ex As Exception
          Response.Write("&lt;br/&gt;" &amp; ex.ToString &amp; "&lt;br/&gt;")
  
      End Try
  End Su
  </code></pre>