.net服务器端发起请求封装
写一个静态类封装类似客户端的请求
public static class HttpHelper
{
#region Get
public static string HttpGet(string url, Encoding encoding = null)
{
WebClient client = new WebClient
{
Encoding = encoding ?? Encoding.UTF8
};
return client.DownloadString(url);
} /// <summary>
/// Get请求的泛型类返回固定类型
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="url"></param>
/// <param name="encoding"></param>
/// <returns></returns>
public static T GetJson<T>(string url, Encoding encoding = null)
{
string input = HttpGet(url, encoding);
JavaScriptSerializer serializer = new JavaScriptSerializer(); return serializer.Deserialize<T>(input);
} /// <summary>
/// Get请求返回json字符串
/// </summary>
/// <param name="url"></param>
/// <param name="encoding"></param>
/// <returns></returns>
public static string GetJsonStr(string url, Encoding encoding = null)
{
string input = HttpGet(url, encoding);
return input;
}
#endregion #region Post
/// <summary>
/// 构造url的参数ajax的data值
/// </summary>
/// <param name="dic"></param>
/// <returns></returns>
public static string GetUrlStr(Dictionary<string, string> dic)
{
StringBuilder sb = new StringBuilder();
foreach (var item in dic)
{
sb.AppendFormat("{0}={1}&", item.Key, item.Value);
}
string urlStr = sb.Remove(sb.Length - , ).ToString();
return urlStr;
} /// <summary>
/// 发送post请求
/// </summary>
/// <param name="postData"></param>
/// <param name="postUrl"></param>
/// <returns></returns>
public static string PostJsonStr(string postData, string postUrl)
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(postUrl);
byte[] data = Encoding.GetEncoding("gbk").GetBytes(postData);
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = data.Length;
using (Stream stream = request.GetRequestStream())
{
stream.Write(data, , data.Length);
}
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
string responseString = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding("utf-8")).ReadToEnd();
return responseString;
} #endregion
}
注意引用的命名空间
using AutoO2O.BLL;
using AutoO2O.Common;
using AutoO2O.Models;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading;
using System.Timers;
using System.Web.Mvc;
将请求的结果转化输出给ajax请求
protected string Write<T>(string callBack, string paramName, object o)
{
Newtonsoft.Json.JsonSerializerSettings jSetting = new Newtonsoft.Json.JsonSerializerSettings();
jSetting.NullValueHandling = NullValueHandling.Ignore;
if (string.IsNullOrEmpty(callBack) && string.IsNullOrEmpty(paramName))
return JsonConvert.SerializeObject(o, jSetting);
else if (!string.IsNullOrEmpty(callBack))
return callBack + "(" + JsonConvert.SerializeObject(o, jSetting) + ");";
else
return "var " + paramName + " = " + JsonConvert.SerializeObject(o, jSetting) + ";";
}
辅助测试
public class MyClass
{
public string Name { get; set; }
public string JC { get; set; } }
public string MyTest()
{
string callBack = Request["callback"] + "";
//string ss =GetJsonStr("http://192.168.5.88:8888/test/GetD/24");
//return Write<MyClass>(callBack, "", ss);
Dictionary<string, string>dic=new Dictionary<string,string>();
dic.Add("id","");
string param = HttpHelper.GetUrlStr(dic);
string result = HttpHelper.PostJsonStr(param, "http://192.168.5.88:8888/test/GetD");
return Write<string>(callBack, "", result);
}
静态页面请求
$.ajax({
url: 'http://192.168.5.88:8022/test/MyTest',
dataType: 'jsonp',
contetType:'text',
success: function (res) {
debugger;
}
})
调试

显然请求成功
.net服务器端发起请求封装的更多相关文章
- CTF SSRF(服务器端伪造请求)
目录 CTF SSRF(服务器端伪造请求) 一.概念 二.危害 三.漏洞挖掘与判断 四.相关函数 五.IP绕过 六.Gopher协议 1.使用限制 2.构造payload CTF SSRF(服务器端伪 ...
- Elasticsearch High Level Rest Client 发起请求的过程分析
本文讨论的是JAVA High Level Rest Client向ElasticSearch6.3.2发送请求(index操作.update.delete--)的一个详细过程的理解,主要涉及到Res ...
- PHP Request请求封装
/** * Request请求封装 * Class Request * @package tool */ class Request { // curl 请求错误码 protected static ...
- axios请求封装和异常统一处理
前端网络请求封装前端采用了axios来处理网络请求,为了避免在每次请求时都去判断各种各样的网络情况,比如连接超时.服务器内部错误.权限不足等等不一而足,我对axios进行了简单的封装,这里主要使用了a ...
- Vue: axios 请求封装及设置默认域名前缀 (for Vue 2.0)
1. 实现效果 以get方法向http://192.168.32.12:8080/users 发起请求.获取数据并进行处理 this.apiGet('/users', {}) .then((res) ...
- javascript 异步请求封装成同步请求
此方法是异步请求封装成同步请求,加上token验证,环境试用微信小程序,可以修改文件中的ajax,进行封装自己的,比如用axios等 成功码采用标准的 200 到 300 和304 ,需要可以自行修改 ...
- flutter dio网络请求封装实现
flutter dio网络请求封装实现 文章友情链接: https://juejin.im/post/6844904098643312648 在Flutter项目中使用网络请求的方式大致可分为两种 ...
- Ajax向服务器端发送请求
Ajax向服务器端发送请求 Ajax的应用场景 页面上拉加载更多数据 列表数据无刷新分页 表单项离开焦点数据验证 搜索框提示文字下拉列表 Ajax运行原理 Ajax 相当于浏览器发送请求与接收响应的代 ...
- Vue2使用Axios发起请求教程详细
当你看到该文章时希望你已知晓什么是跨域请求以及跨域请求的处理,本文不会赘述 本文后台基于Springboot2.3进行搭建,Controller中不会写任何业务逻辑仅用于配合前端调试 Controll ...
随机推荐
- 【wordpress】 $wpdb 应用实例
<?php require_once('e:/php/wordpress/wp-blog-header.php');//注释掉这一句就出错了 global $wpdb; $a = $wpdb-& ...
- codeforces734E
题目连接:http://codeforces.com/contest/734/problem/E E. Anton and Tree time limit per test 3 seconds mem ...
- HDU 2523 sort (hash)
#include<iostream> #include<cstring> #include<cmath> #include<cstdio> using ...
- NOIP2018提高组模拟题(四)
能量(energy) Description 有一块能量田,它的形状是 n*m的矩形,每一个格子上都有一个能量值 a[x][y] (可正可负).一块矩形田的能量定义为它的每个格子的能量值之和. ...
- 利用Java的反射与代理机制实现AOP
在上一篇文章中,我们讲述了利用Java的反射机制中实现Spring中的IOC,在本文中,我们将更进一步,讲述用Java的反射和动态代理机制来实现Spring的AOP. 一.AOP概述 AOP(Aspe ...
- [Atcoder Regular Contest 063] Tutorial
Link: ARC063 传送门 C: 将每种颜色的连续出现称为一段,寻找总段数即可 #include <bits/stdc++.h> using namespace std; ,len; ...
- POJ 2186 Popular Cows(强连通分量)
[题目链接] http://poj.org/problem?id=2186 [题目大意] 给出一张有向图,问能被所有点到达的点的数量 [题解] 我们发现能成为答案的,只有拓扑序最后的SCC中的所有点, ...
- python3中zipfile模块的常用方法
一.zipfile模块的简述 zipfile是python里用来做zip格式编码的压缩和解压缩的,由于是很常见的zip格式,所以这个模块使用频率也是比较高的, 在这里对zipfile的使用方法做一些记 ...
- Delphi 二维码产生和扫描
Zint用于产生二维码. Zxing用读取二维码. VFrames.pas和VSample.pas用于摄像头. 另附带摄像头相关的类库,也可用开源的dspack也可用于摄像头的需求. 以上为开源的信息 ...
- 【R笔记】R语言进阶之4:数据整形(reshape)
R语言进阶之4:数据整形(reshape) 2013-05-31 10:15 xxx 网易博客 字号:T | T 从不同途径得到的数据的组织方式是多种多样的,很多数据都要经过整理才能进行有效的分析,数 ...