【问题标题】:Programmatically submitting to .do file以编程方式提交到 .do 文件
【发布时间】:2012-12-19 20:25:33
【问题描述】:

我希望有人知道这个问题的答案。我正在编写一个必须向安全网页提交请求的应用程序,这本身就是安全网页表单页面的操作(不是很明显,但基本上我正在尝试提交将要填写的信息在初始表单上,并将其提交到表单的“操作”中指定的页面)。我必须提交的“操作”URL 以“.do”扩展名结尾,据我所知,它指定了一个在 JAVA 中动态构建的页面。

我的问题是提交时我没有得到任何回报。这是我正在使用的代码(VB.Net,针对 4.0 版):

Dim PostValues As New NameValueCollection()
Dim RespString As String
Dim RespBytes() As Byte

' URL below is "action" for web form at https://www.deadiversion.usdoj.gov/webforms/validateLogin.jsp
Dim URL As String = "https://www.deadiversion.usdoj.gov/webforms/validateLogin.do"
' These POST values are obtained by examing the source code for the web form ".jsp" page, looking for "input" tags
PostValues.Add("submit", "")
PostValues.Add("deaNum", "--dea number value for our company--")
PostValues.Add("lname", HttpUtility.UrlEncode("boca pharmacal inc"))
PostValues.Add("ssn", "")
PostValues.Add("taxid", "--tax id value for our company--")
PostValues.Add("buttons.next", "Login")

Dim client As New WebClient()
client.Headers.Add("Content-Type", "application/x-www-form-urlencoded")
client.UseDefaultCredentials = True
RespBytes = client.UploadValues(URL, "POST", PostValues)
RespString = Encoding.UTF8.GetString(RespBytes)

当我运行这段代码时,RespBytes 的长度为 0,然后 RespString 只是一个空字符串。相比之下,如果我实验性地替换上面评论中提到的以“.jsp”结尾的 URL,我会得到一个响应,没问题,所以我认为这与它是一个“https”这一事实没有任何关系" 网址。

我还试验性地将“.do” URL 粘贴到我的浏览器中,然后尝试直接通过网络浏览它。在这种情况下,它发回了一个空页面,就像我以编程方式尝试它时它什么也没发回一样,这似乎表明由于某种原因它看不到我尝试通过 POST 发送的值。

这是否与 URL 以“.do”结尾的事实有关?提交到这样的 URL 是否需要我在提交时做一些特别的事情?

【问题讨论】:

    标签: http-post


    【解决方案1】:

    这是微软网站上的代码 sn-p:

    Imports System
    Imports System.IO
    Imports System.Net
    Imports System.Text
    Namespace Examples.System.Net
        Public Class WebRequestPostExample
    
            Public Shared Sub Main()
                ' Create a request using a URL that can receive a post. 
                Dim request As WebRequest = WebRequest.Create("http://www.contoso.com/PostAccepter.aspx ")
                ' Set the Method property of the request to POST.
                request.Method = "POST"
                ' Create POST data and convert it to a byte array.
                Dim postData As String = "This is a test that posts this string to a Web server."
                Dim byteArray As Byte() = Encoding.UTF8.GetBytes(postData)
                ' Set the ContentType property of the WebRequest.
                request.ContentType = "application/x-www-form-urlencoded"
                ' Set the ContentLength property of the WebRequest.
                request.ContentLength = byteArray.Length
                ' Get the request stream.
                Dim dataStream As Stream = request.GetRequestStream()
                ' Write the data to the request stream.
                dataStream.Write(byteArray, 0, byteArray.Length)
                ' Close the Stream object.
                dataStream.Close()
                ' Get the response.
                Dim response As WebResponse = request.GetResponse()
                ' Display the status.
                Console.WriteLine(CType(response, HttpWebResponse).StatusDescription)
                ' Get the stream containing content returned by the server.
                dataStream = response.GetResponseStream()
                ' Open the stream using a StreamReader for easy access.
                Dim reader As New StreamReader(dataStream)
                ' Read the content.
                Dim responseFromServer As String = reader.ReadToEnd()
                ' Display the content.
                Console.WriteLine(responseFromServer)
                ' Clean up the streams.
                reader.Close()
                dataStream.Close()
                response.Close()
            End Sub
        End Class
    End Namespace
    

    您可以在http://msdn.microsoft.com/en-us/library/debx8sh9.aspx?cs-save-lang=1&cs-lang=vb#code-snippet-27找到代码

    【讨论】:

      猜你喜欢
      • 2014-01-27
      • 2010-11-22
      • 2018-06-13
      • 2013-03-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多