private string logStringCreator(int time, string msg) { try { StringBuilder sb = new StringBuilder(); sb.AppendLine(string.Format(">>>{0}<<< 测试进度:({1}/{2})", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"), time, this.pbar_TestProcess.Maximum)); sb.AppendLine(string.Format("请求路径为:{0}", GetUrlFromForm())); sb.AppendLine(string.Format("测试间隔为:{0}", GetIntervalFromForm())); sb.AppendLine(string.Format("Json请求为:{0}", requestType.ToString())); sb.AppendLine(string.Format("返回结果:{0}", msg)); return(sb.ToString()); } catch (Exception e) { return(string.Format("日志生成出错。异常为{0}", e)); } }
/// <summary> /// /// </summary> /// <param name="username"></param> /// <param name="pushEnabled"></param> public static void UpdateUserAccount(string username, string password, PushEnabled pushEnabled, UserStatus userStatus) { RequestMode requestMode = RequestMode.Set; if (_voipServerUrl == null) { _voipServerUrl = ConfigurationManager.AppSettings[NeeoConstants.VoipServerUrl]; } RestRequest request = new RestRequest(); request.AddQueryParameter("mode", requestMode.ToString("G").ToLower()); request.AddQueryParameter("mob", username); if (password != null) { request.AddQueryParameter("pass", password); } if (pushEnabled != Voip.PushEnabled.NotSpecified) { request.AddQueryParameter("pushEnabled", pushEnabled.ToString("D")); } if (userStatus != Voip.UserStatus.NotSpecified) { request.AddQueryParameter("status", userStatus.ToString("D")); } try { ExecuteRequest(request, requestMode); } catch (ApplicationException firstAppException) { LogManager.CurrentInstance.ErrorLogger.LogError( System.Reflection.MethodBase.GetCurrentMethod().DeclaringType, firstAppException.Message); try { if (!NeeoUtility.IsNullOrEmpty(password)) { ExecuteBackupPolicy(firstAppException.Message, request, requestMode); } else { throw new ApplicationException(CustomHttpStatusCode.ServerInternalError.ToString("D")); } } catch (ApplicationException secondAppException) { throw new Exception(secondAppException.Message); } } }
/// <summary> /// /// </summary> /// <param name="URL"></param> /// <param name="Mode"></param> /// <param name="Parameter"></param> /// <returns></returns> public static string WebRequest(this string URL, RequestMode Mode, string Parameter) { HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(URL); request.Method = Mode.ToString(); request.ContentType = "application/x-www-form-urlencoded"; if (URL.ToLower().StartsWith("https")) { ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult); } if (Mode.ToString().Equals("POST") && !String.IsNullOrEmpty(Parameter)) { using (var sw = new StreamWriter(request.GetRequestStream())) { sw.Write(Parameter); } } var resonse = ""; using (HttpWebResponse response = (HttpWebResponse)request.GetResponse()) { if (response.StatusCode != HttpStatusCode.OK) { throw new Exception(string.Format("{0}-{1}", ((int)response.StatusCode).ToString(), "回應不成功")); } using (var sr = new StreamReader(response.GetResponseStream())) { resonse = sr.ReadToEnd(); } } return(resonse); }
private string logStringCreator(int time, string msg) { try { StringBuilder sb = new StringBuilder(); sb.AppendLine(string.Format("{0} 测试进度: ({1}/{2})", DateTime.Now.ToString("MM-dd HH:mm:ss,fff"), time, nodesCount)); sb.AppendLine(string.Format("请求路径为:{0}", GetUrlFromMain())); sb.AppendLine(string.Format("Json请求为:{0}", requestType.ToString())); sb.AppendLine(string.Format("返回结果:{0}", msg)); return(sb.ToString()); } catch (Exception e) { return(string.Format("日志生成出错,异常:{0}", e)); } }
/// <summary> /// /// </summary> public static void RegisterUser(string username, string password, PushEnabled pushEnabled) { RequestMode requestMode = RequestMode.Add; if (_voipServerUrl == null) { _voipServerUrl = ConfigurationManager.AppSettings[NeeoConstants.VoipServerUrl]; } RestRequest request = new RestRequest(); request.AddQueryParameter("mode", requestMode.ToString("G").ToLower()); request.AddQueryParameter("mob", username); request.AddQueryParameter("pass", password); if (pushEnabled != Voip.PushEnabled.NotSpecified) { request.AddQueryParameter("pushEnabled", pushEnabled.ToString("D")); } try { ExecuteRequest(request, requestMode); } catch (ApplicationException firstAppException) { LogManager.CurrentInstance.ErrorLogger.LogError( System.Reflection.MethodBase.GetCurrentMethod().DeclaringType, firstAppException.Message); try { ExecuteBackupPolicy(firstAppException.Message, request, requestMode); } catch (ApplicationException secondAppException) { LogManager.CurrentInstance.ErrorLogger.LogError( System.Reflection.MethodBase.GetCurrentMethod().DeclaringType, secondAppException.Message); try { ExecuteBackupPolicy(firstAppException.Message, request, requestMode); } catch (ApplicationException thirdAppException) { throw new Exception(thirdAppException.Message); } } } }
public static string SendRequest(RequestMode method, String url, String reqParams) { HttpWebRequest myReq = null; HttpWebResponse response = null; string responseFromServer = ""; try { if (method == RequestMode.Get) { url += "?" + reqParams; } myReq = (HttpWebRequest)WebRequest.Create(url); myReq.Method = method.ToString(); myReq.ReadWriteTimeout = DEFAULT_SOCKET_TIMEOUT; myReq.ContentType = contentType; if (method == RequestMode.Post || method == RequestMode.Put || method == RequestMode.Delete) { byte[] bs = Encoding.UTF8.GetBytes(reqParams); myReq.ContentLength = bs.Length; using (Stream reqStream = myReq.GetRequestStream()) { reqStream.Write(bs, 0, bs.Length); reqStream.Close(); } } response = (HttpWebResponse)myReq.GetResponse(); if (Equals(response.StatusCode, HttpStatusCode.OK) || Equals(response.StatusCode, HttpStatusCode.Created)) { using (StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8)) { responseFromServer = reader.ReadToEnd(); } } } catch (WebException e) { throw e; } return(responseFromServer); }
/// <summary> /// 发送请求 /// </summary> /// <param name="method">请求方式</param> /// <param name="url">请求链接</param> /// <param name="reqParams">请求参数</param> /// <returns></returns> public static ResultDTO SendRequest(RequestMode method, String url, String reqParams) { HttpWebRequest myReq = null; HttpWebResponse response = null; try { if (!string.IsNullOrWhiteSpace(reqParams)) { if (method == RequestMode.Get || method == RequestMode.Delete) { url += "?" + reqParams; } } myReq = (HttpWebRequest)WebRequest.Create(url); myReq.Method = method.ToString(); myReq.ReadWriteTimeout = DEFAULT_SOCKET_TIMEOUT; myReq.ContentType = contentType; ////权限验证 //var auth = this.Authorization(); //if (auth != null) //{ // foreach (var item in auth) // { // myReq.Headers.Add(item.Key, item.Value); // } //} if (myReq.Method == "POST" || myReq.Method == "Put") { byte[] bs = Encoding.UTF8.GetBytes(reqParams); myReq.ContentLength = bs.Length; using (Stream reqStream = myReq.GetRequestStream()) { reqStream.Write(bs, 0, bs.Length); reqStream.Close(); } } response = (HttpWebResponse)myReq.GetResponse(); if (Equals(response.StatusCode, HttpStatusCode.OK) || Equals(response.StatusCode, HttpStatusCode.Created)) { using (StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8)) { return(new ResultDTO() { status = true, info = reader.ReadToEnd() }); } } return(new ResultDTO() { status = false, info = "失败" }); } catch (WebException e) { if (e.Status == WebExceptionStatus.ProtocolError) { //HttpStatusCode errorCode = ((HttpWebResponse) e.Response).StatusCode; //string statusDescription = ((HttpWebResponse)e.Response).StatusDescription; using (StreamReader sr = new StreamReader(((HttpWebResponse)e.Response).GetResponseStream(), Encoding.UTF8)) { return(new ResultDTO() { status = false, info = sr.ReadToEnd() }); } } return(new ResultDTO() { status = false, info = e.Message }); } //这里不再抓取非http的异常,如果异常抛出交给开发者自行处理 //catch (System.Exception ex) //{ // String errorMsg = ex.Message; // Debug.Print(errorMsg); //} finally { if (response != null) { response.Close(); } if (myReq != null) { myReq.Abort(); } } }