【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. ...
随机推荐
- 《fullPage.js》创建全屏滚动的网站
插件介绍 fullPage.js是一个简单易用的插件,创建全屏滚动的网站(也被称为单页网站).它允许全屏滚动创建网站,以及添加内部滑块. 浏览器兼容性 主要功能 支持鼠标滚动 支持前进后退和键盘控制 ...
- Android学习笔记:如何设置ImageView中图片的显示方式
我们在用ImageView显示图片时,很多情况下图片的大小与ImageView的尺寸不是完全一样的.这时就涉及到该如何设置显示图片了. ImageView有个重要的属性是ScaleType,该属性用以 ...
- oracle存储过程调试方法
PL/SQL中为我们提供了[调试存储过程]的功能,可以帮助你完成存储过程的预编译与测试. 点击要调试的存储过程,右键选择TEST 如果需要查看变量,当然调试都需要.在右键菜单中选择Add debug ...
- 'Invalid update: invalid number of rows in section xx. The number of rows contained in an existing section after the update (xxx)...
'Invalid update: invalid number of rows in section 5. The number of rows contained in an existing s ...
- 搭建ngrok服务器(ubuntu 14)-- 微信 80端口和IPC备案限制解决方案
概述: ngrok其实这东西,我也不是很懂,所以也直接跟大家说,这就是个类似花生壳的东西. 简单来说,它就好像把我们内网自己使用的电脑和服务器用vpn连接起来,然后你的电脑就可以从互联网来访问了,有个 ...
- Dev-C++程序正确闪退问题
只需要在主函数最后一句语句上面加一句getchar();即可
- 你好,C++(15)四两拨千斤——3.9 指向内存位置的指针
3.9 指向内存位置的指针 一天,两个变量在街上遇到了: “老兄,你家住哪儿啊?改天找你玩儿去.” “哦,我家在静态存储区的0x0049A024号,你家呢?” “我家在动态存储区的0x0022FF0 ...
- Ruby设计模式透析之 —— 适配器(Adapter)
转载请注明出处:http://blog.csdn.net/sinyu890807/article/details/9400153 此为Java设计模式透析的拷贝版,专门为Ruby爱好者提供的,不熟悉R ...
- Apache Rewrite 拟静态配置54
mod_rewrite 规则的使用 RewriteEngine on RewriteCond %{HTTP_HOST} !^www.php100.com [NC] RewriteRule ^/ ...
- Css 八卦
<div class="box"> <div class="right_d"></div> <span class=& ...