2012-06-15 34 views
0

我正在使用DotNetNuke模塊並創建了兩個模塊。第一個模塊接受發佈的數據。第二個模塊將發佈數據發送到第一個模塊。我無法將發佈數據發送到特定的網址。接收器模塊應該將數據存儲到數據庫中。以下是我用於該帖子的代碼。無法發送郵政價值到一個特定的網址?

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

任何幫助,將不勝感激。 預先感謝您。

回答

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