• wp8 入门到精通 WebClient Post


    WebClient wc = new WebClient();

    var URI = new Uri("http://your_uri_goes_here");

    //If any encoding is needed.

    wc.Headers["Content-Type"] = "application/x-www-form-urlencoded";

    //Or any other encoding type.

    //If any key needed

    wc.Headers["KEY"] = "Your_Key_Goes_Here";

    wc.UploadStringCompleted += new UploadStringCompletedEventHandler(wc_UploadStringCompleted);

    wc.UploadStringAsync(URI, "POST", "Data_To_Be_sent");

    void wc_UploadStringCompleted(object sender, UploadStringCompletedEventArgs e)
    {

    try
    {
    MessageBox.Show(e.Result);
    //e.result fetches you the response against your POST request.

    }

    catch (Exception exc)
    {
    MessageBox.Show(exc.ToString());
    }

    }

    ===================================================

    using Newtonsoft.Json.Linq;
    using Newtonsoft.Json;

     JObject ubody = new JObject();

    ubody.Add(new JProperty("cmd", "user"));
    JObject uData = new JObject();
    uData.Add(new JProperty("name", ""));
    uData.Add(new JProperty("sex", ""));
    uData.Add(new JProperty("age", ""));
    ubody.Add(new JProperty("data", uData));

    string Content = JsonConvert.SerializeObject(ubody);

    Uri address = new Uri("http://api.api.cn/");
    WebClient webClient = new WebClient();
    webClient.UploadStringAsync(address, "POST", Content);
    webClient.Encoding = System.Text.Encoding.UTF8;
    webClient.Headers[HttpRequestHeader.Accept] = "*/*";
    webClient.Headers[HttpRequestHeader.ContentType] = "application/x-www-form-urlencoded";
    webClient.Headers[HttpRequestHeader.UserAgent] = "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1)";
    webClient.UploadStringCompleted += (s1, e1) =>
    {
    try
    {
    ShellToast toast = new ShellToast();
    toast.Title = "Background Agent Sample";
    toast.Content = e1.Result;
    toast.Show();

    }
    catch (Exception ex)
    {


    }
    };

  • 相关阅读:
    668. Kth Smallest Number in Multiplication Table
    658. Find K Closest Elements
    483. Smallest Good Base
    475. Heaters
    454. 4Sum II
    441. Arranging Coins
    436. Find Right Interval
    410. Split Array Largest Sum
    392. Is Subsequence
    378. Kth Smallest Element in a Sorted Matrix
  • 原文地址:https://www.cnblogs.com/androllen/p/3634953.html
Copyright © 2020-2023  润新知