泛微OA的第三方异构系统调用流程,请提供c#的演示代码
时间: 2024-02-22 15:00:25 浏览: 281
泛微OA的第三方异构系统调用流程通常包括以下步骤:
1. 获取Token:首先需要获取Token,通过以下API接口获取Token
```
http://服务器地址:端口号/weaver/batch/authentication.do
```
请求方式:POST
请求参数:
```
{
"params": {
"language": "7",
"loginid": "账号",
"password": "密码",
"useragent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.146 Safari/537.36",
"domain": "泛微OA的域名"
},
"requestid": "1",
"serviceid": "rest",
"ticket": "null"
}
```
2. 调用API:获取Token后,就可以调用API接口了。通过以下API接口调用。
```
http://服务器地址:端口号/rest/workflow/request/create.action
```
请求方式:POST
请求参数:
```
{
"requestname": "测试流程",
"workflowid": "流程编号",
"operatorid": "操作人编号",
"csrftoken": "token",
"formdata": [{
"fieldid": "字段ID",
"fieldvalue": "字段值"
}, {
"fieldid": "字段ID",
"fieldvalue": "字段值"
}],
"attachments": [{
"filename": "文件名",
"filebody": "文件内容",
"filesize": "文件大小"
}]
}
```
其中,需要替换的参数有:
- 服务器地址:泛微OA服务器地址
- 端口号:泛微OA端口号
- 账号:泛微OA登录账号
- 密码:泛微OA登录密码
- 泛微OA的域名:泛微OA的域名
- 流程编号:泛微OA中指定的流程编号
- 操作人编号:泛微OA中指定的操作人编号
- token:获取到的Token
- 字段ID:泛微OA中定义的字段ID
- 字段值:需要提交的字段值
- 文件名:需要提交的文件名
- 文件内容:需要提交的文件内容
- 文件大小:需要提交的文件大小
演示代码如下:
```csharp
using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Text;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
// 获取Token
string token = GetToken("账号", "密码", "泛微OA的域名");
// 调用API
string result = CallAPI(token, "流程编号", "操作人编号", "字段ID", "字段值");
Console.WriteLine(result);
}
// 获取Token
static string GetToken(string loginid, string password, string domain)
{
string url = "http://服务器地址:端口号/weaver/batch/authentication.do";
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Method = "POST";
request.ContentType = "application/json;charset=UTF-8";
string postData = "{\"params\":{\"language\":\"7\",\"loginid\":\"" + loginid + "\",\"password\":\"" + password + "\",\"useragent\":\"Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.146 Safari/537.36\",\"domain\":\"" + domain + "\"},\"requestid\":\"1\",\"serviceid\":\"rest\",\"ticket\":\"null\"}";
byte[] bytes = Encoding.UTF8.GetBytes(postData);
request.ContentLength = bytes.Length;
Stream stream = request.GetRequestStream();
stream.Write(bytes, 0, bytes.Length);
stream.Close();
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream responseStream = response.GetResponseStream();
StreamReader reader = new StreamReader(responseStream, Encoding.UTF8);
string result = reader.ReadToEnd();
reader.Close();
responseStream.Close();
response.Close();
int start = result.IndexOf("csrfToken\":\"") + 12;
int end = result.IndexOf("\"", start);
string token = result.Substring(start, end - start);
return token;
}
// 调用API
static string CallAPI(string token, string workflowid, string operatorid, string fieldid, string fieldvalue)
{
string url = "http://服务器地址:端口号/rest/workflow/request/create.action";
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Method = "POST";
request.ContentType = "application/json;charset=UTF-8";
request.Headers.Add("X-CSRF-TOKEN", token);
string postData = "{\"requestname\":\"测试流程\",\"workflowid\":\"" + workflowid + "\",\"operatorid\":\"" + operatorid + "\",\"csrftoken\":\"" + token + "\",\"formdata\":[{\"fieldid\":\"" + fieldid + "\",\"fieldvalue\":\"" + fieldvalue + "\"}],\"attachments\":[]}";
byte[] bytes = Encoding.UTF8.GetBytes(postData);
request.ContentLength = bytes.Length;
Stream stream = request.GetRequestStream();
stream.Write(bytes, 0, bytes.Length);
stream.Close();
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream responseStream = response.GetResponseStream();
StreamReader reader = new StreamReader(responseStream, Encoding.UTF8);
string result = reader.ReadToEnd();
reader.Close();
responseStream.Close();
response.Close();
return result;
}
}
}
```
注意:代码中的参数需要根据实际情况进行替换。
阅读全文