c# 后台GET、POST、PUT、DELETE传输发送json数据
一、Get 方式传输
//url为请求的网址,param参数为需要查询的条件(服务端接收的参数,没有则为null)
//返回该次请求的响应
public string HttpGet(string url, Dictionary<String, String> param)
{
if (param != null) //有参数的情况下,拼接url
{
url = url + "?";
foreach (var item in param)
{
url = url + item.Key + "=" + item.Value + "&";
}
url = url.Substring(, url.Length - );
}
HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;//创建请求
request.Method = "GET"; //请求方法为GET
HttpWebResponse res; //定义返回的response
try
{
res = (HttpWebResponse)request.GetResponse(); //此处发送了请求并获得响应
}
catch (WebException ex)
{
res = (HttpWebResponse)ex.Response;
}
StreamReader sr = new StreamReader(res.GetResponseStream(), Encoding.UTF8);
string content = sr.ReadToEnd(); //响应转化为String字符串
return content;
}
二、POST 方式传输
public static string HttpPost(string url, Dictionary<String, String> param)
{
HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest; //创建请求
CookieContainer cookieContainer = new CookieContainer();
request.CookieContainer = cookieContainer;
request.AllowAutoRedirect = true;
//request.AllowReadStreamBuffering = true;
request.MaximumResponseHeadersLength = ;
request.Method = "POST"; //请求方式为post
request.AllowAutoRedirect = true;
request.MaximumResponseHeadersLength = ;
request.ContentType = "application/json";
JObject json = new JObject();
if (param.Count != ) //将参数添加到json对象中
{
foreach (var item in param)
{
json.Add(item.Key, item.Value);
}
}
string jsonstring = json.ToString();//获得参数的json字符串
byte[] jsonbyte = Encoding.UTF8.GetBytes(jsonstring);
Stream postStream = request.GetRequestStream();
postStream.Write(jsonbyte, , jsonbyte.Length);
postStream.Close();
//发送请求并获取相应回应数据
HttpWebResponse res;
try
{
res = (HttpWebResponse)request.GetResponse();
}
catch (WebException ex)
{
res = (HttpWebResponse)ex.Response;
}
StreamReader sr = new StreamReader(res.GetResponseStream(), Encoding.UTF8);
string content = sr.ReadToEnd(); //获得响应字符串
return content;
}
其中PUT、DELETE方式跟上面基本相似。这里就不再多说明
c# 后台GET、POST、PUT、DELETE传输发送json数据的更多相关文章
- python 全栈开发,Day75(Django与Ajax,文件上传,ajax发送json数据,基于Ajax的文件上传,SweetAlert插件)
昨日内容回顾 基于对象的跨表查询 正向查询:关联属性在A表中,所以A对象找关联B表数据,正向查询 反向查询:关联属性在A表中,所以B对象找A对象,反向查询 一对多: 按字段:xx book ----- ...
- Django与Ajax,文件上传,ajax发送json数据,基于Ajax的文件上传,SweetAlert插件
一.Django与Ajax AJAX准备知识:JSON 什么是 JSON ? JSON 指的是 JavaScript 对象表示法(JavaScript Object Notation) JSON 是轻 ...
- JSON的简单使用_向前台发送JSON数据
转自:http://www.cnblogs.com/digdeep/p/5574366.html 1.前台页面 <%@ page language="java" conten ...
- Django中数据传输编码格式、ajax发送json数据、ajax发送文件、django序列化组件、ajax结合sweetalert做二次弹窗、批量增加数据
前后端传输数据的编码格式(contentType) 提交post请求的两种方式: form表单 ajax请求 前后端传输数据的编码格式 urlencoded formdata(form表单里的) ja ...
- SpringMVC客户端发送json数据时报400错误
当测试客户端发送json数据给服务器时,找不到响应路径? 原来是参数类型不符,即使是json也要考虑参数的个数和类型 解决:将age请求参数由"udf"改为"3" ...
- iOS开发网络篇—发送json数据给服务器以及多值参数
iOS开发网络篇—发送json数据给服务器以及多值参数 一.发送JSON数据给服务器 发送JSON数据给服务器的步骤: (1)一定要使用POST请求 (2)设置请求头 (3)设置JSON数据为请求体 ...
- 【转】iOS开发网络篇—发送json数据给服务器以及多值参数
原文: http://www.cnblogs.com/wendingding/p/3950132.html 一.发送JSON数据给服务器 发送JSON数据给服务器的步骤: (1)一定要使用POST请求 ...
- perl post发送json数据
sub wx_init { #$login_url ="https://wx.qq.com/cgi-bin/mmwebwx-bin/webwxinit?r=- ...
- IOS-网络(发送JSON数据给服务器和多值参数)
三步走: 1.使用POST请求 2.设置请求头 [request setValue:@"application/json" forHTTPHeaderField:@"Co ...
随机推荐
- TLS协议扫盲(握手,非对称加密,证书,电子签名等)
想学习TLS协议最好的方法应该是去看RFC,但如果对安全传输协议没有一些基本认识的人很难一上来就读懂RFC里面的种种细节和设计原则,所以这里为了能够进一步去弄懂TLS协议,把一些基本的知识放在这里,算 ...
- 28.OGNL与ValueStack(VS)-总结$ # %的区别
转自:https://wenku.baidu.com/view/84fa86ae360cba1aa911da02.html $用于i18n和struts配置文件 #取得ActionContext的值 ...
- 跟我学算法- tensorflow VGG模型进行测试
我们使用的VGG模型是别人已经训练好的一个19层的参数所做的一个模型 第一步:定义卷积分部操作函数 mport scipy.io import numpy as np import os import ...
- sql注入及事务
Statement会有一个关于sql注入的bug ,所以基本不使用 一般使用PreparedStatement import java.sql.Connection;import java.sql.P ...
- 14.Longest Common Prefix (String)
Write a function to find the longest common prefix string amongst an array of strings. class Solutio ...
- [leetcode]621. Task Scheduler任务调度
Given a char array representing tasks CPU need to do. It contains capital letters A to Z where diffe ...
- cmake条件编译
CMake的条件编译基于if elseif endif.3.0版本具体语法如下 if(expression) # then section. COMMAND1(ARGS ...) COMMAND2(A ...
- Linq多字段排序
var q = db.Customers.OrderBy(c => c.City).ThenBy(c => c.ContactName).ToList(); var q = from it ...
- netstat/lsof
netstat/lsof netstat命令用于显示与IP.TCP.UDP和ICMP协议相关的统计数据,一般用于检验本机各端口的网络连接情况 -a 显示一个所有的有效连接信息列表(包括已建立的连接,也 ...
- rsync同步常用命令[转载]
转载:http://blog.csdn.net/niushuai666/article/details/16880061 如果你是一位运维工程师,你很可能会面对几十台.几百台甚至上千台服务器,除了批量 ...