2013-02-20 46 views
3

我想使用API​​调用发布对象。我在代码隐藏中使用以下代码获取数据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

你为什么不定义金额和MailID模式? – 2013-02-21 05:04:10

+0

参数太多,并且模型参数不起作用。所以我把它作为单独的字符串传递。 – NewBie 2013-02-21 05:05:35

回答

3

POST使用形式:使用JSON

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,假设你有DTO类:

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

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

if (reponse.IsSuccessStatusCode) 
{} 
+0

对于使用JSON的POST,我是否需要将postData设置为键值对? – NewBie 2013-02-20 09:43:25

+0

@NewBie:不,只是将你的对象传入方法 – 2013-02-20 10:17:02

+0

我试过你的代码,它加深了调用我的POST方法,但我的参数没有被分配到post方法的参数。 – NewBie 2013-02-21 04:34:42

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; 
     } 
+0

没有任何我可以直接使用..没有第三方库?一些平淡的东西? – NewBie 2013-02-20 07:50:57

+0

你可以像HttpWebRequest.Method一样使用HttpWebRequest,比你必须写你发布的内容流并计算它的长度,然后 - 发送到服务器 – 2013-02-20 07:56:21

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(); 

希望这有助于!

+0

我可以在哪里传递发布数据?我有和对象,如何发送它作为发布数据? – NewBie 2013-02-20 07:54:54