【C#网络基础】C# get post请求
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请求 /// <summary>
/// GET请求
/// </summary>
/// <param name="nUrl"></param>
/// <param name="nCacheSecond"></param>
/// <returns></returns>
public static string _Get(string nUrl, int nCacheSecond = )
{
try
{
string cacheKey = "";
TraceLog.m_Trace.Trace("_Get Request Url=" + nUrl); if (nCacheSecond > )
{
if (nUrl.Contains("&signature="))
{
string temp = nUrl.Substring(, 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 = * ; 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 > && 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请求 /// <summary>
/// POST请求
/// </summary>
/// <param name="nUrl"></param>
/// <param name="nMethodName"></param>
/// <param name="nPostData"></param>
/// <returns></returns>
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 = * ; // 发送数据
Stream newStream = myRequest.GetRequestStream();
newStream.Write(bSend, , 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 "";
}
}
}
【C#网络基础】C# get post请求的更多相关文章
- 网络基础知识(http请求)
http请求的过程 域名解析----TCP连接 ----发送请求-----响应请求----获取html代码----浏览器渲染 TCP是主机对主机层的控制传输协议,提供可靠的连接服务 TCP的三次握手 ...
- JAVA基础知识之网络编程——-网络基础(Java的http get和post请求,多线程下载)
本文主要介绍java.net下为网络编程提供的一些基础包,InetAddress代表一个IP协议对象,可以用来获取IP地址,Host name之类的信息.URL和URLConnect可以用来访问web ...
- 前端学HTTP之网络基础
× 目录 [1]网络 [2]OSI [3]TCP/IP 前面的话 HTTP协议对于前端工程师是非常重要的.我们在浏览网站时,访问的每一个WEB页面都需要使用HTTP协议实现.如果不了解HTTP协议,就 ...
- iOS网络基础知识
iOS网络基础知识 1.一次HTTP请求的完整过程 (1)浏览器或应用发起Http请求,请求包含Http请求Http(请求),地址(url),协议(Http1.1)请求为头部 (2)web服务器接收到 ...
- iOS开发——网络篇——HTTP/NSURLConnection(请求、响应)、http响应状态码大全
一.网络基础 1.基本概念> 为什么要学习网络编程在移动互联网时代,移动应用的特征有几乎所有应用都需要用到网络,比如QQ.微博.网易新闻.优酷.百度地图只有通过网络跟外界进行数据交互.数据更新, ...
- Android系列之网络(二)----HTTP请求头与响应头
[声明] 欢迎转载,但请保留文章原始出处→_→ 生命壹号:http://www.cnblogs.com/smyhvae/ 文章来源:http://www.cnblogs.com/smyhvae/p/ ...
- 网络基础知识、ASP.NET 核心知识(1)*
为什么要写网络? 我原本的计划是这样的,连续两天梳理ASP.NET开发的核心知识.说到这呢,有人问了.“不是说好了做ASP.NET笔记吗?为啥要写网络基础知识?是不是傻?” 原因是这样的.作为网站开发 ...
- 网络处理1-异步GET请求
前言 云计算 近几年来,云计算是一个非常热门的技术名词,很多专家认为,云计算会改变互联网的技术基础,甚至会影响整个产业的格局.可能还很多人不了解什么是云计算,简单来说,就是把用户的数据(比如文档.照片 ...
- Java 网络编程(一) 网络基础知识
链接地址:http://www.cnblogs.com/mengdd/archive/2013/03/09/2951826.html 网络基础知识 网络编程的目的:直接或间接地通过网络协议与其他计算机 ...
- http(一)web和网络基础
深入学习http不为别的,只为补充底层知识,打好根基,深入了解其他技术,擒贼先擒王,学好九阳神功以后,乾坤大挪移,太极剑就容易了,急于求成,就只能变周芷若.走着...... 来源于:图解HTTP 1. ...
随机推荐
- AS 自动生成选择器 SelectorChapek
简介 https://github.com/inmite/android-selector-chapek 设计师给我们提供好了各种资源,每个按钮都要写一个selector是不是很麻烦? 这么这个插件就 ...
- codevs 1173 最优贸易(DP+SPFA运用)
/* 中国的题目 ——贱买贵卖 0.0 这题wa了好多遍 第一遍看着题 哎呀这不很简单嘛 从起点能到的点都是合法的点 然后统计合法的点里最大最小值 然后printf 也不知道哪里来的自信 就这么交了 ...
- [JavaScript]plupload多图片上传图片
var uploader = new plupload.Uploader({ //创建实例的构造方法 runtimes: 'html5,flash,silverlight,html4', ...
- UITableView初始
近期在自学IOS,看了黑马提供的视频,讲的很好.在此做些笔记,以供以后查阅.注明了知识来源应该不算侵权吧. 一 UITableView 1,数据展示的条件 1⃣️ UITableView的所有数据都是 ...
- Spring Boot Web项目之参数绑定
一.@RequestParam 这个注解用来绑定单个请求数据,既可以是url中的参数,也可以是表单提交的参数和上传的文件 它有三个属性,value用于设置参数名,defaultValue用于对参数设置 ...
- java事件处理
1.ActionEven事件 文本框,按钮,菜单项,密码框,单选按钮都可以出发ActionEvent事件 使用 addActionListener(ActionListener listen1) 来注 ...
- The partial sum problem
算法:搜索 描述 One day,Tom's girlfriend give him an array A which contains N integers and asked him:Can yo ...
- Ecshop布局参考图
文章列表页: article_cat.dwt 文章内容页: article.dwt 商品品牌页: brand.dwt 所有分类页: catalog.dwt 商品列表页: category.dwt 商品 ...
- AJAX快速上手
创建XMLHttpRequest对象 xmlHttp = new XMLHttpRequest(); xmlHttp = new ActiveXObject('Microsoft.XMLHTTP'); ...
- ServerInfo.INI解密
[GlobalInfo]LastServerName=000781ED2D127FBA074D97444DC82F216443034E66BB341A428B14E326A656B9LastServe ...