VB.NET中的Web服务呼叫问题

时间:2018-10-12 13:30:20

标签: vb.net

我正在尝试调用API方法,但是却收到“ 401未经授权”。如果我从邮递员那里调用相同的方法,它会起作用。

我的代码:

ServicePointManager.SecurityProtocol = CType(3072, SecurityProtocolType)

objClient = New RestSharp.RestClient(SL_Data.RequestURL & sMethodName)
objRequest = New RestSharp.RestRequest(RestSharp.Method.POST)
objRequest.AddHeader("Content-Type", "application/x-www-form-urlencoded")
objRequest.AddHeader("Host", "api.levinfintech.com")
objRequest.AddParameter("form-data", sWSRequest)

Dim objResponse As RestSharp.IRestResponse = objClient.Execute(objRequest)
If objResponse.StatusCode <> HttpStatusCode.OK Then
    sWSResponse = "Response Not Receive, Please try again"
    bPage.WriteDataToFileNew("SL_GetResponse", objResponse.StatusDescription)
    Return False
End If

邮递员代码:

Private Sub SurroundingSub()
    Dim client = New RestClient("https://api.levinfintech.com/api/levin/get-customer")
    Dim request = New RestRequest(Method.POST)
    request.AddHeader("postman-token", "3aeb1721-4d76-0f7b-c491-77b7f828efaf")
    request.AddHeader("cache-control", "no-cache")
    request.AddHeader("content-type", "multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW")
    request.AddParameter("multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW", "------WebKitFormBoundary7MA4YWxkTrZu0gW" & vbCrLf & "Content-Disposition: form-data; name=""api_token""" & vbCrLf & vbCrLf & "YhqF52lYHE5b6YubIpS4YmrSM033jQL5Bay7SerFX1vbEazrhjzJ1bP7zqnn" & vbCrLf & "------WebKitFormBoundary7MA4YWxkTrZu0gW" & vbCrLf & "Content-Disposition: form-data; name=""mobile_number""" & vbCrLf & vbCrLf & "8460883677" & vbCrLf & "------WebKitFormBoundary7MA4YWxkTrZu0gW--", ParameterType.RequestBody)
    Dim response As IRestResponse = client.Execute(request)
End Sub

0 个答案:

没有答案