博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
【C#网络基础】C# get post请求
阅读量:6711 次
发布时间:2019-06-25

本文共 5802 字,大约阅读时间需要 19 分钟。

using KTCommon.Helper;using KTCommon.LOG;using System;using System.Collections.Generic;using System.IO;using System.Linq;using System.Net;using System.Text;using System.Threading.Tasks;namespace KTCommon.HTTP{    public class KTHttpRequest    {        #region GET请求        ///         /// GET请求        ///         ///         ///         /// 
public static string _Get(string nUrl, int nCacheSecond = 0) { try { string cacheKey = ""; TraceLog.m_Trace.Trace("_Get Request Url=" + nUrl); if (nCacheSecond > 0) { if (nUrl.Contains("&signature=")) { string temp = nUrl.Substring(0, nUrl.IndexOf("&signature=")); cacheKey = MyMD5Helper.GetMD532(temp).ToUpper(); } else { cacheKey = MyMD5Helper.GetMD532(nUrl).ToUpper(); } var cache = CacheHelper.Get(cacheKey); if (null != cache) { TraceLog.m_Trace.Trace(cacheKey + " read cache..."); return cache as string; } } string htmltext = ""; HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(nUrl); // 头信息 myRequest.Headers.Add("Cache-Control", "no-cache"); myRequest.Method = "GET"; myRequest.ContentType = "text/plain"; // 发送的数据信息 myRequest.Timeout = 15 * 1000; HttpWebResponse response = (HttpWebResponse)myRequest.GetResponse(); Stream responseStream = response.GetResponseStream(); StreamReader sr = new StreamReader(responseStream, Encoding.GetEncoding("utf-8")); htmltext = sr.ReadToEnd(); sr.Close(); sr.Dispose(); responseStream.Close(); TraceLog.m_Trace.Trace("_Get htmltext=" + htmltext); if (nCacheSecond > 0 && htmltext.StartsWith("{\"code\":0,")) { TraceLog.m_Trace.Trace(cacheKey + " wrrite cache..."); CacheHelper.Insert(cacheKey, htmltext, nCacheSecond); } return htmltext; } catch (Exception ex) { TraceLog.m_Trace.Trace("_Get Exception=" + ex); return ""; } } #endregion #region POST请求 /// /// POST请求 /// /// /// /// ///
public static string _Post(string nUrl, string nMethodName, object nPostData, bool IsCache = false) {#if DEBUG //urlAddress = "http://localhost/airwaykeeper/v1/API/API2WShare.aspx"; //strMethodName = "_GetShareInfoByUID"; //strRequest = File.ReadAllText("d:\\request.txt", Encoding.UTF8);#endif string htmltext = ""; string cacheKey = ""; Byte[] bSend = null; try { string postData = Newtonsoft.Json.JsonConvert.SerializeObject(nPostData); TraceLog.m_Trace.Trace(nMethodName + " RequestUrl=" + nUrl); TraceLog.m_Trace.Trace(nMethodName + " PostData=" + postData); //缓存 if (IsCache) { cacheKey = nMethodName + "-" + MyMD5Helper.GetMD532(nUrl + nMethodName + postData).ToUpper(); var cache = CacheHelper.Get(cacheKey); if (null != cache) { TraceLog.m_Trace.Trace(cacheKey + " read cache..."); return cache as string; } } HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(nUrl); // 头信息 myRequest.Headers.Add("Cache-Control", "no-cache"); myRequest.Headers.Add("MethodName", nMethodName); myRequest.Method = "POST"; myRequest.ContentType = "application/json"; // 发送的数据信息 bSend = Encoding.UTF8.GetBytes(postData); myRequest.ContentLength = bSend.Length; myRequest.Timeout = 60 * 1000; // 发送数据 Stream newStream = myRequest.GetRequestStream(); newStream.Write(bSend, 0, bSend.Length); newStream.Close(); HttpWebResponse response = (HttpWebResponse)myRequest.GetResponse(); Stream responseStream = response.GetResponseStream(); StreamReader sr = new StreamReader(responseStream, Encoding.GetEncoding("utf-8")); htmltext = sr.ReadToEnd(); sr.Close(); sr.Dispose(); responseStream.Close(); } catch (Exception ex) { TraceLog.m_Trace.Trace(nMethodName + " Exception=" + ex.ToString()); return new BaseResponse(ex.Message).ToString(); } TraceLog.m_Trace.Trace(nMethodName + " ResponseJson=" + htmltext); //缓存 if (IsCache) { if (htmltext.Contains("IsSuccess\":true,")) { TraceLog.m_Trace.Trace(cacheKey + " wrrite cache..."); CacheHelper.Max(cacheKey, htmltext); } } return htmltext; } #endregion } public class BaseResponse { public BaseResponse(string tmp) { } public override string ToString() { return ""; } }}

 

转载地址:http://yxalo.baihongyu.com/

你可能感兴趣的文章
Android 唯一识别码
查看>>
Canonical今天宣布推出Plex Media Server作为Snap Store中的Snap应用程序
查看>>
gdb 学习1
查看>>
SVG TEXT 水平和垂直方向居中
查看>>
Kurento API 参考
查看>>
hello world
查看>>
C语言基础及指针⑦结构体与指针
查看>>
四种常用线程池
查看>>
兼容IE的radio遍历
查看>>
Ossim下RRDTool实战
查看>>
向服务器请求XML数据时中文乱码
查看>>
微信消息接口发送信息到分组和用户,错误代码40003和40008
查看>>
HTTP状态码 错误列表
查看>>
scala依赖限制
查看>>
Font Awesome
查看>>
Dubbo消费者
查看>>
java序列化和持久化
查看>>
thinkphp调试
查看>>
虚拟化中虚拟机处理器核数与物理主机cpu的关系
查看>>
redmine不能发邮件及错误处理“DSN: Service unavailable ”
查看>>