问题描述
我是Web API开发的新手。我已经找到了如何从API构建和发送GET数据请求。我以为我编写了我的例程以便能够发送帖子请求,但我还没想出如何发送我的JSON字符串以及要发布的数据。
我已经包含了我的代码,非常感谢所有帮助。
Hi, I am new to development with Web API's. I have figured out how to build and send request to GET data from the the API. I thought I had coded my routines to be able to send a post request, but I have not figured out how to send my JSON string with the data to be posted.
I have included my code and any and all help is greatly appreciated.
Public Async Function MakeRequest(verb As String, resource As String) As Task
Dim client As HttpClient = New HttpClient
Dim response As Task(Of HttpResponseMessage)
Dim result As HttpResponseMessage = New HttpResponseMessage
Dim request As HttpRequestMessage = New HttpRequestMessage()
Dim requestp As HttpWebRequest
Dim queryString As String = Nothing
' determine sandbox or live there
RequestGood = False
If My.Application.CommandLineArgs(0).ToString() = "S" Then
uri = New Uri(sandboxuri)
Else
uri = New Uri(liveuri)
End If
commapikey = "21DDA0A8-9F6B-498A-BD1E-0116C308A263"
sSignature = GetAuthHeader(softwarekey, apiclientid)
client = CreateClient(uri, commapikey, apiversion, sSignature)
headers = GetHeaders(commapikey, apiversion, sSignature)
Select Case verb
Case "GET"
request = BuildRequest(client, HttpMethod.Get, String.Format("{1}?subscription-key={0}", subscriptionkey, resource))
Case "PUT"
request = BuildRequest(client, HttpMethod.Put, String.Format("{1}?subscription-key={0}", subscriptionkey, resource))
Case "POST"
request = BuildRequest(client, HttpMethod.Post, String.Format("{1}?subscription-key={0}", subscriptionkey, resource))
Case Else
request = BuildRequest(client, HttpMethod.Get, String.Format("{1}?subscription-key={0}", subscriptionkey, resource))
End Select
response = client.SendAsync(request)
result = response.Result
If result.StatusCode = HttpStatusCode.BadRequest Then
' error
Else
json = result.Content.ReadAsStringAsync().Result
RequestGood = True
End If
End Function
Public Function GetAuthHeader(ByVal key As String, ByVal message As String) As String
Dim sToken As String = String.Format("{0}:{1}", message, key)
Dim sReturn As String = Nothing
Dim bTokenBytes() = System.Text.ASCIIEncoding.UTF8.GetBytes(sToken)
sReturn = String.Format("Basic {0}", Convert.ToBase64String(bTokenBytes))
Return sReturn
End Function
Public Function CreateClient(ByVal config As Uri, ByVal commApiKey As String, ByVal APIVer As String, ByVal APISignature As String) As HttpClient
Dim handler As HttpClientHandler = New HttpClientHandler
Dim client As New HttpClient(handler)
client.BaseAddress = config
client.DefaultRequestHeaders.Accept.Add(New MediaTypeWithQualityHeaderValue("application/json"))
client.DefaultRequestHeaders.Add("community-api-key", commApiKey)
client.DefaultRequestHeaders.Add("api-version", APIVer)
client.DefaultRequestHeaders.Add("authorization", APISignature)
If json <> Nothing Then
client.DefaultRequestHeaders.Add("data", json)
End If
Return client
End Function
Public Function GetHeaders(ByVal commApiKey As String, ByVal APIVer As String, ByVal APISignature As String) As String
Dim sb As New StringBuilder()
sb.Append(String.Format("community-api-key:{0}" & Chr(10), commApiKey))
sb.Append(String.Format("api-version:{0}" & Chr(10), APIVer))
sb.Append(String.Format("authorization:{0}" & Chr(10), APISignature))
Return sb.ToString().ToLower()
End Function
Public Function BuildRequest(ByVal client As HttpClient, ByVal method As HttpMethod, ByVal uri As String) As HttpRequestMessage
Dim request As HttpRequestMessage = New HttpRequestMessage()
request.Headers.Accept.Add(New MediaTypeWithQualityHeaderValue("application/json"))
request.Method = method
request.RequestUri = New Uri(client.BaseAddress, uri)
Return request
End Function
推荐答案
...
5.将ContentType属性设置为适当的值:
...
5. Set the ContentType property to an appropriate value:
request.ContentType = "..."
6.通过调用 []:
6. Get the stream that holds request data by calling the GetRequestStream
method[^]:
dataStream = request.GetRequestStream()
7.将数据写入此方法返回的Stream对象:
7. Write the data to the Stream object returned by this method:
dataStream.Write(byteArray, 0, byteArray.Length)
8.通过调用Stream.Close方法关闭请求流:
8. Close the request stream by calling the Stream.Close method:
dataStream.Close()
...
...
这篇关于我可以使用相同的SendAsync进程在Web API中发布事务吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!