3

誰かがこの問題の答えを知っていることを願っています。安全な Web ページにリクエストを送信する必要があるアプリを作成しています。これは、それ自体が安全な Web フォーム ページのアクションです (明白なことは言いませんが、基本的には、入力される情報を送信しようとしています)。最初のフォームで、フォームの「アクション」で指定されたページに送信します)。私が送信しなければならない「アクション」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」であることとは関係ないと思います。 "URL.

また、実験的に「.do」URL を取得してブラウザに貼り付け、直接 Web ブラウズしてみました。その場合、プログラムで試したときに何も返さなかったのと同じように、空のページが返されました。これは、何らかの理由で POST 経由で送信しようとした値が表示されないことを示しているようです。

これは URL が「.do」で終わっていることと関係がありますか? このような URL に送信する場合、送信時に何か特別なことを行う必要がありますか?

4

1 に答える 1

0

以下は、Microsoft の Web サイトから直接引用したコード スニペットです。

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にあります。

于 2012-12-19T20:35:42.923 に答える