0

私はDotNetNukeモジュールに取り組んでおり、2つのモジュールを作成しました。最初のモジュールは、投稿されたデータを受け入れます。2番目のモジュールは、投稿データを最初のモジュールに送信します。投稿データを特定のURLに送信できません。受信モジュールは、データをデータベースに保存する必要があります。以下は私が投稿に使用するコードです。

  string myParameters = "EmailId=" + tbEmail.Text;
  WebClient wc = new WebClient();
  wc.Headers["Content-type"] = "application/x-www-form-urlencoded";
  string HtmlResult = wc.UploadString(UrlKeyGen, myParameters);

どんな助けでもいただければ幸いです。前もって感謝します。

4

1 に答える 1

0

あなたが使おうとしている WebClient クラスに精通しているとは言えません。投稿に使用するコードは次のとおりです。

        Public Function UrlPOST(ByVal URL As String, ByVal POSTdata As String) As String
        Dim rtnValue As StringBuilder = New StringBuilder()
        Dim isValidParams As Boolean = True

        ' check for url
        If (URL.Length < 10) Then
            isValidParams = False
            Throw New Exception("URL = '" + URL + "' is not a valid URL.")
        ElseIf (URL.IndexOf("://") <= 0) Then
            isValidParams = False
            Throw New Exception("URL = '" + URL + "' is not a valid URL. URL must include protocol, e.g. 'http://'")
        End If

        If (isValidParams) Then
            Dim result As WebResponse = Nothing
            Try
                ' setup WebRequest object
                Dim request As WebRequest = WebRequest.Create(URL)
                request.Method = "POST"
                request.Timeout = 5000
                request.ContentType = "application/x-www-form-urlencoded"

                ' add payload for POST to request stream
                Dim SomeBytes As Byte()
                SomeBytes = Encoding.UTF8.GetBytes(POSTdata)
                request.ContentLength = SomeBytes.Length
                Dim newStream As Stream = request.GetRequestStream()
                newStream.Write(SomeBytes, 0, SomeBytes.Length)
                newStream.Close()

                ' POST data and get response
                result = request.GetResponse()
                Dim objSD As StringDictionary = URIQueryParser(result.ResponseUri.Query())

                rtnValue.Append(TranslateErrorCode(objSD("codes")))

            Catch ex As Exception
                Dim ex1 As New Exception("Exception Posting to External Site. Posting String: " & POSTdata & "... " & ex.ToString)
                LogException(ex1)
                '               Dim objEventLog As New Services.Log.EventLog.EventLogController
                '               objEventLog.AddLog("WebRequest Exception: ", "Exception Posting to External Site: " & ex.ToString  & ", Posting String: " & PostData, PortalSettings, UserId, Services.Log.EventLog.EventLogController.EventLogType.ADMIN_ALERT)
                Throw ex1
            Finally
                If (Not (result Is Nothing)) Then
                    result.Close()
                    result = Nothing
                End If
            End Try
        End If
        Return rtnValue.ToString()
    End Function

    Private Function URIQueryParser(ByVal UriQuery As String) As System.Collections.Specialized.StringDictionary
        Dim sd As New StringDictionary
        Dim objStringArray() As String
        If (Not UriQuery Is Nothing) AndAlso UriQuery.Length > 0 Then
            UriQuery = UriQuery.Substring(1)
            objStringArray = UriQuery.Split("&"c)
            For Each s As String In objStringArray
                sd.Add(s.Split("="c)(0), s.Split("="c)(1))
            Next
        End If

        Return sd
    End Function
于 2012-06-15T15:30:17.583 に答える