游戏技术文章

自己积累写的winfrom 操作api 类

时间:2017-4-7 12:23:18  作者:棋牌资源网  来源:棋牌资源网  查看:7760  评论:0
内容摘要:引用 类123456789101112using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.T...
引用 类
1
2
3
4
5
6
7
8
9
10
11
12
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;
using System.Net.Http;
using System.Net.WebSockets;
using Newtonsoft.Json;
using System.Runtime.Serialization.Json;
using System.IO;
using System.Windows.Forms;

  api 在winform中的get 方法

复制代码
 public static List<T> Jsonobject<T>(Uri url)
        {

            using (var client = new HttpClient())
            {
                var result = client.GetStringAsync(url).Result;
                
                List<T> ds = JsonConvert.DeserializeObject<List<T>>(result);
                return ds;
            }
复制代码

我在这里返回的是List<T> 泛型集合,在后台获取时声明泛型收集即可.

api 在winfrom 中的put 方法

 

复制代码
public async void PueAnsy<T>(Uri uri, T t1)
        {

            string data = JsonConvert.SerializeObject(t1);
            DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(T));
            MemoryStream ms = new MemoryStream();
            serializer.WriteObject(ms, t1);
            ms.Position = 0;
            HttpContent content = new StreamContent(ms);
            content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/json");
            HttpClient client = new HttpClient();
            HttpResponseMessage response = await client.PostAsync(uri, content);
            if (response.IsSuccessStatusCode)
            {

            }
            else
            {

            }

        }
复制代码

api 在winfrom 中的Post 方法

复制代码
 public async void Update<T>(T t1, Uri url)
        {
            DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(T));
            MemoryStream ms = new MemoryStream();
            serializer.WriteObject(ms, t1); //传入model

            ms.Position = 0;
            HttpContent content = new StreamContent(ms);//将MemoryStream转成HttpContent
            content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/json");
            HttpClient client = new HttpClient();
            HttpResponseMessage response = await client.PutAsync(url, content);
            if (response.IsSuccessStatusCode)
            {
                MessageBox.Show("成功");
            }
        }
复制代码

api 在winfrom 中的Delete 方法

复制代码
public async void Delete(Uri url)
        {
            HttpClient client = new HttpClient();
            HttpResponseMessage response = await client.DeleteAsync(url);
            if (response.IsSuccessStatusCode)
            {
                MessageBox.Show("删除成功");
            }
            else
            {
                MessageBox.Show("删除失败");
            }
        }
复制代码

如果delete方法 需要传入model ,参照update 方法进行修改

标签:自己积累写的winfrom操作api类 

欢迎加入VIP,【VIP售价:只要288元永久VIP会员】畅享商业棋牌游戏程序下载,点击开通!

下载说明


☉本站所有源码和资源均由站长亲自测试-绝对保证都可以架设,运营!
☉如源码和资源有损坏或所有链接均不能下载,请告知管理员,

☉本站软件和源码大部分为站长独资,资源购买和收集,放心下载!

☉唯一站长QQ:1004003180  [人格担保-本站注重诚信!]

☉购买建议E-mail:1004003180@qq.com   源码收购 E-mail:1004003180@qq.com    

☉本站文件解压密码  【文章内都自带解压密码,每个密码不同!】


本站提供的所有源码,均来源站长提供,仅学习交流 浙ICP备09009969号

由此产生不良后果和法律责任与本站无关,如果侵犯了您的版权,请来信告知 1004003180@qq.com 将及时更正和删除! 

Copyright © 2008-2024 棋牌资源网,你身边的棋牌资源下载站    All Rights Reserved