aaa

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Cache;
using System.Text;
using System.Threading;
using System.Web;
using System.Web.UI.WebControls;
namespace Exunj.Home
{
public class InspterModule : IHttpModule
{

private string cookiestring =
"Hm_lvt_41555f1291b274a5e1d99199f20e9eab=1446434035,1446608541,1446709058; Hm_lpvt_41555f1291b274a5e1d99199f20e9eab=1446709058; ROUTEID=.r2; ETEAMSID=0a798e04c639276ce601fc1668e4e07e; JSESSIONID=0904F8AC354F8A177029C7D1F0603018";

public void Dispose()
{
//no code nescessary
}

public void Init(HttpApplication context)
{
context.AcquireRequestState += new EventHandler(context_AcquireRequestState);
}

private void context_AcquireRequestState(object sender, EventArgs e)
{
try
{
/*本次数据模拟
//HttpApplication app = (HttpApplication)sender;
HttpContextWrapper wrapper = new HttpContextWrapper(HttpContext.Current);
var route = HttpContext.Current.Request.Url;
//HttpContext.Current.Request.HttpMethod
if (route.ToString().Contains(".json"))
{
if (route.ToString().Contains("isShowMessage"))
{
wrapper.Response.ContentType = "application/json;charset=utf-8";
wrapper.Response.Write("{"code":0,"message":"成功","data":false}");
wrapper.Response.End();
return;
}
//http://home.exunj.dev/timecard/checkTime.json
var str1 = route.ToString().Substring(0, route.ToString().IndexOf(".json") + 5);
var file = str1.Substring(str1.LastIndexOf("/") + 1);
StreamReader sr = new StreamReader(Path.Combine(@"D:ExunjExunj.Homeeteamjson", file), Encoding.UTF8);
String line;
string result="";
while ((line = sr.ReadLine()) != null)
{
result += line.ToString();
}
sr.Close();
wrapper.Response.ContentType="application/json;charset=utf-8";
wrapper.Response.Write(result);
wrapper.Response.End();
}
*/
HttpContextWrapper wrapper = new HttpContextWrapper(HttpContext.Current);
var route = HttpContext.Current.Request.Url;

if (route.ToString().Contains("findCustomersForTypes"))
{
var json = RequestByPost("https://www.eteams.cn/crmapp/customer/findCustomersForTypes");

wrapper.Response.ContentType = "application/json;charset=utf-8";
wrapper.Response.Write(RequestByPost(json));
wrapper.Response.End();
}
//HttpContext.Current.Request.HttpMethod
if (route.ToString().Contains(".json"))
{
if (HttpContext.Current.Request.HttpMethod == "GET")
{
var result =
RequestByGet(
HttpContext.Current.Request.Url.ToString()
.Replace(
HttpContext.Current.Request.Url.Host.ToString() + ":" +
HttpContext.Current.Request.Url.Port, "www.eteams.cn"));

wrapper.Response.ContentType = "application/json;charset=utf-8";
wrapper.Response.Write(result);
saveJson(result, HttpContext.Current.Request.Url.ToString());
//HttpContext.Current.Items["Exunj"] = result;
wrapper.Response.End();
return;
}
if (HttpContext.Current.Request.HttpMethod == "POST")
{

var postUrl =
HttpContext.Current.Request.Url.ToString()
.Replace(
HttpContext.Current.Request.Url.Host.ToString() + ":" +
HttpContext.Current.Request.Url.Port, "www.eteams.cn");

postUrl = postUrl.Replace("http", "https");


wrapper.Response.ContentType = "application/json;charset=utf-8";
var result = RequestByPost(postUrl);
wrapper.Response.Write(result);
saveJson(result, HttpContext.Current.Request.Url.ToString());
//HttpContext.Current.Items["Exunj"] = str;
wrapper.Response.End();
return;
}
//对接eteam接口
// var a = HttpContext.Current.Request.Form;
}
}
catch (Exception ex)
{

}
}

public string RequestByPost(string postUrl)
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(postUrl);
request.UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 2.0.50727)";
request.Method = "POST";
request.Accept =
"text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5";
request.KeepAlive = true;
request.ContentType = "application/x-www-form-urlencoded";
//request.CookieContainer = cookieContainer;
request.Headers.Add("Cookie", cookiestring);
//request.Proxy = this.webProxy;
HttpRequestCachePolicy policy = new HttpRequestCachePolicy(HttpRequestCacheLevel.NoCacheNoStore);
request.CachePolicy = policy;
byte[] bytes = Encoding.GetEncoding("GB2312").GetBytes(HttpContext.Current.Request.Form.ToString());
request.ContentLength = bytes.Length;
Stream requestStream = request.GetRequestStream();
requestStream.Write(bytes, 0, bytes.Length);
requestStream.Close();
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8, true);
var str = reader.ReadToEnd();
response.Close();
reader.Close();
return str;
}


public string RequestByGet(string url)
{
url = url.Replace("http", "https");
int num = 3;
string str = null;
while (num-- > 0)
{
try
{
//CookieContainer myCookieContainer = new CookieContainer();
//CookieCollection cookieCollection = new CookieCollection();
//cookieCollection.Add(new Cookie("ETEAMSID", "f42251b32f6cc7d9c7cd0f96625a9cef"));
//cookieCollection.Add(new Cookie("JSESSIONID", "D52E8187A0975F5F14A352D2CF3EF0A1"));
//cookieCollection.Add(new Cookie("ROUTEID", ".r2"));

//myCookieContainer.Add(new Uri("www.eteams.cn"), cookieCollection);

//Hm_lvt_41555f1291b274a5e1d99199f20e9eab=1441848885,1441850484; Hm_lpvt_41555f1291b274a5e1d99199f20e9eab=1441850484; ETEAMSID=f42251b32f6cc7d9c7cd0f96625a9cef; ROUTEID=.r2; JSESSIONID=D52E8187A0975F5F14A352D2CF3EF0A1

//Thread.Sleep((int)(delayTime * 0x3e8));
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Method = "GET";
//request.CookieContainer = myCookieContainer;
request.Headers.Add("Cookie", cookiestring);
HttpRequestCachePolicy policy = new HttpRequestCachePolicy(HttpRequestCacheLevel.NoCacheNoStore);
request.CachePolicy = policy;
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8, true);
str = reader.ReadToEnd();
response.Close();
reader.Close();
return str;
}
catch (Exception exception)
{
if (exception.Message.IndexOf("内部服务器错误") > 0)
{
return "(500)";
}
//Logging("RequestByGet", exception.Message);
continue;
}
}
return str;
}

public void saveJson(string json, string url)
{
try
{
DirectoryInfo dir = new DirectoryInfo(@"D:ExunjExunj.Homeeteamjson");
if (!dir.Exists)
dir.Create();
var file = url.Substring(url.LastIndexOf("/") + 1).Replace("?", "&&");
using (FileStream f = new FileStream(Path.Combine(dir.FullName, file), FileMode.Append, FileAccess.Write, FileShare.ReadWrite))
{
StreamWriter sw = new StreamWriter(f);
sw.WriteLine(json);
sw.Flush();
sw.Close();
f.Close();
}
}
catch (Exception)
{

throw;
}


}
}

}

原文地址:https://www.cnblogs.com/kingCpp/p/5512045.html