如何在Xamarin中发送POST请求?

时间:2015-05-10 07:33:40

标签: c# android xamarin xamarin.android

我已经在铁轨中建造了我的后端。 我的电子邮件地址是" sample@zmail.com"它已经注册了。密码是" 28902890"这里

在终端

中提供以下命令后
curl -v -H 'Content-Type: application/json' -H 'Accept: application/json' -X POST https://auth-agdit.herokuapp.com/api/v1/sessions -d "{\"user\":{\"email\":\"sample@zmail\",\"password\":\"28902890\"}}"

我从后端得到了这个回复,

{"success":true,"info":"Logged in :) ","data":{"authentication_token":"iexGFwJ6HwERQZ3wJ4NG"}}

现在我需要从我的Android应用中获取此数据。 我可以通过使用WebClient()。downloadString()方法获取json,用于简单的json,其中不需要身份验证,请求方法是GET。

现在我需要为POST方法获取输出Json。 我怎么能做到这一点?

3 个答案:

答案 0 :(得分:3)

有几种方法可以做到这一点。您可以使用名为RestSharp的Xamarin组件。这将为您提供与后端连接的简便方法。

var request = new RestRequest("resource/{id}", Method.POST);
request.AddParameter("name", "value"); // adds to POST or URL querystring based on Method
request.AddUrlSegment("id", 123); // replaces matching token in request.Resource

// add parameters for all properties on an object
request.AddObject(object);

// execute the request
RestResponse response = client.Execute(request);

如果您只想使用BCL提供的WebClient类,可以使用WebClient.UploadString(string, string)方法,如下所示:

using (WebClient client = new WebClient())
{
  string json = "{\"user\":{\"email\":\"sample@zmail\",\"password\":\"28902890\"}}";
  client.UploadString("https://example.com/api/v1/sessions, json);
}

如果您需要对请求进行更多控制(例如设置接受标头等),那么您可以使用HttpRequest,请参阅this question作为示例。

答案 1 :(得分:2)

我就这样做了:

  WebClient wc = new WebClient();
        string baseSiteString = wc.DownloadString("https://auth-agdit.herokuapp.com");
        string csrfToken = Regex.Match(baseSiteString, "<meta name=\"csrf-token\" content=\"(.*?)\" />").Groups[1].Value;
        string cookie = wc.ResponseHeaders[HttpResponseHeader.SetCookie];
        Console.WriteLine("CSRF Token: {0}", csrfToken);
        Console.WriteLine("Cookie: {0}", cookie);

        wc.Headers.Add(HttpRequestHeader.Cookie, cookie);
        wc.Headers.Add(HttpRequestHeader.ContentType, "application/json; charset=utf-8");
        wc.Headers.Add(HttpRequestHeader.Accept, "application/json, text/javascript, */*; q=0.01");
        wc.Headers.Add("X-CSRF-Token", csrfToken);
        wc.Headers.Add("X-Requested-With", "XMLHttpRequest");


        string dataString = @"{""user"":{""email"":""email_here"",""password"":""password_here""}}";
     //   string dataString = @"{""user"":{""email"":"""+uEmail+@""",""password"":"""+uPassword+@"""}}";
        byte[] dataBytes = Encoding.UTF8.GetBytes(dataString);
        byte[] responseBytes = wc.UploadData(new Uri("https://auth-agdit.herokuapp.com/api/v1/sessions.json"), "POST", dataBytes);
        string responseString = Encoding.UTF8.GetString(responseBytes);
        Console.WriteLine(responseString);

答案 2 :(得分:-1)

尝试使用此代码:

Uri address = new Uri("http://example.com/insert.php");
NameValueCollection nameValueCollection = new NameValueCollection();
nameValueCollection["Name"] = "string-input";

var webClient = new WebClient();
webClient.UploadValuesAsync(address, "POST", nameValueCollection);
相关问题