如何详细解析HttpHelper类调用方法?
- 内容介绍
- 文章标签
- 相关推荐
本文共计1111个文字,预计阅读时间需要5分钟。
javapublic class HttpHelper { // ... // summary Http操作类 // summary public static void main(String[] args) { // 本文本示例为使用HttpHelper类的例子,以下是大体内容 // 首先,列出HttpHelper类,然后是summary注释,表示Http操作类 // 最后是HttpHelper类的public构造函数定义 // public class HttpHelper { // private static log4net.ILog mLog=log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); // // ... // } } // ...}
本文实例为大家分享了HttpHelper类的方法使用,供大家参考,具体内容如下
首先列出HttpHelper类
/// <summary> /// Http操作类 /// </summary> public class HttpHelper { private static log4net.ILog mLog = log4net.LogManager.GetLogger("HttpHelper"); [DllImport("wininet.dll", CharSet = CharSet.Auto, SetLastError = true)] public static extern bool InternetSetCookie(string lpszUrlName, string lbszCookieName, string lpszCookieData); [DllImport("wininet.dll", CharSet = CharSet.Auto, SetLastError = true)] public static extern bool InternetGetCookie(string lpszUrlName, string lbszCookieName, StringBuilder lpszCookieData, ref int lpdwSize); public static StreamReader mLastResponseStream = null; public static System.IO.StreamReader LastResponseStream { get { return mLastResponseStream; } } private static CookieContainer mCookie = null; public static CookieContainer Cookie { get { return mCookie; } set { mCookie = value; } } private static CookieContainer mLastCookie = null; public static HttpWebRequest CreateWebRequest(string url, HttpRequestType i.cnblogs.com"; string xml = HttpHelper.SendRequest(url, HttpRequestType.POST, null, null, Encoding.UTF8, Encoding.UTF8); NormalResponse nr = HuaweiXMLHelper.GetNormalResponse(xml); if (nr.Code == "0") { HttpHelper.SetHttpCookie(HttpHelper.GetLastCookie()); mIsConnect = true; return true; } else { mIsConnect = false; return false; } } catch (System.Exception ex) { mIsConnect = false; return false; } }
2.带参数调用
private bool HandleIntelligentTask(string taskId,bool bStop) { try { if (!mIsConnect) { return false; } StringBuilder sb = new StringBuilder(); sb.AppendFormat("<request>\r\n"); sb.AppendFormat("<task_id>{0}</task_id>\r\n", taskId);//<!-- task-id为调用方生成的UUID或其它串 --> sb.AppendFormat("<status>{0}</status>\r\n",bStop?0:1); sb.AppendFormat("</request>\r\n"); string xml = sb.ToString(); string url = mIAServerUrl + "/sdk_service/rest/video-analysis/handle-intelligent-analysis"; string xml2 = HttpHelper.SendRequest(url, HttpRequestType.POST, "text/plain;charset=utf-8", xml, Encoding.UTF8, Encoding.UTF8); NormalResponse nr = HuaweiXMLHelper.GetNormalResponse(xml2); if (nr.Code == "0") { return true; } else { return false; } } catch (System.Exception ex) { return false; } }
3.异步调用
private void ReStartAlarmServer(List<string> list, string alarmUrl, Thread[] listThread) { StopAlarm(alarmUrl, listThread); listThread[0]= new Thread(new ThreadStart(delegate() { try { if (!mIsConnect) { mLog.Error("未登录!--ReStartAlarmServer-结束!"); return; } mLog.Info("ReStartAlarmServer开始报警连接...."); if (String.IsNullOrWhiteSpace(alarmUrl)) return; mLog.InfoFormat("ReStartAlarmServer请求报警:URL={0}", alarmUrl); string xml = "task-id=0"; string xml2 = HttpHelper.SendRequest(alarmUrl, HttpRequestType.POST, "application/x-www-form-urlencoded", xml, Encoding.UTF8, Encoding.UTF8, AlarmCallBack); mLog.Info("ReStartAlarmServer报警连接成功!"); } catch (System.Threading.ThreadAbortException ex) { mLog.Info("ReStartAlarmServer线程已人为终止!" + ex.Message, ex); } catch (System.Exception ex) { mLog.Error("ReStartAlarmServer开始报警连接失败:" + ex.Message, ex); mLog.Info("ReStartAlarmServer开始重新报警连接...."); mTimes = 50; } finally { } })); listThread[0].IsBackground = true; listThread[0].Start(); } private void AlarmCallBack(IAsyncResult ir) { try { HttpWebRequest webRequest = (HttpWebRequest)ir.AsyncState; string salarmUrl = webRequest.Address.OriginalString; Thread[] alarmThead = dicAlarmUrls[salarmUrl]; HttpWebResponse response = (HttpWebResponse)webRequest.EndGetResponse(ir); Stream stream = response.GetResponseStream(); alarmThead[1]= new Thread(new ThreadStart(delegate() { try { byte[] buffer = new byte[mAlarmReadCount]; int count = 0; string strMsg = ""; int startIndex = -1; int endIndex = -1; NormalResponse res = null; DateTime dtStart = DateTime.Now; DateTime dtEnd = DateTime.Now; while (!mIsCloseAlarm) { count = stream.Read(buffer, 0, mAlarmReadCount); if (count > 0) { strMsg += Encoding.UTF8.GetString(buffer, 0, count); startIndex = strMsg.IndexOf("<response>"); endIndex = strMsg.IndexOf("</response>"); string xml = strMsg.Substring(startIndex, endIndex - startIndex + "</response>".Length); res = HuaweiXMLHelper.GetNormalResponse(xml); strMsg = strMsg.Substring(endIndex + "</response>".Length); startIndex = -1; endIndex = -1; break; } dtEnd = DateTime.Now; if ((dtEnd - dtStart).TotalSeconds > 10) { throw new Exception("连接信息未有获取到,需要重启报警!"); } } while (!mIsCloseAlarm) { count = stream.Read(buffer, 0, mAlarmReadCount); if (count > 0) { string temp = Encoding.UTF8.GetString(buffer, 0, count); strMsg += temp; while (strMsg.Length > 0) { if (startIndex == -1)//未发现第一个<task-info> { startIndex = strMsg.IndexOf("<task-info>"); if (startIndex == -1) { if (strMsg.Length >= "<task-info>".Length) { strMsg = strMsg.Substring(strMsg.Length - "<task-info>".Length); } break; } } if (startIndex >= 0) { int i = startIndex + "<task-info>".Length; int taskInfoEndIndex = strMsg.IndexOf("</task-info>", i); if (taskInfoEndIndex > 0)//必须有任务结束节点 { i = taskInfoEndIndex + "</task-info>".Length; int i1 = strMsg.IndexOf("</attach-rules>", i);//找到轨迹节点结束 int i2 = strMsg.IndexOf("</alarm>", i);//找到报警节点结束,发现一条报警 if (i1 == -1 && i2 == -1)//没有标志结束 { break; } else if (i1 >= 0 && (i1 < i2 || i2 == -1))//找到轨迹结束节点 { strMsg = strMsg.Substring(i1 + "</attach-rules>".Length); startIndex = -1; endIndex = -1; continue; } else if (i2 > 0 && (i2 < i1 || i1 == -1))//找报警节点 { endIndex = i2;//找到报警节点结束,发现一条报警 string alarmXml = "<taskalarm>" + strMsg.Substring(startIndex, endIndex - startIndex + "</alarm>".Length) + "</taskalarm>"; Thread th = new Thread(new ThreadStart(delegate() { ParseAlarmXml(alarmXml); })); th.IsBackground = true; th.Start(); strMsg = strMsg.Substring(endIndex + "</alarm>".Length); startIndex = -1; endIndex = -1; continue; } } else { break; } } } } else { Console.WriteLine("##########读取报警反馈:无"); Thread.Sleep(1000); } } } catch (System.Threading.ThreadAbortException ex) { mLog.Info("AlarmCallBack...7"); try { if (stream != null) { stream.Close(); stream.Dispose(); response.Close(); } } catch { } mLog.Info("AlarmCallBack线程已人为终止!--0" + ex.Message, ex); } catch(IOException ex) { mLog.Info("AlarmCallBack...8"); try { if (stream != null) { stream.Close(); stream.Dispose(); response.Close(); } } catch { } } catch (ObjectDisposedException ex) { mLog.Info("AlarmCallBack...9"); mLog.Info("AlarmCallBack读取流已人为终止!--2" + ex.Message, ex); try { if (stream != null) { stream.Close(); stream.Dispose(); response.Close(); } } catch { } } catch (System.Exception ex) { mLog.Info("AlarmCallBack...10"); mLog.Error("AlarmCallBack 0:" + ex.Message,ex); try { if (stream != null) { stream.Close(); stream.Dispose(); response.Close(); } } catch { } } finally { } })); alarmThead[1].IsBackground = true; alarmThead[1].Start(); } catch (System.Exception ex) { mLog.Info("AlarmCallBack...11"); mLog.Error("AlarmCallBack 1:" + ex.Message,ex); mLog.Info("AlarmCallBack开始重新报警连接....3"); mTimes = 50; } finally { } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持易盾网络。
本文共计1111个文字,预计阅读时间需要5分钟。
javapublic class HttpHelper { // ... // summary Http操作类 // summary public static void main(String[] args) { // 本文本示例为使用HttpHelper类的例子,以下是大体内容 // 首先,列出HttpHelper类,然后是summary注释,表示Http操作类 // 最后是HttpHelper类的public构造函数定义 // public class HttpHelper { // private static log4net.ILog mLog=log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); // // ... // } } // ...}
本文实例为大家分享了HttpHelper类的方法使用,供大家参考,具体内容如下
首先列出HttpHelper类
/// <summary> /// Http操作类 /// </summary> public class HttpHelper { private static log4net.ILog mLog = log4net.LogManager.GetLogger("HttpHelper"); [DllImport("wininet.dll", CharSet = CharSet.Auto, SetLastError = true)] public static extern bool InternetSetCookie(string lpszUrlName, string lbszCookieName, string lpszCookieData); [DllImport("wininet.dll", CharSet = CharSet.Auto, SetLastError = true)] public static extern bool InternetGetCookie(string lpszUrlName, string lbszCookieName, StringBuilder lpszCookieData, ref int lpdwSize); public static StreamReader mLastResponseStream = null; public static System.IO.StreamReader LastResponseStream { get { return mLastResponseStream; } } private static CookieContainer mCookie = null; public static CookieContainer Cookie { get { return mCookie; } set { mCookie = value; } } private static CookieContainer mLastCookie = null; public static HttpWebRequest CreateWebRequest(string url, HttpRequestType i.cnblogs.com"; string xml = HttpHelper.SendRequest(url, HttpRequestType.POST, null, null, Encoding.UTF8, Encoding.UTF8); NormalResponse nr = HuaweiXMLHelper.GetNormalResponse(xml); if (nr.Code == "0") { HttpHelper.SetHttpCookie(HttpHelper.GetLastCookie()); mIsConnect = true; return true; } else { mIsConnect = false; return false; } } catch (System.Exception ex) { mIsConnect = false; return false; } }
2.带参数调用
private bool HandleIntelligentTask(string taskId,bool bStop) { try { if (!mIsConnect) { return false; } StringBuilder sb = new StringBuilder(); sb.AppendFormat("<request>\r\n"); sb.AppendFormat("<task_id>{0}</task_id>\r\n", taskId);//<!-- task-id为调用方生成的UUID或其它串 --> sb.AppendFormat("<status>{0}</status>\r\n",bStop?0:1); sb.AppendFormat("</request>\r\n"); string xml = sb.ToString(); string url = mIAServerUrl + "/sdk_service/rest/video-analysis/handle-intelligent-analysis"; string xml2 = HttpHelper.SendRequest(url, HttpRequestType.POST, "text/plain;charset=utf-8", xml, Encoding.UTF8, Encoding.UTF8); NormalResponse nr = HuaweiXMLHelper.GetNormalResponse(xml2); if (nr.Code == "0") { return true; } else { return false; } } catch (System.Exception ex) { return false; } }
3.异步调用
private void ReStartAlarmServer(List<string> list, string alarmUrl, Thread[] listThread) { StopAlarm(alarmUrl, listThread); listThread[0]= new Thread(new ThreadStart(delegate() { try { if (!mIsConnect) { mLog.Error("未登录!--ReStartAlarmServer-结束!"); return; } mLog.Info("ReStartAlarmServer开始报警连接...."); if (String.IsNullOrWhiteSpace(alarmUrl)) return; mLog.InfoFormat("ReStartAlarmServer请求报警:URL={0}", alarmUrl); string xml = "task-id=0"; string xml2 = HttpHelper.SendRequest(alarmUrl, HttpRequestType.POST, "application/x-www-form-urlencoded", xml, Encoding.UTF8, Encoding.UTF8, AlarmCallBack); mLog.Info("ReStartAlarmServer报警连接成功!"); } catch (System.Threading.ThreadAbortException ex) { mLog.Info("ReStartAlarmServer线程已人为终止!" + ex.Message, ex); } catch (System.Exception ex) { mLog.Error("ReStartAlarmServer开始报警连接失败:" + ex.Message, ex); mLog.Info("ReStartAlarmServer开始重新报警连接...."); mTimes = 50; } finally { } })); listThread[0].IsBackground = true; listThread[0].Start(); } private void AlarmCallBack(IAsyncResult ir) { try { HttpWebRequest webRequest = (HttpWebRequest)ir.AsyncState; string salarmUrl = webRequest.Address.OriginalString; Thread[] alarmThead = dicAlarmUrls[salarmUrl]; HttpWebResponse response = (HttpWebResponse)webRequest.EndGetResponse(ir); Stream stream = response.GetResponseStream(); alarmThead[1]= new Thread(new ThreadStart(delegate() { try { byte[] buffer = new byte[mAlarmReadCount]; int count = 0; string strMsg = ""; int startIndex = -1; int endIndex = -1; NormalResponse res = null; DateTime dtStart = DateTime.Now; DateTime dtEnd = DateTime.Now; while (!mIsCloseAlarm) { count = stream.Read(buffer, 0, mAlarmReadCount); if (count > 0) { strMsg += Encoding.UTF8.GetString(buffer, 0, count); startIndex = strMsg.IndexOf("<response>"); endIndex = strMsg.IndexOf("</response>"); string xml = strMsg.Substring(startIndex, endIndex - startIndex + "</response>".Length); res = HuaweiXMLHelper.GetNormalResponse(xml); strMsg = strMsg.Substring(endIndex + "</response>".Length); startIndex = -1; endIndex = -1; break; } dtEnd = DateTime.Now; if ((dtEnd - dtStart).TotalSeconds > 10) { throw new Exception("连接信息未有获取到,需要重启报警!"); } } while (!mIsCloseAlarm) { count = stream.Read(buffer, 0, mAlarmReadCount); if (count > 0) { string temp = Encoding.UTF8.GetString(buffer, 0, count); strMsg += temp; while (strMsg.Length > 0) { if (startIndex == -1)//未发现第一个<task-info> { startIndex = strMsg.IndexOf("<task-info>"); if (startIndex == -1) { if (strMsg.Length >= "<task-info>".Length) { strMsg = strMsg.Substring(strMsg.Length - "<task-info>".Length); } break; } } if (startIndex >= 0) { int i = startIndex + "<task-info>".Length; int taskInfoEndIndex = strMsg.IndexOf("</task-info>", i); if (taskInfoEndIndex > 0)//必须有任务结束节点 { i = taskInfoEndIndex + "</task-info>".Length; int i1 = strMsg.IndexOf("</attach-rules>", i);//找到轨迹节点结束 int i2 = strMsg.IndexOf("</alarm>", i);//找到报警节点结束,发现一条报警 if (i1 == -1 && i2 == -1)//没有标志结束 { break; } else if (i1 >= 0 && (i1 < i2 || i2 == -1))//找到轨迹结束节点 { strMsg = strMsg.Substring(i1 + "</attach-rules>".Length); startIndex = -1; endIndex = -1; continue; } else if (i2 > 0 && (i2 < i1 || i1 == -1))//找报警节点 { endIndex = i2;//找到报警节点结束,发现一条报警 string alarmXml = "<taskalarm>" + strMsg.Substring(startIndex, endIndex - startIndex + "</alarm>".Length) + "</taskalarm>"; Thread th = new Thread(new ThreadStart(delegate() { ParseAlarmXml(alarmXml); })); th.IsBackground = true; th.Start(); strMsg = strMsg.Substring(endIndex + "</alarm>".Length); startIndex = -1; endIndex = -1; continue; } } else { break; } } } } else { Console.WriteLine("##########读取报警反馈:无"); Thread.Sleep(1000); } } } catch (System.Threading.ThreadAbortException ex) { mLog.Info("AlarmCallBack...7"); try { if (stream != null) { stream.Close(); stream.Dispose(); response.Close(); } } catch { } mLog.Info("AlarmCallBack线程已人为终止!--0" + ex.Message, ex); } catch(IOException ex) { mLog.Info("AlarmCallBack...8"); try { if (stream != null) { stream.Close(); stream.Dispose(); response.Close(); } } catch { } } catch (ObjectDisposedException ex) { mLog.Info("AlarmCallBack...9"); mLog.Info("AlarmCallBack读取流已人为终止!--2" + ex.Message, ex); try { if (stream != null) { stream.Close(); stream.Dispose(); response.Close(); } } catch { } } catch (System.Exception ex) { mLog.Info("AlarmCallBack...10"); mLog.Error("AlarmCallBack 0:" + ex.Message,ex); try { if (stream != null) { stream.Close(); stream.Dispose(); response.Close(); } } catch { } } finally { } })); alarmThead[1].IsBackground = true; alarmThead[1].Start(); } catch (System.Exception ex) { mLog.Info("AlarmCallBack...11"); mLog.Error("AlarmCallBack 1:" + ex.Message,ex); mLog.Info("AlarmCallBack开始重新报警连接....3"); mTimes = 50; } finally { } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持易盾网络。

