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. Html.ActionLink简单用法(转)

    一 Html.ActionLink("要显示的文字","actionName") 该重载的第一个参数是该链接要显示的文字,第二个参数是对应的控制器的方法, 默认 ...

  2. (转)HTML特殊字符

    HTML 原始码 显示结果 描述 < < 小於号或显示标记 > > 大於号或显示标记 & & 可用於显示其它特殊字符 " " 引号 ® ® ...

  3. java SSH整合配置

    web.xml <?xml version="1.0" encoding="UTF-8"?> <web-app version="3 ...

  4. W3C小组宣布:HTML5标准制定完成

    近日,W3C小组宣布已经完成对HTML5标准以及Canvas 2D性能草案的制定,这就意味着开发人员将会有一个稳定的“计划和实施”目标. Web性能工作组已经推出W3C的两个版本建议草案. Navig ...

  5. 【转】三十分钟学会STL算法

    转载自: http://net.pku.edu.cn/~yhf/UsingSTL.htm 这是本小人书.原名是<using stl>,不知道是谁写的.不过我倒觉得很有趣,所以化了两个晚上把 ...

  6. c - 水仙花数.

    #include <stdio.h> #include <math.h> /* *打印出所有的“水仙花数” ,所谓“水仙花数”是指一个三位数,其各位数字立方和等于该数本身. * ...

  7. maven实现tomcat热部署

    1.使用maven+tomcat事项热部署 1.1修改tomcat-user.xml <role rolename="manager-gui"/> <!--man ...

  8. mysql查询计划

    mysql查询计划 1:客户端发起查询请求 2:服务器接收到请求后,先查询缓存 如果缓存命中,直接返回数据给客户端 否则,解析sql 3:sql解析完成后,进行预处理 4:有查询优化器生存查询计划 5 ...

  9. Asp.net Repeater控件

    Repeater控件和DataList控件,可以用来一次显示一组数据项.比如,可以用它们显示一个数据表中的所有行.     Repeater控件完全由模板驱动,提供了最大的灵活性,可以任意设置它的输出 ...

  10. php插入转义与查找转义

    //转义用于查找 function deepslashes($data) { //判断data表现形式 if(empty($data)) { return $data; } return is_arr ...