不能说我熟悉您尝试使用的 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