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请求的更多相关文章

  1. 网络基础知识(http请求)

    http请求的过程 域名解析----TCP连接 ----发送请求-----响应请求----获取html代码----浏览器渲染 TCP是主机对主机层的控制传输协议,提供可靠的连接服务 TCP的三次握手 ...

  2. JAVA基础知识之网络编程——-网络基础(Java的http get和post请求,多线程下载)

    本文主要介绍java.net下为网络编程提供的一些基础包,InetAddress代表一个IP协议对象,可以用来获取IP地址,Host name之类的信息.URL和URLConnect可以用来访问web ...

  3. 前端学HTTP之网络基础

    × 目录 [1]网络 [2]OSI [3]TCP/IP 前面的话 HTTP协议对于前端工程师是非常重要的.我们在浏览网站时,访问的每一个WEB页面都需要使用HTTP协议实现.如果不了解HTTP协议,就 ...

  4. iOS网络基础知识

    iOS网络基础知识 1.一次HTTP请求的完整过程 (1)浏览器或应用发起Http请求,请求包含Http请求Http(请求),地址(url),协议(Http1.1)请求为头部 (2)web服务器接收到 ...

  5. iOS开发——网络篇——HTTP/NSURLConnection(请求、响应)、http响应状态码大全

    一.网络基础 1.基本概念> 为什么要学习网络编程在移动互联网时代,移动应用的特征有几乎所有应用都需要用到网络,比如QQ.微博.网易新闻.优酷.百度地图只有通过网络跟外界进行数据交互.数据更新, ...

  6. Android系列之网络(二)----HTTP请求头与响应头

    ​[声明] 欢迎转载,但请保留文章原始出处→_→ 生命壹号:http://www.cnblogs.com/smyhvae/ 文章来源:http://www.cnblogs.com/smyhvae/p/ ...

  7. 网络基础知识、ASP.NET 核心知识(1)*

    为什么要写网络? 我原本的计划是这样的,连续两天梳理ASP.NET开发的核心知识.说到这呢,有人问了.“不是说好了做ASP.NET笔记吗?为啥要写网络基础知识?是不是傻?” 原因是这样的.作为网站开发 ...

  8. 网络处理1-异步GET请求

    前言 云计算 近几年来,云计算是一个非常热门的技术名词,很多专家认为,云计算会改变互联网的技术基础,甚至会影响整个产业的格局.可能还很多人不了解什么是云计算,简单来说,就是把用户的数据(比如文档.照片 ...

  9. Java 网络编程(一) 网络基础知识

    链接地址:http://www.cnblogs.com/mengdd/archive/2013/03/09/2951826.html 网络基础知识 网络编程的目的:直接或间接地通过网络协议与其他计算机 ...

  10. http(一)web和网络基础

    深入学习http不为别的,只为补充底层知识,打好根基,深入了解其他技术,擒贼先擒王,学好九阳神功以后,乾坤大挪移,太极剑就容易了,急于求成,就只能变周芷若.走着...... 来源于:图解HTTP 1. ...

随机推荐

  1. There is already an open DataReader associated with this Connection which must be closed first

    使用MVC4 EF Linq获取foreach列表循环的时候遇到了如下的问题:报错提示 There is already an open DataReader associated with this ...

  2. java获取字符串格式日期向前或向后n天的日期

    private void setTilteMessage(){          BaseDao dao = new BaseDao();          String titleData = da ...

  3. Mysql数据数据[字节、长度、数据范围]一览表

    1.mysql有哪些数据类型: 主要包括以下五大类: 整数类型:BIT.BOOL.TINY INT.SMALL INT.MEDIUM INT. INT. BIG INT 浮点数类型:FLOAT.DOU ...

  4. 华为S5300交换机配置基于接口的本地端口镜像

    配置思路 1.  将Ethernet0/0/20接口配置为观察端口(监控端口) 2.  将Ethernet0/0/1----Ethernet0/0/10接口配置为镜像端口 配置步骤 1.  配置观察端 ...

  5. Shell中IFS用法

    一 .IFS的介绍    Shell 脚本中有个变量叫IFS(Internal Field Seprator) ,内部域分隔符.完整定义是The shell uses the value stored ...

  6. iOS 网络与多线程--8.百度地图的使用(调用系统浏览器)

    通过调用设备自带的浏览器,打开百度地图 // 1.定义一个方法,用来打开谷歌地图的功能 -(IBAction)openMaps { // 2.定义一个字符串,作为百度地图的当前地理位置 废弃 NSSt ...

  7. zepto源码研究 - ajax.js($.ajaxJSONP 的分析)

    简要:jsonp是一种服务器和客户端信息传递方式,一般是利用script元素赋值src来发起请求.一般凡是带有src属性的元素发起的请求都是可以跨域的. 那么jsonp是如何获取服务器的数据的呢? j ...

  8. CentOS下几种软件安装方式

    1.rpmRPM RedHat Package Manager(RedHat软件包管理工具)的缩写,这一文件格式名称虽然打上了RedHat的标志, 但是其原始设计理念是开放式的,现在包括OpenLin ...

  9. 自己动手写框架——IoC的实现

    先看看 IoC百度百科 优化过程 namespace Test { class Program { static void Main(string[] args) { //场景 某公司客服要回访一些客 ...

  10. centos 下使用sublime

    CentOS 之 Sublime text3 安装及配置(不支持中文输入) sublime text 的界面友好,自动补全功能也不错. (本来用vim+php_function.txt的形式进行补全的 ...