The official interface instructions and examples are java-based, but without a more detailed description of the specific header, making it difficult to use C # Language conversion, after testing, finally found a not too complete solution, the code is as follows.
<summary>///post file///</summary>///<param name= "url" ></param> <param name= "Timeout" ></param>///<param name= "Filekeyname" > such as Staples the media file is used for the server The data associated with the KeyName is read when the data is received. </param>///<param name= "Filebuffer" > File data </param>///<param name= "filename" > FileName &
lt;/param>///<returns></returns> public static string Post (string URL,
String filekeyname, byte[] Filebuffer, String fileName, int timeout) {var boundary = Securityhelper.gene
Rateradomstr ();
WebClient WebClient = new WebClient (); WEBCLIENT.HEADERS.ADD ("Content-type", String. Format ("Multipart/form-data;
boundary={0} ", boundary));
String fileformdatatemplate = "\r\n--" + boundary + "\r\ncontent-disposition:form-data;name=\" {0}\; filename=\ "{1}\" "
+ "\r\ncontent-type:application/octet-stream" + "\r\n\r\n";
String formdataheader = String.Format (fileformdatatemplate, "media", fileName);
byte[] Formdataheaderbuffer = Encoding.UTF8.GetBytes (Formdataheader);
String begin = $ "--{boundary}\r\n";
byte[] Beginbuffer = Encoding.UTF8.GetBytes (begin);
String end = $ "\r\n--{boundary}--\r\n";
byte[] Endbuffer = Encoding.UTF8.GetBytes (end);;
byte[] DataStream = new Byte[formdataheaderbuffer.length + beginbuffer.length + filebuffer.length + endbuffer.length];
Formdataheaderbuffer.copyto (dataStream, 0);
Beginbuffer.copyto (DataStream, formdataheaderbuffer.length); Filebuffer.copyto (DataStream, Formdataheaderbuffer.length + begin.
Length);Endbuffer.copyto (DataStream, Formdataheaderbuffer.length + begin.
Length + filebuffer.length);
var returnbuffer = webclient.uploaddata (URL, "POST", DataStream);
Encoding encode = Encoding.UTF8; String Resultjson = encode.
GetString (Returnbuffer);
return Resultjson;
}
The code invoked
int timeout = 1000 * 5;
String Resultjson = Requesthelper.post (Requesturl, "Media", Filebuffer, FileName, timeout);//media is a fixed string
Where Filebuffer as a file of the word throttling, requesturl in accordance with the way the interface to find https://oapi.dingtalk.com/media/upload?access_token=access_token& Type=type.
Currently measured, type=file is feasible, type=image when I do not know why always prompts the "system busy", but also pass the people who can provide solutions.
After successful upload, you need to download, download, due to the success and failure of the return data is not the same, so you need to first a number of bytes before the trial processing, and then continue to deal with the test results, the code is as follows
Attach a method for reading media files #region fetchmediafile Function///<summary>///Get media files///</summary> <param name= "MediaId" > Media file id</param>///<returns></returns> public STA
Tic Ddmediafetchresult fetchmediafile (string mediaId) {Ddmediafetchresult result = null;
String apiurl = Formatapiurlwithtoken (urls.media_get);
Apiurl = $ "{apiurl}&{keys.media_id}={mediaid}";
WebClient WebClient = new WebClient ();
var data = Webclient.downloaddata (Apiurl);
int testheadermaxlength = 100; var testheaderbuffer = new byte[(data. Length < testheadermaxlength? Data.
Length:testheadermaxlength)];
Array.copy (data, 0, Testheaderbuffer, 0, testheaderbuffer.length);
Encoding encoder = Encoding.UTF8; String testheaderstr = encoder.
GetString (Testheaderbuffer); if (TestheaderStr.startswith ("--)") {//Normal return data, the first row of data is the dividing line, and the dividing line must be "--" start.
var Temparr = Testheaderstr.split (new string[] {Environment.NewLine}, Stringsplitoptions.none);
string boundary = Temparr[0] + Environment.NewLine; int boundarybytelength = encoder. GetBytes (boundary).
Length; byte[] Destdata = new Byte[data.
Length-boundarybytelength];
array.copy (data, boundarybytelength, Destdata, 0, destdata.length);
result = new Ddmediafetchresult (); result.
Errcode = Dderrcodeenum.ok; result.
ErrMsg = "OK"; result.
Data = Destdata;
Const string content_length = "Content-length"; if (webclient.responseheaders = null | |
(!webclient.responseheaders.allkeys.contains (CONTENT_LENGTH)) {Result.
Filelength =-1;
var lengthstr = webclient.responseheaders[content_length];
int length = 0; if (int. TryParse (lengthstr, out length)} {result.
filelength = length; else {result.
filelength = 0;
Const string content_type = "Content-type"; if (webclient.responseheaders = null | |
(!webclient.responseheaders.allkeys.contains (Content_Type)) {result.
FileType = "Unknown"; else {result.
FileType = Webclient.responseheaders[content_type]; } else {string Resultjson = Encoder.
GetString (data); result = Ddrequestanalyzer.analyzeresult<ddmediafetchresult> (Resultjson);//Will Resultjson againstOrder to Ddmediafetchresult} return result;
#endregion///<summary>///media file Get results///</summary> public class Ddmediafetchresult
{///<summary>///error code///</summary> public int errcode{get;set;
<summary>///error message///</summary> public string errmsg {get; set;}} <summary>///HTTP response header///</summary> public dictionary<string, string> Heade
r {get; set;}
<summary>///obtained data///</summary> public byte[] Data {get; set;}
<summary>///file length///</summary> public int filelength {get; set;}
<summary>///File type///</summary> public String FileType {get; set;} }
To save the received data into a file, such as the previous upload of the mediaid passed in, download the following figure
A method for generating random strings
#region generateradomstr
///<summary>
///generate random strings
///</summary>
///<param name= " Length "> Random string Lengths </param>
///<returns></returns> public
static string Generateradomstr ( int length = =
{
string chars = ' abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789 ';
String str = "";
Random rad = new Random ();
for (int i = 0; i < length; i++)
{
str + chars. Substring (RAD. Next (0, chars. Length-1), 1);
}
return str;
}
#endregion
Welcome to the left two-dimensional code to play rewards.
Reprint please indicate the source.