JS跨域代码片段_javascript技巧
程序员文章站
2022-04-30 19:02:06
...
下面的代码块是js调用一般处理程序的代理来实现js跨域的。如果js需要多次跨域,推荐下面的方法。
public string GetInfo(HttpContext context)
{
string post = "a=XX&b=XX";
return CreateHttpRequest("https://www.XXXX.com", post, "POST");
}
#region 构造请求
///
/// 构造请求
///
/// 请求地址
/// 请求参数
/// 请求方式
///
public string CreateHttpRequest(string requestUrl, string requestParam, string requestMethod)
{
try
{
System.Net.HttpWebRequest request = System.Net.HttpWebRequest.Create(requestUrl) as System.Net.HttpWebRequest;
request.Method = requestMethod;
string post = requestParam;
byte[] bytes = System.Text.Encoding.UTF8.GetBytes(post);
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = bytes.Length;
System.IO.Stream stream = request.GetRequestStream();
stream.Write(bytes, 0, bytes.Length);
System.Net.HttpWebResponse response = request.GetResponse() as System.Net.HttpWebResponse;
System.IO.StreamReader sr = new System.IO.StreamReader(response.GetResponseStream(), System.Text.Encoding.UTF8);
return sr.ReadToEnd();
}
catch (Exception)
{
return "";
}
}
#endregion
复制代码 代码如下:
public string GetInfo(HttpContext context)
{
string post = "a=XX&b=XX";
return CreateHttpRequest("https://www.XXXX.com", post, "POST");
}
#region 构造请求
///
/// 构造请求
///
/// 请求地址
/// 请求参数
/// 请求方式
///
public string CreateHttpRequest(string requestUrl, string requestParam, string requestMethod)
{
try
{
System.Net.HttpWebRequest request = System.Net.HttpWebRequest.Create(requestUrl) as System.Net.HttpWebRequest;
request.Method = requestMethod;
string post = requestParam;
byte[] bytes = System.Text.Encoding.UTF8.GetBytes(post);
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = bytes.Length;
System.IO.Stream stream = request.GetRequestStream();
stream.Write(bytes, 0, bytes.Length);
System.Net.HttpWebResponse response = request.GetResponse() as System.Net.HttpWebResponse;
System.IO.StreamReader sr = new System.IO.StreamReader(response.GetResponseStream(), System.Text.Encoding.UTF8);
return sr.ReadToEnd();
}
catch (Exception)
{
return "";
}
}
#endregion
推荐阅读
-
学习drag and drop js实现代码经典之作_javascript技巧
-
JS不能跨域借助jquery获取IP地址的方法_javascript技巧
-
springcloud Springboot vue.js Activiti6 前后分离 跨域 工作流 集成代码生成器 shiro权限
-
Springboot Activiti6 工作流 集成代码生成器 shiro 权限 vue.js html 跨域 前后分离
-
PHP json格式和js json格式 js跨域调用实现代码
-
javascript简写常用的12个技巧(可以大大减少你的js代码量)
-
Springboot vue.js html 跨域 前后分离 Activiti6 工作流 集成代码生成器 shiro 权限
-
Js代码优化规则和技巧(JavaScript优化)
-
OA办公系统 Springboot Activiti6 工作流 集成代码生成器 vue.js 前后分离 跨域
-
让textarea自动调整大小的js代码_javascript技巧