zoukankan      html  css  js  c++  java
  • http基础扩展

     1 public class HttpExtend
     2 {
     3 /// <summary>
     4 /// 日志
     5 /// </summary>
     6 public static readonly ILog Log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
     7 
     8 /// <summary> 
     9 /// post提交 Created By ZhangQC 2016.08.23
    10 /// </summary>
    11 /// <param name="postUrl"></param>
    12 /// <param name="paramData"></param>
    13 /// <returns></returns>
    14 public string PostWebRequest(string postUrl, string paramData)
    15 {
    16 var ret = string.Empty;
    17 try
    18 {
    19 ServicePointManager.Expect100Continue = false;//防止407 错误
    20 Encoding dataEncode = Encoding.GetEncoding("GBK");
    21 byte[] byteArray = dataEncode.GetBytes(paramData); //转化
    22 
    23 var webReq = (HttpWebRequest)WebRequest.Create(new Uri(postUrl));
    24 webReq.Method = "POST";
    25 webReq.ContentType = "application/x-www-form-urlencoded";
    26 webReq.ContentLength = byteArray.Length;
    27 
    28 Stream newStream = webReq.GetRequestStream();
    29 newStream.Write(byteArray, 0, byteArray.Length);//写入参数
    30 newStream.Close();
    31 var response = (HttpWebResponse)webReq.GetResponse();
    32 // ReSharper disable once AssignNullToNotNullAttribute
    33 var sr = new StreamReader(response.GetResponseStream(), encoding: Encoding.Default);
    34 ret = sr.ReadToEnd();
    35 sr.Close();
    36 response.Close();
    37 newStream.Close();
    38 }
    39 catch (Exception ex)
    40 {
    41 Log.ErrorFormat("Post请求出错,用于手机短信发送:{0}",ex);
    42 }
    43 return ret;
    44 }
    45 }
  • 相关阅读:
    51 Nod 1068 Bash游戏v3
    51 Nod Bash 游戏v2
    51 Nod 1073 约瑟夫环
    UVA 12063 Zeros and ones 一道需要好好体会的好题
    51 Nod 1161 Partial sums
    2018中国大学生程序设计竞赛
    UVA 11971 Polygon
    UVA 10900 So do you want to be a 2^n-aire?
    UVA 11346 Possibility
    python with as 的用法
  • 原文地址:https://www.cnblogs.com/creater/p/6322081.html
Copyright © 2011-2022 走看看