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. PHP 类型比较表

    以下的表格显示了 PHP 类型和比较运算符在松散和严格比较时的作用.该补充材料还和类型戏法的相关章节内容有关.同时,大量的用户注释和 » BlueShoes 的工作也给该材料提供了帮助. 在使用这些表 ...

  2. 搭建ngrok服务器(ubuntu 14)-- 微信 80端口和IPC备案限制解决方案

    概述: ngrok其实这东西,我也不是很懂,所以也直接跟大家说,这就是个类似花生壳的东西. 简单来说,它就好像把我们内网自己使用的电脑和服务器用vpn连接起来,然后你的电脑就可以从互联网来访问了,有个 ...

  3. (三)Knockout - ViewModel 的使用2 - select、list 应用

    select下拉菜单 <select>常用的data-bind参数: •options : 指向数组或ko.observableArray(),KO会将数组元素转换为下拉选项.如果是ko. ...

  4. Android环境搭建-Eclipse-Luna

    所需软件:eclipse-jee-luna-R-win32-x86_64 所需插件:Adt 23.02 其他:Android SDK 环境搭建-JDK安装.Eclipse安装 配置安卓环境变量: cm ...

  5. treeview递归

    1.数据库 table A( ID int pk, Value varchar, Fid int ) A: ID   Value    Fid 1   value1     0 2   value2  ...

  6. C#中启动外部应用程序

    C#中我们可以通过Process类直接启动外部应用程序 代码如下: Process p = new Process();                    p.StartInfo.FileName ...

  7. 模块SEO优化中{分类名称}分隔符去掉及只调用下级分类方法

    if($catid) { if($CAT['parentid']) { $seo_catname = ''; $tmp = strip_tags(cat_pos($CAT, 'DESTOON')); ...

  8. NET调用Java之100-Continue的坑

    场景:这段时间开发的时候遇到了需要NET调用java的restful api的情况,java端用的服务器是tomcat,框架是spring boot,net调用java端的接口之后只要java端的接口 ...

  9. $ npm install opencv ? 你试试?! 在windows环境下,使用node.js调用opencv攻略

    博主之前写过一篇文章<html5与EmguCV前后端实现——人脸识别篇>,叙述的是opencv和C#的故事.最近在公司服务器上更新了一套nodejs环境,早就听闻npm上有opencv模块 ...

  10. zoj 3647 Gao the Grid

    http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=4837 先求出从所有点随机找出三个点的组合数,然后去掉共线的,平行好去掉,斜线就 ...