public void ReceiveInstruct(string key,string clientUrl, InstructProxy[] instructs) { string instructXML = Serializer.GetXML(instructs); WebRequest request = HttpWebRequest.Create(clientUrl); request.Method = "POST"; request.ContentType = "application/octet-stream"; request.Headers.Add("key", key); using (Stream stream = request.GetRequestStream()) { byte[] contents = Encoding.UTF8.GetBytes(instructXML); stream.Write(contents, 0, contents.Length); stream.Close(); } WebResponse response = request.GetResponse(); using(StreamReader reader = new StreamReader( response.GetResponseStream(), Encoding.UTF8)) { string responseText = reader.ReadToEnd(); } response.Close(); }
/// <summary> /// 发送指令 /// </summary> /// <param name="ins"></param> private bool SendInstruct(Instruct[] ins, bool isSync) { if (IsDisposed) return false; watch.Reset(); watch.Start(); InstructProxy[] instructs = new InstructProxy[ins.Length]; int i = 0; //转换 foreach (Instruct instruct in ins) { InstructProxy clientInstruct = new InstructProxy(); clientInstruct.InstructID = instruct.InstructID; if (instruct.Datas != null) { clientInstruct.Datas = instruct.Datas.Trim('\0'); } else { clientInstruct.Datas = string.Empty; } clientInstruct.InstructType = (int)instruct.InstructType; clientInstruct.CreateDateTime = instruct.CreateDate; clientInstruct.TargetID = instruct.TargetID; instructs[i++] = clientInstruct; } try { ClientService.ReceiveInstruct(this.Client.AccessKey,this.Client.APIUrl, instructs); } //catch (SoapException ex)//客户端异常, 跳过 //{ // // if (isSync) // throw; //} catch (WebException ex) { bool skeepIns = false; HttpWebResponse response = ex.Response as HttpWebResponse; if (response != null) { switch (response.StatusCode) { case HttpStatusCode.InternalServerError: //500 skeepIns = true; LogHelper.CreateLog(ex, ex.Message + "(指令被跳过)", string.Format("passport_{0}_Exception_{1}.log", this.Client.ClientID, DateTime.Now.ToString("yyyyMMdd"))); break; } } if (isSync) throw; if (!skeepIns) { watch.Reset(); return false; } } SendCount++; watch.Stop(); ElapsedTime += watch.Elapsed.TotalMilliseconds; #if DEBUG //客户端API延时监控记录部分 if (watch.ElapsedMilliseconds > 500) { string msg =string.Concat( "InstructType = "+ins[0].InstructType,"\r\n" , "Time = ", watch.Elapsed.TotalSeconds, "\r\n" , " ClientID=", this.Client.ClientID,"\r\n"); LogHelper.CreateLog(null, msg, string.Format("InstructCall_{0}.txt", DateTime.Now.ToString("yyyy-MM-dd"))); } #endif return true; }