1

我需要使用 POST 参数调用 API,例如:

我看过 XDocument 但不确定如何在请求中发送参数。我也不确定我将如何异步调用,即使我应该或者在另一个线程中运行是否更好/更容易。

我将从基于 Windows 的 C# 应用程序中调用它。

4

4 回答 4

3

您可以使用WebClient的上传方法之一。

WebClient client = new WebClient();
string response = client.UploadString(
                  "http://localhost/myAPI/?options=blue&type=car", 
                  "POST data");
于 2012-04-09T17:32:17.893 回答
0

从哪里调用?Javascript?如果是这样,您可以使用 JQuery:

http://api.jquery.com/jQuery.post/

$.post('http://localhost/myAPI/', { options: "blue", type="car"}, function(data) {
  $('.result').html(data);
});

data 将包含您的帖子的结果。

如果从服务器端,您可以使用 HttpWebRequest 并使用您的参数写入它的流。

// Create a request using a URL that can receive a post. 
        WebRequest request = WebRequest.Create ("http://localhost/myAPI/");
        // Set the Method property of the request to POST.
        request.Method = "POST";
        // Create POST data and convert it to a byte array.
        string postData = "options=blue&type=car";
        byte[] byteArray = Encoding.UTF8.GetBytes (postData);
        // Set the ContentType property of the WebRequest.
        request.ContentType = "application/x-www-form-urlencoded";
        // Set the ContentLength property of the WebRequest.
        request.ContentLength = byteArray.Length;
        // Get the request stream.
        Stream dataStream = request.GetRequestStream ();
        // Write the data to the request stream.
        dataStream.Write (byteArray, 0, byteArray.Length);
        // Close the Stream object.
        dataStream.Close ();
        // Get the response.
        WebResponse response = request.GetResponse ();
        // Display the status.
        Console.WriteLine (((HttpWebResponse)response).StatusDescription);
        // Get the stream containing content returned by the server.
        dataStream = response.GetResponseStream ();
        // Open the stream using a StreamReader for easy access.
        StreamReader reader = new StreamReader (dataStream);
        // Read the content.
        string responseFromServer = reader.ReadToEnd ();
        // Display the content.
        Console.WriteLine (responseFromServer);
        // Clean up the streams.
        reader.Close ();
        dataStream.Close ();
        response.Close ();
于 2012-04-09T17:38:31.567 回答
0

要将另一个选项放入环中,您可能需要考虑使用 .NET 4.5 中的PostAsync方法HttpClient。一个公认的未经测试的刺:

        HttpClient client = new HttpClient();
        var task = client.PostAsync(string.Format("{0}{1}", "http://localhost/myAPI", "?options=blue&type=car"), null);
        Car car = task.ContinueWith(
            t =>
            {
                return t.Result.Content.ReadAsAsync<Car>();
            }).Unwrap().Result;
于 2012-04-09T18:11:07.077 回答
0

在 asp .NET 中

var client = new RestClient("www.api.url");
client.Timeout = -1;
var request = new RestRequest(Method.POST);
request.AddHeader("id", "givenid");
request.AddHeader("HASH", "generatedHash");
request.AddParameter("text/plain", "fullxml or body",  ParameterType.RequestBody);
IRestResponse response = client.Execute(request);
Console.WriteLine(response.Content);

在 HTTP 中

POST /api/somename/1.1
Host: google.com
id: merchantid
HASH: generatedshah
<node1><element>Individual</element></node1>

Phython - http.client

import http.client
import mimetypes
conn = http.client.HTTPSConnection("url")
payload = "<Node><Element>Individual</Element></Node>"
headers = {
  'id': 'givenid',
  'HASH': 'generatedhash'
}
conn.request("POST", "/api/blacklist/verify", payload, headers)
res = conn.getresponse()
data = res.read()
print(data.decode("utf-8"))
于 2020-01-15T12:15:10.463 回答