C#请求接口的方法,具体代码:
首先需要添加引用和第三方的组件,具体如下:
引用命名空间:
using System.IO;
using Newtonsoft.Json.Linq;
using System.Web.Script.Serialization;
常用到的两种请求方式的代码类,具体实现如下
/// <summary>
/// Post 带有参数请求Json的方法
/// </summary>
/// <param name="PARAMETER">请求的参数</param>
/// <param name="POSTURL">请求的URL地址</param>
/// <returns>返回json字符串</returns>
public static JObject RequestJsonByUrlPost(string PARAMETER, string POSTURL)
{
JObject jo = new JObject();
try
{
UTF8Encoding encoding = new UTF8Encoding();
byte[] data = encoding.GetBytes(PARAMETER);
HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(POSTURL);
myRequest.Method = "Post";
myRequest.ContentType = "application/x-www-form-urlencoded";
myRequest.ContentLength = data.Length;
Stream newStream = myRequest.GetRequestStream();
newStream.Write(data, , data.Length);
newStream.Close();
HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse();
StreamReader reader = new StreamReader(myResponse.GetResponseStream(), Encoding.UTF8);
string sJson = reader.ReadToEnd();
jo = JObject.Parse(sJson);
}
catch (Exception ex)
{
MessageUtil.ShowException("系统错误,请联系管理员!", ex);
}
return jo;
}
/// <summary>
/// Get 请求的方法
/// </summary>
/// <param name="geturl">请求的URL地址</param>
/// <returns>返回json字符串</returns>
public static JObject RequestJsonByUrlGet(string geturl)
{
JObject jo = new JObject();
try
{
HttpWebRequest http = WebRequest.Create(geturl) as HttpWebRequest;
HttpWebResponse response = (HttpWebResponse)http.GetResponse();
StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8); string sJson = reader.ReadToEnd();
http.Abort();
ArrayList ObjList = new ArrayList();
//将字符串转换成Json对象
jo = JObject.Parse(sJson);
}
catch (Exception ex)
{
MessageUtil.ShowException("系统错误,请联系管理员!", ex);
}
return jo;
}
处理接口返回的 JSON 数据转换常用的帮助类,具体代码如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace HackMoney
{
public class JsonHelper
{ /// <summary>
/// 转换对象为JSON格式数据
/// </summary>
/// <typeparam name="T">类</typeparam>
/// <param name="obj">对象</param>
/// <returns>字符格式的JSON数据</returns>
public static string GetJSON<T>(object obj) {
string result = String.Empty;
try {
System.Runtime.Serialization.Json.DataContractJsonSerializer serializer =new System.Runtime.Serialization.Json.DataContractJsonSerializer(typeof(T));
using (System.IO.MemoryStream ms = new System.IO.MemoryStream()) {
serializer.WriteObject(ms, obj);
result = System.Text.Encoding.UTF8.GetString(ms.ToArray());
}
} catch (Exception ex) {
throw ex;
}
return result;
}
/// <summary>
/// 转换List<T>的数据为JSON格式
/// </summary>
/// <typeparam name="T">类</typeparam>
/// <param name="vals">列表值</param>
/// <returns>JSON格式数据</returns>
public static string JSON<T>(List<T> vals) {
System.Text.StringBuilder st = new System.Text.StringBuilder();
try {
System.Runtime.Serialization.Json.DataContractJsonSerializer s = new System.Runtime.Serialization.Json.DataContractJsonSerializer(typeof(T)); foreach (T city in vals) {
using (System.IO.MemoryStream ms = new System.IO.MemoryStream()) {
s.WriteObject(ms, city);
st.Append(System.Text.Encoding.UTF8.GetString(ms.ToArray()));
}
}
} catch (Exception ex) {
throw ex;
} return st.ToString();
}
/// <summary>
/// JSON格式字符转换为T类型的对象
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="jsonStr"></param>
/// <returns></returns>
public static T ParseFormByJson<T>(string jsonStr) {
T obj = Activator.CreateInstance<T>();
using (System.IO.MemoryStream ms =
new System.IO.MemoryStream(System.Text.Encoding.UTF8.GetBytes(jsonStr))) {
System.Runtime.Serialization.Json.DataContractJsonSerializer serializer =
new System.Runtime.Serialization.Json.DataContractJsonSerializer(typeof(T));
return (T)serializer.ReadObject(ms);
}
} public static string JSON1<SendData>(List<SendData> vals) {
System.Text.StringBuilder st = new System.Text.StringBuilder();
try {
System.Runtime.Serialization.Json.DataContractJsonSerializer s = new System.Runtime.Serialization.Json.DataContractJsonSerializer(typeof(SendData)); foreach (SendData city in vals) {
using (System.IO.MemoryStream ms = new System.IO.MemoryStream()) {
s.WriteObject(ms, city);
st.Append(System.Text.Encoding.UTF8.GetString(ms.ToArray()));
}
}
} catch (Exception ex) {
throw ex;
} return st.ToString();
} }
}