zoukankan      html  css  js  c++  java
  • ServiceHelper

      1     public class ServiceHelper
      2     {
      3         private static string _baseUrl = $@"http://{Config.Instance.MesServerUrl}/";
      4         private static string _sessionHeader;
      5         public static void ClearSession()
      6         {
      7             _sessionHeader = "";
      8         }
      9         public static string SessionId;
     10 
     11         public static string BaseUrl
     12         {
     13             get { return _baseUrl; }
     14             set { _baseUrl = $@"http://{value}/";} //value  包含IP和端口号 + 后台服务名, 如:localhost:8080/DadeMes
     15         } 
     16 
     17 
     18         /// <summary>
     19         /// HttpClient实现Get请求
     20         /// <param name="subUrl">子URL前面必须带 "/" 符号</param>
     21         /// </summary>
     22         public static string Get(string subUrl)
     23         {
     24             try
     25             {
     26                 using (var client = GetClient())
     27                 {
     28                     return client.DownloadString(subUrl);
     29 
     30                 }
     31             }
     32             catch (WebException)
     33             {
     34                 throw new Exception("无法连接服务器");
     35             }
     36         }
     37 
     38         /// <summary>
     39         /// Post
     40         /// </summary>
     41         /// <param name="url">子地址</param>
     42         /// <param name="strData">要传输的json字符串</param>
     43         /// <returns></returns>
     44         public static string Post(string url, string strData = "")
     45         {
     46             try
     47             {
     48                 using (var client = GetClient())
     49                 {
     50                     var postData = Encoding.UTF8.GetBytes(strData); //编码,尤其是汉字,事先要看下抓取网页的编码方式
     51                     var responseData = client.UploadData(url, "POST", postData); //得到返回字符流
     52                     if (string.IsNullOrWhiteSpace(_sessionHeader))
     53                     {
     54                         _sessionHeader = client.ResponseHeaders["Set-Cookie"];//保存session
     55                     }
     56                     return Encoding.UTF8.GetString(responseData); //解码
     57                 }
     58             }
     59             catch (WebException ex)
     60             {
     61                 throw new Exception("无法连接服务器");
     62             }
     63         }
     64 
     65         /// <summary>
     66         /// 将本地文件上传到服务器
     67         /// </summary>
     68         /// <param name="url">服务器路径</param>
     69         /// <param name="filePath">本地路径</param>
     70         /// <returns>上传成功后返回的是上文件的路径</returns>
     71         public static string UploadFile(string url, string filePath)
     72         {
     73             try
     74             {
     75                 using (var client = GetClient())
     76                 {
     77                     var responseData = client.UploadFile(url, filePath); //得到返回字符流
     78 
     79                     if (string.IsNullOrWhiteSpace(_sessionHeader))
     80                     {
     81                         _sessionHeader = client.ResponseHeaders["Set-Cookie"];//保存session
     82                     }
     83                     return Encoding.UTF8.GetString(responseData); //解码
     84                 }
     85             }
     86             catch (WebException ex)
     87             {
     88                 throw new Exception("无法连接服务器");
     89             }
     90         }
     91 
     92         private static WebClient GetClient()
     93         {
     94             return new WebClient
     95             {
     96                 //webClient.Headers.Add("Cookie",header);//在给服务器发送消息时加上这个Session值
     97                 Headers =
     98                 {
     99                     [HttpRequestHeader.UserAgent] = "Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; Trident/6.0)",
    100                     ["DNT"] = "1",
    101                     //["Content-Type"]="application/x-www-form-urlencoded",
    102                     ["Content-Type"]="application/json",
    103                     ["Cookie"]=_sessionHeader,
    104                     ["x-requested-with"]="XMLHttpRequest",
    105                 },
    106                 Encoding = Encoding.UTF8,
    107                 BaseAddress = BaseUrl
    108             };
    109         }
    110     }
  • 相关阅读:
    Pytorch-基于Transformer的情感分类
    Pytorch-LSTM+Attention文本分类
    .NET ------ 批量修改
    idea ---- idea中关联GitHub
    .NET ----- 将文本框改成下划线,将下拉框改为下拉下划线
    表设计(省市县)
    锁:并发编程中的三个问题(可见性、原子性、有序性)
    freemarker:常用指令、null值的处理、基本数据类型、自定义指令
    vue:绑定属性指令(绑定属性、绑定class(对象语法、数组语法))
    vue:指令(插值操作、指令(v-once、v-html、v-text、v-pre、v-cloak))
  • 原文地址:https://www.cnblogs.com/jonney-wang/p/7576083.html
Copyright © 2011-2022 走看看