我想使用API调用发布对象。我在代码隐藏
中使用以下代码获取数据 HttpClient client = new HttpClient();
client.BaseAddress = new Uri(ConfigurationManager.AppSettings["JUri"]);
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
HttpResponseMessage response = client.GetAsync("api/receipt/" + jID).Result;
if (response.IsSuccessStatusCode)
{}
我想知道有任何相当于POST的代码。
答案 0 :(得分:3)
使用表格进行POST:
HttpClient client = new HttpClient();
client.BaseAddress = new Uri(ConfigurationManager.AppSettings["JUri"]);
var postData = new List<KeyValuePair<string, string>>();
postData.Add(new KeyValuePair<string, string>("Key1", "Value1"));
postData.Add(new KeyValuePair<string, string>("Key2 ", "Value2"));
HttpContent content = new FormUrlEncodedContent(postData);
var response = client.PostAsync("api/receipt/" + jID, content)
if (response.IsSuccessStatusCode)
{}
使用JSON POST,假设你有Dto类:
var client = new HttpClient();
var dto = new Dto {Pro1 = "abc"};
var reponse = client.PostAsJsonAsync("api/receipt/" + jID, dto).Result;
if (reponse.IsSuccessStatusCode)
{}
答案 1 :(得分:1)
最好的方法是使用第三方库,例如RestSharp 通过RestSharp向api发布内容的简单方法如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;
using System.Threading;
using RestSharp;
using System.Text.RegularExpressions;
namespace ConsoleApplication2
{
public class SimpleConnector
{
private CookieContainer _cookieJar = new CookieContainer();
private RestClient client = new RestClient();
public string TwitterAuthenticate(string user, string pass)
{
client.CookieContainer = _cookieJar;
//RestClient client = new RestClient("https://twitter.com");
IRestRequest request = new RestRequest("https://twitter.com/", Method.GET);
client.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:18.0) Gecko/20100101 Firefox/18.0";
client.AddDefaultHeader("Accept", "*/*");
//request.AddParameter("name", "value"); // adds to POST or URL querystring based on Method
//request.AddUrlSegment("id", "123"); // replaces matching token in request.Resource
// easily add HTTP Headers
//request.AddHeader("header", "value");
// add files to upload (works with compatible verbs)
// execute the request
IRestResponse response = client.Execute(request);
var content = response.Content;
Match m = Regex.Match(content, @"name=""authenticity_token""\s*value=""(.*?)"">");
string authenticity_token = m.Groups[1].Value;
request = new RestRequest("https://twitter.com/sessions", Method.POST);
request.AddParameter("session[username_or_email]", user);
request.AddParameter("session[password]", pass);
request.AddParameter("return_to_ssl", "true");
request.AddParameter("scribe_log", "");
request.AddParameter("redirect_after_login", "/");
request.AddParameter("authenticity_token", authenticity_token);
response = client.Execute(request);
content = response.Content;
return content;
}
答案 2 :(得分:1)
是的,你可以这样做:
HttpClient client = new HttpClient();
client.BaseAddress = new Uri(ConfigurationManager.AppSettings["JUri"]);
您可以根据您的要求使用以下给出的其中一种方法:
Task<HttpResponseMessage> response = client.PostAsJsonAsync();
OR
Task<HttpResponseMessage> response = client.PostAsXmlAsync();
OR
Task<HttpResponseMessage> response = client.PostAsync();
希望这有帮助!