码迷,mamicode.com
首页 > Web开发 > 详细

HttpClient post

时间:2017-05-27 14:27:44      阅读:218      评论:0      收藏:0      [点我收藏+]

标签:httpclient

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using Xinleda.Common;
using Xinleda.Model;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            HttpClient client = new HttpClient();


            FormUrlEncodedContent content = new FormUrlEncodedContent(
                new List<KeyValuePair<string, string>>() {
                    new KeyValuePair<string, string>("batdata[0].id", "213"),
                    new KeyValuePair<string, string> ("token","qwe") });

            var result = client.PostAsync("http://localhost:55787/api/XldSalary/Update", content).Result.Content.ReadAsStringAsync().Result;


            Console.WriteLine(result);


            Console.ReadKey();
        }
    }
}













 UploadPaydataModel model = new Xinleda.Model.UploadPaydataModel();
            model.entrname = "上海有限公司";
            model.batdata = new List<batdata>() { new batdata() { bank = "名还是呢过" } };

            var content = SerializerHelper.JsonSerialize(model);

            WebRequest request = HttpWebRequest.Create("http://localhost:55787/api/XldSalary/Update");
            request.ContentType = "application/json";
            request.Method = "post";

            using (StreamWriter sw = new StreamWriter(request.GetRequestStream()))
            {
                sw.Write(content);
                sw.Close();
            }

            WebResponse r = request.GetResponse();
            string result = "";
            using (StreamReader sr = new StreamReader(r.GetResponseStream()))
            {
                result = sr.ReadToEnd();
                sr.Close();
            }
            Console.WriteLine(result);

            Console.ReadKey();


本文出自 “程序猿的家--Hunter” 博客,请务必保留此出处http://962410314.blog.51cto.com/7563109/1930090

HttpClient post

标签:httpclient

原文地址:http://962410314.blog.51cto.com/7563109/1930090

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!