For example, someone sends a picture of our public micro-signal, and then we process the photo, such as OCR recognition (and then down to this example), or face recognition, or photo forensics, which is quite useful. Then we need to analyze this process now. Micro-letter platform must not help us OCR or face recognition functions, to do these functions first to get pictures! The photos taken by the user were first uploaded to the wenxin server, and then there was a mediaid that we could use to download to our own server and then process the results to the micro-trust platform, which is ultimately fed back to the user (the followers). The micro-letter development document has been given a way to download resources, which I have transformed into. NET, as follows:
<SUMMARY>///Download Save multimedia file, return to multimedia save path///</SUMMARY>///<param name= "Access_token" ></param >///<param name= "media_id" ></PARAM>///<RETURNS></RETURNS> public string getmultime Dia (String Access_token, String media_id) {string file = string.
Empty; String content = String.
Empty; String strpath = String.
Empty; String Savepath = String.
Empty; String sturl = "http://file.api.weixin.qq.com/cgi-bin/media/get?access_token=" + Access_token + "&media_id=" +
media_id;
HttpWebRequest req = (HttpWebRequest) httpwebrequest.create (Sturl); Req.
method = ' Get '; using (WebResponse WR = req. GetResponse ()) {HttpWebResponse Myresponse = (httpwebresponse) req.
GetResponse ();
strpath = MyResponse.ResponseUri.ToString ();
Writelog ("Receive Category://" + Myresponse.contenttype);
WebClient mywebclient = new WebClient (); Savepath = Server.MapPath ("image") + "\" + DatETime.Now.ToString ("yyyymmddhhmmssfff") + (new Random ()). Next (). ToString ().
Substring (0, 4) + ". jpg";
Writelog ("Path://" + Savepath); try {mywebclient.
DownloadFile (strpath, Savepath);
File = Savepath; The catch (Exception ex) {Savepath = ex.
ToString ();
} return file;
}
The above two parameters are well understood, the first is Access_token, said a lot before, the second is in the Micro-trust server resource ID, that is, MediaID. If we want to download the resources on the micro-trust server, we always need to know the ID. But how did media_id come into being? I first change the message entity class before, add the MediaID property
Class Wxmessage
{public
string Fromusername {get; set;}
public string Tousername {get; set;}
public string Msgtype {get; set;}
public string EventName {get; set;}
public string Content {get; set;}
public string Recognition {get; set;}
public string MediaId {get; set;}
public string Eventkey {get; set;}
}
Then change the Getwxmessage () and assign the MediaID to the value
Private Wxmessage Getwxmessage () {wxmessage wx = new Wxmessage ();
StreamReader str = new StreamReader (Request.inputstream, System.Text.Encoding.UTF8);
XmlDocument xml = new XmlDocument (); Xml.
Load (str); Wx. Tousername = XML. selectSingleNode ("xml"). selectSingleNode ("Tousername").
InnerText; Wx. Fromusername = XML. selectSingleNode ("xml"). selectSingleNode ("Fromusername").
InnerText; Wx. Msgtype = XML. selectSingleNode ("xml"). selectSingleNode ("Msgtype").
InnerText; if (WX). Msgtype.trim () = = "Text") {WX. Content = XML. selectSingleNode ("xml"). selectSingleNode ("Content").
InnerText; } if (WX. Msgtype.trim () = = "Event") {WX. EventName = XML. selectSingleNode ("xml"). selectSingleNode ("Event").
InnerText; Wx. Eventkey = XML. selectSingleNode ("xml"). selectSingleNode ("Eventkey").
InnerText; } if (WX. Msgtype.trim () = = "Voice") {WX. Recognition = XML. selectSingleNode ("xml"). SelectsinglenodE ("Recognition").
InnerText; } if (WX. Msgtype.trim () = = "image") {wx. MediaId = XML. selectSingleNode ("xml"). selectSingleNode ("MediaId").
InnerText;
return WX; }
If we are modifying the message accepted code, we can do it, the customer sends a photo to the micro-platform, the program detects the picture, and then, according to MediaID, calls the Getmultimedia method to download the picture to its own server. The work behind, you want to do what.
The example just described is the user (the concern), send a picture, and then through the micro-letter platform to our server, there is a situation, the user sent a user name: such as "Hemeng", and then I need to call the existing server in the Hemeng avatar picture feedback to the user, this how to do? How do we pass our pictures to the micro-trust platform and pass it on to the user? We used the Upload method:
<SUMMARY>///upload multimedia file, return MediaId///</SUMMARY>///<param name= "Access_token" ></param&
Gt <param name= "Type" ></PARAM>///<RETURNS></RETURNS> public string Uploadmultimedia (Strin
G Access_token, String Type) {string result = ' ";
String wxurl = "http://file.api.weixin.qq.com/cgi-bin/media/upload?access_token=" + Access_token + "&type=" + type;
string filepath = Server.MapPath ("image") + "\\hemeng80.jpg";(the address of the local server) writelog ("Upload path:" + filepath);
WebClient mywebclient = new WebClient ();
Mywebclient.credentials = CredentialCache.DefaultCredentials;
try {byte[] Responsearray = Mywebclient.uploadfile (Wxurl, "POST", filepath);
result = System.Text.Encoding.Default.GetString (Responsearray, 0, responsearray.length);
Writelog ("Upload result:" + result);
UPLOADMM _mode = jsonhelper.parsefromjson<uploadmm> (result);
result = _mode.media_id;catch (Exception ex) {result = ' Error: ' + ex.
message;
} writelog ("Upload mediaid:" + result);
return result; }
The second parameter, if it is a picture "image", can refer to the document of the micro-letter. The return value of the function is a mediaid so that you can use the function that sends the picture, send to the customer, the function that sends the picture is as follows:
Protected string Sendpictextmessage (Msg _mode, string MediaId)
{
string res = string. Format (@ "<xml>
<tousername><![ Cdata[{0}]]></tousername>
<fromusername><![ cdata[{1}]]></fromusername>
<CreateTime>{2}</CreateTime>
<msgtype><![ cdata[image]]></msgtype>
<Image>
<mediaid><![ cdata[{3}]]></mediaid>
</Image>
</xml> ",
_mode. Fromusername, _mode. Tousername, DateTime.Now, MediaId);
return res;
}
Other video, voice operations are similar, it is no longer redundant introduction. With this knowledge, can we do a lot of application? Of course yes, but our code is not optimized, the structure is unreasonable, do not worry, we will gradually introduce, because we have not fully understand the powerful function of micro-letter.
The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.