Web API HttpClient POST无法在.Net中运行

时间:2016-12-09 20:35:48

标签: .net json http-post httpclient dotnet-httpclient

我需要通过服务器端的Web API调用来获取和POST数据

GET呼叫正在工作并且正在退回json,见下文

Dim json As String = String.Empty
Dim client As New HttpClient()
client.BaseAddress = New Uri("http:\\example.com\test")
client.DefaultRequestHeaders.Accept.Add(New MediaTypeWithQualityHeaderValue("application/json"))
Dim response As HttpResponseMessage = client.GetAsync("http:\\example.com\test").Result

If response.IsSuccessStatusCode Then
    json = response.Content.ReadAsStringAsync().Result
End If

当我尝试在下面进行POST调用时,没有任何事情发生

POST调用接受json

{
  "messageDescription": "string",
  "effectiveStartDate": "2016-12-09",
  "effectiveEndDate": "2016-12-09",
  "isActive": "string"
}

Dim client = New HttpClient()
client.BaseAddress = New Uri("http:\\example.com\test")
client.DefaultRequestHeaders.Accept.Add(New MediaTypeWithQualityHeaderValue("application/json"))
Dim request = New HttpRequestMessage(HttpMethod.Post, "http:\\example.com\test")

Dim isActive As String = "Y"

Dim requestContent = String.Format("effectiveStartDate={0}&effectiveEndDate={1}&messageDescription={2}&isActive={3}", effectiveStartDate, effectiveEndDate, txtValue, isActive)
request.Content = New StringContent(requestContent, Encoding.UTF8, "application/x-www-form-urlencoded")

Dim response = client.SendAsync(request)

有人可以帮我正确指导

0 个答案:

没有答案