• 帮助类4


    #region 构造URL GET请求
     2         /// <summary>
     3         /// 获取请求的反馈信息
     4         /// </summary>
     5         /// <param name="url">地址</param>
     6         /// <returns></returns>
     7         public static string doGetRequest(string url)
     8         {
     9             HttpWebRequest hwRequest;
    10             HttpWebResponse hwResponse;
    11 
    12             string strResult = string.Empty;
    13             try
    14             {
    15                 hwRequest = (System.Net.HttpWebRequest)WebRequest.Create(url);
    16                 hwRequest.Timeout = timeout;
    17                 hwRequest.Method = "GET";
    18                 hwRequest.ContentType = "application/x-www-form-urlencoded;charset=utf-8";
    19             }
    20             catch 
    21             {
    22                 return strResult;
    23             }
    24 
    25             //get response
    26             try
    27             {
    28                 hwResponse = (HttpWebResponse)hwRequest.GetResponse();
    29                 StreamReader srReader = new StreamReader(hwResponse.GetResponseStream(), Encoding.UTF8);
    30                 strResult = srReader.ReadToEnd();
    31                 srReader.Close();
    32                 hwResponse.Close();
    33             }
    34             catch 
    35             {
    36                 return strResult;
    37             }
    38 
    39             return strResult;
    40         }
    41         #endregion

    #region POST请求
     2         public static string PostMethod(string url, string param)
     3         {
     4             byte[] data = Encoding.UTF8.GetBytes(param);
     5             return doPostRequest(url, data);
     6         }
     7         /// <summary>
     8         /// POST请求
     9         /// </summary>
    10         /// <param name="url">URL</param>
    11         /// <param name="encoding">编码gb2312/utf8</param>
    12         /// <param name="param">参数</param>
    13         /// <returns>结果</returns>
    14         public static string PostMethod(string url, string encoding, string param)
    15         {
    16             HttpWebRequest hwRequest;
    17             HttpWebResponse hwResponse;
    18 
    19             string strResult = string.Empty;
    20             byte[] bData = null;
    21             if (string.IsNullOrEmpty(param))
    22             {
    23                 int p = url.IndexOf("?");
    24                 string sData = "";
    25                 if (p > 0)
    26                 {
    27                     sData = url.Substring(p + 1);
    28                     url = url.Substring(0, p);
    29                 }
    30                 bData = Encoding.GetEncoding(encoding).GetBytes(sData);
    31                 
    32             }
    33             else
    34             {
    35                 bData = Encoding.GetEncoding(encoding).GetBytes(param);
    36             }
    37             try
    38             {
    39                 ServicePointManager.Expect100Continue = false;//远程服务器返回错误: (417) Expectation failed 异常源自HTTP1.1协议的一个规范: 100(Continue)
    40                 hwRequest = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(url);
    41                 hwRequest.Timeout = timeout;
    42                 hwRequest.Method = "POST";
    43                 hwRequest.ContentType = "application/x-www-form-urlencoded";
    44                 hwRequest.ContentLength = bData.Length;
    45                 Stream smWrite = hwRequest.GetRequestStream();
    46                 smWrite.Write(bData, 0, bData.Length);
    47                 smWrite.Close();
    48             }
    49             catch
    50             {
    51                 return strResult;
    52             }
    53             //get response
    54             try
    55             {
    56                 hwResponse = (HttpWebResponse)hwRequest.GetResponse();
    57                 StreamReader srReader = new StreamReader(hwResponse.GetResponseStream(), Encoding.GetEncoding(encoding));
    58                 strResult = srReader.ReadToEnd();
    59                 srReader.Close();
    60                 hwResponse.Close();
    61             }
    62             catch
    63             {
    64                 return strResult;
    65             }
    66 
    67             return strResult;
    68         }
    69         #endregion

    #region 访问提交创建文件 (供生成静态页面使用,无需模板)
     2         /// <summary>
     3         /// 访问提交创建文件 (供生成静态页面使用,无需模板)
     4         /// 调用实例 Utils.CreateFileHtml("http://www.xiaomi.com", Server.MapPath("/xxx.html"));
     5         /// </summary>
     6         /// <param name="url">原网址</param>
     7         /// <param name="createpath">生成路径</param>
     8         /// <returns>true false</returns>
     9         public static bool CreateFileHtml(string url, string createpath)
    10         {
    11             if (!string.IsNullOrEmpty(url))
    12             {
    13                 string result = PostMethod(url, "");
    14                 if (!string.IsNullOrEmpty(result))
    15                 {
    16                     if (string.IsNullOrEmpty(createpath))
    17                     {
    18                         createpath = "/default.html";
    19                     }
    20                     string filepath = createpath.Substring(createpath.LastIndexOf(@""));
    21                     createpath = createpath.Substring(0, createpath.LastIndexOf(@""));
    22                     if (!Directory.Exists(createpath))
    23                     {
    24                         Directory.CreateDirectory(createpath);
    25                     }
    26                     createpath = createpath + filepath;
    27                     try
    28                     {                       
    29                         FileStream fs2 = new FileStream(createpath, FileMode.Create);
    30                         StreamWriter sw = new StreamWriter(fs2, System.Text.Encoding.UTF8);
    31                         sw.Write(result);
    32                         sw.Close();
    33                         fs2.Close();
    34                         fs2.Dispose();
    35                         return true;
    36                     }
    37                     catch { return false; }
    38                 }
    39                 return false;
    40             }
    41             return false;
    42         }
    43         /// <summary>
    44         /// 访问提交创建文件 (供生成静态页面使用,需要模板)
    45         /// 调用实例 Utils.CreateFileHtmlByTemp(html, Server.MapPath("/xxx.html"));
    46         /// </summary>
    47         /// <param name="url">原网址</param>
    48         /// <param name="createpath">生成路径</param>
    49         /// <returns>true false</returns>
    50         public static bool CreateFileHtmlByTemp(string result, string createpath)
    51         {
    52                 if (!string.IsNullOrEmpty(result))
    53                 {
    54                     if (string.IsNullOrEmpty(createpath))
    55                     {
    56                         createpath = "/default.html";
    57                     }
    58                     string filepath = createpath.Substring(createpath.LastIndexOf(@""));
    59                     createpath = createpath.Substring(0, createpath.LastIndexOf(@""));
    60                     if (!Directory.Exists(createpath))
    61                     {
    62                         Directory.CreateDirectory(createpath);
    63                     }
    64                     createpath = createpath + filepath;
    65                     try
    66                     {
    67                         FileStream fs2 = new FileStream(createpath, FileMode.Create);
    68                         StreamWriter sw = new StreamWriter(fs2, new UTF8Encoding(false));//去除UTF-8 BOM
    69                         sw.Write(result);
    70                         sw.Close();
    71                         fs2.Close();
    72                         fs2.Dispose();
    73                         return true;
    74                     }
    75                     catch { return false; }
    76                 }
    77                 return false;
    78         }
    79         #endregion
  • 相关阅读:
    vim编辑器基本操作及文件权限,sudo命令等介绍
    centos的基本命令04
    linux的sed(增删改查)使用方法
    rm: cannot remove `xxx’: Operation not permitted问题的处理方案
    迅为iTOP-4412物联网开发板入门学习高手进阶项目开发超树莓派
    迅为I.MX6Q开发板配不同分辨率不同尺寸液晶屏幕
    iTOP-4412开发板-实战教程-ssh服务器移植到arm开发板
    iTOP-4412开发板全新升级支持4G全网通模块
    迅为iMX6UL开发板低功耗高能效开发平台
    iTOP-4418/6818开发板支持双屏异显,双屏同显
  • 原文地址:https://www.cnblogs.com/zhangxiaolei521/p/5808695.html
Copyright © 2020-2023  润新知