C# 如何调用xmlpostapi
我需要使用POST参数调用API,例如:C# 如何调用xmlpostapi,c#,.net,winforms,C#,.net,Winforms,我需要使用POST参数调用API,例如: URL= ARGS=选项=蓝色&类型=汽车 我已经看过XDocument,但不确定如何在请求中发送参数。我也不确定如何异步调用,甚至不确定是否应该异步调用,或者在另一个线程中运行是否更好/更容易 我将从基于windows的C#应用程序中调用此功能。您可以使用其中一个 从哪里打电话?Javascript?如果是这样,您可以使用JQuery: 数据将包含您发布的结果 如果是从服务器端,您可以使用HttpWebRequest并使用参数写入其流 // Cr
- URL=
- ARGS=选项=蓝色&类型=汽车
从哪里打电话?Javascript?如果是这样,您可以使用JQuery: 数据将包含您发布的结果 如果是从服务器端,您可以使用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 ();
在环中抛出另一个选项,您可能想考虑使用.<代码> PASTASYNC 方法> .NET中的代码> HTTPcli客< /代码>。一个公认的未经测试的刺:
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;
HttpClient=newhttpclient();
var task=client.PostAsync(string.Format(“{0}{1}”)http://localhost/myAPI“,”?选项=蓝色,类型=汽车”),空);
Car Car=任务。继续(
t=>
{
返回t.Result.Content.ReadAsAsync();
}).Unwrap().Result;
在asp.NET中
在HTTP中
POST/api/somename/1.1
主持人:google.com
id:merchantid
HASH:generatedshah
个人
Phython-http.client
导入http.client
导入模拟类型
conn=http.client.HTTPSConnection(“url”)
有效载荷=“个人”
标题={
'id':'givenid',
'HASH':'generatedhash'
}
conn.request(“POST”、“/api/blacklist/verify”、有效负载、标题)
res=conn.getresponse()
data=res.read()
打印(数据解码(“utf-8”))
来自问题:我会从基于windows的C#应用程序中调用它。
谢谢你的提示,这很有帮助。我还发现我还需要设置client.Headers。。标题[“内容类型”]=“应用程序/x-www-form-urlencoded”;
// 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 ();
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;
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);
POST /api/somename/1.1
Host: google.com
id: merchantid
HASH: generatedshah
<node1><element>Individual</element></node1>
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"))