0

我正在开发一个 DotNetNuke 模块并创建了两个模块。第一个模块接受发布的数据。第二个模块将发布数据发送到第一个模块。我无法将帖子数据发送到特定的 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 回答