ApiTransfer.cs
1.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
namespace CsbrHcgClient.Common
{
public static class ApiTransfer
{
/// <summary>
/// 调用api返回json
/// </summary>
/// <param name="url">api地址</param>
/// <param name="jsonstr">接收参数</param>
/// <param name="type">类型</param>
/// <returns></returns>
public static string HttpApi(string url, string jsonstr, string type)
{
Encoding encoding = Encoding.UTF8;
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);//webrequest请求api地址
request.Accept = "text/html,application/xhtml+xml,*/*";
request.ContentType = "application/json";
request.Method = type.ToUpper().ToString();//get或者post
byte[] buffer = encoding.GetBytes(jsonstr);
request.ContentLength = buffer.Length;
request.GetRequestStream().Write(buffer, 0, buffer.Length);
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
using (StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8))
{
return reader.ReadToEnd();
}
}
}
}