3

我想使用 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 等效的代码。

4

3 回答 3

3

使用表单发布:

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)
{}

POST 使用 JSON,假设你有 Dto 类:

var client = new HttpClient();
var dto = new Dto {Pro1 = "abc"};

var reponse = client.PostAsJsonAsync("api/receipt/" + jID, dto).Result;

if (reponse.IsSuccessStatusCode)
{}
于 2013-02-20T08:07:53.093 回答
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;
        }
于 2013-02-20T07:45:39.310 回答
1

是的,您可以这样做:

 HttpClient client = new HttpClient();

 client.BaseAddress = new Uri(ConfigurationManager.AppSettings["JUri"]);

您可以根据您的要求使用下面给出的这些方法之一:

Task<HttpResponseMessage> response = client.PostAsJsonAsync();

或者

Task<HttpResponseMessage> response = client.PostAsXmlAsync();

或者

Task<HttpResponseMessage> response = client.PostAsync();

希望这可以帮助!

于 2013-02-20T07:51:23.880 回答