簡體   English   中英

在C#中發出POST請求

[英]Making a POST request in C#

場景:

我在server1上有一個Web方法。 我正在嘗試使用C#/ vb.net(不是jQuery)從另一台服務器server2調用server1中的Webmethod1,作為后端處理。

以下是我在server1上擁有的Web方法。

<WebMthod> _
Public Function WebMethod1(Param1 As String, Param2 As MyComplexType) As Boolean
    Try
    // Do somethidn somewhere and return boolean
    Catch ex As Exception
    // log something somewhere and throw appropriate error
    End Try
End Function

我正在使用Rest Sharp / HttpClient進行發布呼叫。 以下是使用RestClient的示例代碼:

Dim request As New RestRequest("WebMethod1", Method.POST)

Dim client As New RestClient(url)
request.RequestFormat = DataFormat.Json

request.AddParameter("Param1", 1)
request.AddParameter("Param2", JsonConvert.SerializeObject(MyComplexObject))

request.RequestFormat = DataFormat.Json
request.OnBeforeDeserialization = Function(resp)
                                              resp.ContentType = "application/json"
                                              Return Nothing
                                          End Function

Dim response As RestResponse = client.Execute(request)

問題:

現在,我沒有任何錯誤/異常,但是問題是它引發了InternalException500。可能是什么問題?

我想念什么?

這可能是因為在你的Web方法你期望string作為第一個參數,但你逝去的1這是不是串!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM