Esempio n. 1
0
        public static JsonMessage Send(string phone, string sendMsg)
        {
            sendMsg = HttpUtility.UrlEncode(sendMsg, Encoding.GetEncoding("GBK"));
            string res = string.Empty;
            JsonMessage ret = new JsonMessage();
            try
            {
                string SMSUserName = System.Configuration.ConfigurationManager.AppSettings["SMSUserName"];
                string SMSPassWord = System.Configuration.ConfigurationManager.AppSettings["SMSPassWord"];
                string SMSServiceUrl = System.Configuration.ConfigurationManager.AppSettings["SMSServiceUrl"];

                HttpItem parm = new HttpItem();
                parm.ResultType = ResultType.String;
                parm.URL = string.Format("{0}?name={1}&password={2}&mobile={3}&message={4}",
                    SMSServiceUrl, SMSUserName, SMSPassWord, phone, sendMsg);

                HttpHelper httpHelper = new HttpHelper();
                HttpResult httpResult = httpHelper.GetHtml(parm);
                res = httpResult.Html;
                if (httpResult.StatusCode == System.Net.HttpStatusCode.OK)
                {
                    string[] str = res.Split(',');
                    if (str[0] == "succ")
                    {
                        ret.Info = "发送成功";
                        ret.Status = true;
                    }
                    else
                    {
                        ret.Info = "发送失败,返回内容:" + httpResult.StatusCode + "   " + res;
                        ret.Status = false;
                        ret.Data = res;
                    }
                }
                else
                {
                    ret.Info = "发送失败,返回内容:" + httpResult.StatusCode + "   " + res;
                    ret.Status = false;
                    ret.Data = res;
                }
            }
            catch (Exception ex)
            {
                ret.Info = ex.Message + ex.InnerException + ex.Source + ex.TargetSite + ex.StackTrace;
                ret.Data = res;
                ret.Status = false;
            }
            return ret;
        }
Esempio n. 2
0
 /// <summary>
 /// 根据相传入的数据,得到相应页面数据
 /// </summary>
 /// <param name="objhttpitem">参数类对象</param>
 /// <returns>返回HttpResult类型</returns>
 public HttpResult GetHtml(HttpItem objhttpitem)
 {
     //返回参数
     HttpResult result = new HttpResult();
     try
     {
         //准备参数
         SetRequest(objhttpitem);
     }
     catch (Exception ex)
     {
         result = new HttpResult()
         {
             Cookie = "",
             Header = null,
             Html = ex.Message,
             StatusDescription = "配置参数时出错:" + ex.Message
         };
         return result;
     }
     try
     {
         #region 得到请求的response
         using (response = (HttpWebResponse)request.GetResponse())
         {
             result.StatusCode = response.StatusCode;
             result.StatusDescription = response.StatusDescription;
             result.Header = response.Headers;
             if (response.Cookies != null) result.CookieCollection = response.Cookies;
             if (response.Headers["set-cookie"] != null) result.Cookie = response.Headers["set-cookie"];
             MemoryStream _stream = new MemoryStream();
             //GZIIP处理
             if (response.ContentEncoding != null && response.ContentEncoding.Equals("gzip", StringComparison.InvariantCultureIgnoreCase))
             {
                 //开始读取流并设置编码方式
                 //new GZipStream(response.GetResponseStream(), CompressionMode.Decompress).CopyTo(_stream, 10240);
                 //.net4.0以下写法
                 _stream = GetMemoryStream(new GZipStream(response.GetResponseStream(), CompressionMode.Decompress));
             }
             else
             {
                 //开始读取流并设置编码方式
                 //response.GetResponseStream().CopyTo(_stream, 10240);
                 //.net4.0以下写法
                 _stream = GetMemoryStream(response.GetResponseStream());
             }
             //获取Byte
             byte[] RawResponse = _stream.ToArray();
             _stream.Close();
             //是否返回Byte类型数据
             if (objhttpitem.ResultType == ResultType.Byte) result.ResultByte = RawResponse;
             //从这里开始我们要无视编码了
             if (encoding == null)
             {
                 Match meta = Regex.Match(Encoding.Default.GetString(RawResponse), "<meta([^<]*)charset=([^<]*)[\"']", RegexOptions.IgnoreCase);
                 string charter = (meta.Groups.Count > 1) ? meta.Groups[2].Value.ToLower() : string.Empty;
                 if (charter.Length > 2)
                     encoding = Encoding.GetEncoding(charter.Trim().Replace("\"", "").Replace("'", "").Replace(";", "").Replace("iso-8859-1", "gbk"));
                 else
                 {
                     if (string.IsNullOrEmpty(response.CharacterSet)) encoding = Encoding.UTF8;
                     else encoding = Encoding.GetEncoding(response.CharacterSet);
                 }
             }
             //得到返回的HTML
             result.Html = encoding.GetString(RawResponse);
         }
         #endregion
     }
     catch (WebException ex)
     {
         //这里是在发生异常时返回的错误信息
         response = (HttpWebResponse)ex.Response;
         result.Html = ex.Message;
         if (response != null)
         {
             result.StatusCode = response.StatusCode;
             result.StatusDescription = response.StatusDescription;
         }
     }
     catch (Exception ex)
     {
         result.Html = ex.Message;
     }
     if (objhttpitem.IsToLower) result.Html = result.Html.ToLower();
     return result;
 }
Esempio n. 3
0
 /// <summary>
 /// 为请求准备参数
 /// </summary>
 ///<param name="objhttpItem">参数列表</param>
 private void SetRequest(HttpItem objhttpItem)
 {
     //设置安全协议
     ServicePointManager.SecurityProtocol = objhttpItem.SecurityProtocolType;
     // 验证证书
     SetCer(objhttpItem);
     //设置Header参数
     if (objhttpItem.Header != null && objhttpItem.Header.Count > 0) foreach (string item in objhttpItem.Header.AllKeys)
         {
             request.Headers.Add(item, objhttpItem.Header[item]);
         }
     // 设置代理
     SetProxy(objhttpItem);
     if (objhttpItem.ProtocolVersion != null) request.ProtocolVersion = objhttpItem.ProtocolVersion;
     request.ServicePoint.Expect100Continue = objhttpItem.Expect100Continue;
     //请求方式Get或者Post
     request.Method = objhttpItem.Method;
     request.Timeout = objhttpItem.Timeout;
     request.ReadWriteTimeout = objhttpItem.ReadWriteTimeout;
     //Accept
     request.Accept = objhttpItem.Accept;
     //ContentType返回类型
     request.ContentType = objhttpItem.ContentType;
     //UserAgent客户端的访问类型,包括浏览器版本和操作系统信息
     request.UserAgent = objhttpItem.UserAgent;
     // 编码
     encoding = objhttpItem.Encoding;
     //设置Cookie
     SetCookie(objhttpItem);
     //来源地址
     request.Referer = objhttpItem.Referer;
     //是否执行跳转功能
     request.AllowAutoRedirect = objhttpItem.Allowautoredirect;
     //设置Post数据
     SetPostData(objhttpItem);
     //设置最大连接
     if (objhttpItem.Connectionlimit > 0) request.ServicePoint.ConnectionLimit = objhttpItem.Connectionlimit;
 }
Esempio n. 4
0
 /// <summary>
 /// 设置代理
 /// </summary>
 /// <param name="objhttpItem">参数对象</param>
 private void SetProxy(HttpItem objhttpItem)
 {
     if (!string.IsNullOrEmpty(objhttpItem.ProxyIp))
     {
         //设置代理服务器
         if (objhttpItem.ProxyIp.Contains(":"))
         {
             string[] plist = objhttpItem.ProxyIp.Split(':');
             WebProxy myProxy = new WebProxy(plist[0].Trim(), Convert.ToInt32(plist[1].Trim()));
             //建议连接
             myProxy.Credentials = new NetworkCredential(objhttpItem.ProxyUserName, objhttpItem.ProxyPwd);
             //给当前请求对象
             request.Proxy = myProxy;
         }
         else
         {
             WebProxy myProxy = new WebProxy(objhttpItem.ProxyIp, false);
             //建议连接
             myProxy.Credentials = new NetworkCredential(objhttpItem.ProxyUserName, objhttpItem.ProxyPwd);
             //给当前请求对象
             request.Proxy = myProxy;
         }
         //设置安全凭证
         request.Credentials = CredentialCache.DefaultNetworkCredentials;
     }
 }
Esempio n. 5
0
 /// <summary>
 /// 设置Post数据
 /// </summary>
 /// <param name="objhttpItem">Http参数</param>
 private void SetPostData(HttpItem objhttpItem)
 {
     //验证在得到结果时是否有传入数据
     if (request.Method.Trim().ToLower().Contains("post"))
     {
         if (objhttpItem.PostEncoding != null)
         {
             postencoding = objhttpItem.PostEncoding;
         }
         byte[] buffer = null;
         //写入Byte类型
         if (objhttpItem.PostDataType == PostDataType.Byte && objhttpItem.PostdataByte != null && objhttpItem.PostdataByte.Length > 0)
         {
             //验证在得到结果时是否有传入数据
             buffer = objhttpItem.PostdataByte;
         }//写入文件
         else if (objhttpItem.PostDataType == PostDataType.FilePath && !string.IsNullOrEmpty(objhttpItem.Postdata))
         {
             StreamReader r = new StreamReader(objhttpItem.Postdata, postencoding);
             buffer = postencoding.GetBytes(r.ReadToEnd());
             r.Close();
         } //写入字符串
         else if (!string.IsNullOrEmpty(objhttpItem.Postdata))
         {
             buffer = postencoding.GetBytes(objhttpItem.Postdata);
         }
         if (buffer != null)
         {
             request.ContentLength = buffer.Length;
             request.GetRequestStream().Write(buffer, 0, buffer.Length);
         }
     }
 }
Esempio n. 6
0
 /// <summary>
 /// 设置Cookie
 /// </summary>
 /// <param name="objhttpItem">Http参数</param>
 private void SetCookie(HttpItem objhttpItem)
 {
     if (!string.IsNullOrEmpty(objhttpItem.Cookie))
         //Cookie
         request.Headers[HttpRequestHeader.Cookie] = objhttpItem.Cookie;
     //设置Cookie
     if (objhttpItem.CookieCollection != null)
     {
         request.CookieContainer = new CookieContainer();
         request.CookieContainer.Add(objhttpItem.CookieCollection);
     }
 }
Esempio n. 7
0
 /// <summary>
 /// 设置多个证书
 /// </summary>
 /// <param name="objhttpItem"></param>
 private void SetCerList(HttpItem objhttpItem)
 {
     if (objhttpItem.ClentCertificates != null && objhttpItem.ClentCertificates.Count > 0)
     {
         foreach (X509Certificate item in objhttpItem.ClentCertificates)
         {
             request.ClientCertificates.Add(item);
         }
     }
 }
Esempio n. 8
0
 /// <summary>
 /// 设置证书
 /// </summary>
 /// <param name="objhttpItem"></param>
 private void SetCer(HttpItem objhttpItem)
 {
     if (!string.IsNullOrEmpty(objhttpItem.CerPath))
     {
         //这一句一定要写在创建连接的前面。使用回调的方法进行证书验证。
         ServicePointManager.ServerCertificateValidationCallback = new System.Net.Security.RemoteCertificateValidationCallback(CheckValidationResult);
         //初始化对像,并设置请求的URL地址
         request = (HttpWebRequest)WebRequest.Create(objhttpItem.URL);
         SetCerList(objhttpItem);
         //将证书添加到请求里
         request.ClientCertificates.Add(new X509Certificate(objhttpItem.CerPath));
     }
     else
     {
         //初始化对像,并设置请求的URL地址
         request = (HttpWebRequest)WebRequest.Create(objhttpItem.URL);
         SetCerList(objhttpItem);
     }
 }