c# class中接口访问的类,使用http方式

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

using System.IO;

using System.Net;

using Newtonsoft.Json.Linq;

using Newtonsoft.Json;

namespace MIAS_DigitalTool_Platform.Class

{

public class HttpClient

{

/// <summary>

/// Seivice URL

/// </summary>

public string Url { get; set; }

/// <summary>

/// 内容

/// </summary>

public string Content { get; set; }

/// <summary>

/// Cookie,保证登录后,所有访问持有一个Cookie;

/// </summary>

static CookieContainer Cookie = new CookieContainer();

/// <summary>

/// HTTP访问

/// </summary>

public string AsyncRequest()

{

HttpWebRequest httpRequest = HttpWebRequest.Create(Url) as HttpWebRequest;

httpRequest.Method = "POST";

httpRequest.ContentType = "application/json";

httpRequest.CookieContainer = Cookie;

httpRequest.Timeout = 1000 * 60 * 10;//10min

using (Stream reqStream = httpRequest.GetRequestStream())

{

JObject jObj = new JObject();

jObj.Add("format", 1);

jObj.Add("useragent", "ApiClient");

jObj.Add("rid", Guid.NewGuid().ToString().GetHashCode().ToString());

jObj.Add("parameters", Content);

jObj.Add("timestamp", DateTime.Now);

jObj.Add("v", "1.0");

string sContent = jObj.ToString();

var bytes = UnicodeEncoding.UTF8.GetBytes(sContent);

reqStream.Write(bytes, 0, bytes.Length);

reqStream.Flush();

}

using (var repStream = httpRequest.GetResponse().GetResponseStream())

{

using (var reader = new StreamReader(repStream))

{

return ValidateResult(reader.ReadToEnd());

}

}

}

private static string ValidateResult(string responseText)

{

if (responseText.StartsWith("response_error:"))

{

return responseText.TrimStart("response_error:".ToCharArray());

}

return responseText;

}

}

}

如下是实际代码中调用上述类方法:

#region 到金蝶ERP中查询规格型号以作信息比对

Class.HttpClient httpClient = new Class.HttpClient();

httpClient.Url = "http://10.3.10.10:9000/k3cloud/Kingdee.BOS.WebApi.ServicesStub.AuthService.ValidateUser.common.kdsvc";

List<object> Parameters = new List<object>();

Parameters.Add("66c541f8cc1e6a");//帐套Id

Parameters.Add("李");//用户名

Parameters.Add("Mis@0123456789");//密码

Parameters.Add(2052);

httpClient.Content = JsonConvert.SerializeObject(Parameters);

var iResult = JObject.Parse(httpClient.AsyncRequest())["LoginResultType"].Value<int>();

if (iResult == 1)

{

//todo:验证成功,处理业务

httpClient.Url = "http://10.3.10.10:9000/k3cloud/Kingdee.BOS.WebApi.ServicesStub.DynamicFormService.View.common.kdsvc";

//业务对象Id

Parameters.Clear();

String formid = "BD_MATERIAL";//物料列表

Parameters.Add(formid);

//Json字串

string data = "{\"Number\":\"24-1508\"}";

Parameters.Add(data);

httpClient.Content = JsonConvert.SerializeObject(Parameters);

// 发送请求并等待响应

string responseBody = httpClient.AsyncRequest();

// 解析响应体

JObject jsonResponse = JObject.Parse(responseBody);

// 获取嵌套的 result 下的 result 下的 specification 值

JToken specificationToken = jsonResponse["Result"]["Result"]["Specification"][0]["Value"];

// 将JToken转换为字符串(如果需要)

string specificationValue = specificationToken.ToString();

if (specificationToken != null)

{

string specification = specificationToken.ToString();

// 处理 specification 值

}

else

{

MessageBox.Show("这个项目号在金蝶中没有找到对应的规格型号信息,请到ERP中确认,以防ERP/PDM系统中信息不同步");return;

}

}

else

{

MessageBox.Show("访问ERP受阻,请联系技术中心 Frank Li 确认此问题");return;

}

#endregion

相关推荐
flysh05几秒前
C# 中类型转换与模式匹配核心概念
开发语言·c#
AC赳赳老秦几秒前
Python 爬虫进阶:DeepSeek 优化反爬策略与动态数据解析逻辑
开发语言·hadoop·spring boot·爬虫·python·postgresql·deepseek
浩瀚之水_csdn2 分钟前
Python 三元运算符详解
开发语言·python
源代码•宸27 分钟前
GoLang八股(Go语言基础)
开发语言·后端·golang·map·defer·recover·panic
rit843249936 分钟前
基于MATLAB的SUSAN特征检测算子边缘提取实现
开发语言·matlab
g***557538 分钟前
Java高级开发进阶教程之系列
java·开发语言
鲁正杰1 小时前
【运维部署】现代化内网穿透与文件共享方案 (Rust)
运维·开发语言·rust
2401_876907521 小时前
USB TYPE-C 公头连接器设计规范总结:提升可靠性、降本增效的关键指南
c语言·开发语言·设计规范
额呃呃2 小时前
std::allocator<T>::destroy
开发语言
期待のcode2 小时前
Java虚拟机栈
java·开发语言·jvm