using System;
using System.Diagnostics;
using System.IO;
using System.Net;
using System.Text;
using System.Threading;
namespace Simulation
{
class NewWebResponse
{
private HttpWebResponse _response;
private string _content;
public HttpWebResponse Response
{
get
{
return _response;
}
set
{
_response = value;
}
}
public string Content
{
get
{
return _content;
}
set
{
_content = value;
}
}
}
class Program
{
static CookieContainer cookie;
static void Main(string[] args)
{
cookie = new CookieContainer();
Thread thread = new Thread(AcceptInfo1);
thread.IsBackground = true;
thread.Start();
Console.ReadLine();
}
static void AcceptInfo1()
{
HttpListener listerner = new HttpListener();
listerner.Prefixes.Add("http://127.0.0.1:9999/");
listerner.Start();
while (true)
{
HttpListenerContext ctx = listerner.GetContext();
try
{
if (ctx.Request.Url.AbsoluteUri == "http://127.0.0.1:9999/")
{
var nresponseg = resutlRequest("host/xxx/login", string.Format("name={0}&pwd={1}", "admin", "123"), ctx.Request);
ctx.Response.ContentType = nresponseg.Response.ContentType;
using (StreamWriter writer = new StreamWriter(ctx.Response.OutputStream, Encoding.GetEncoding("GB2312")))
{
writer.Write(nresponseg.Content);
writer.Close();
ctx.Response.Close();
}
}
else
{
var rstring = "";
using (StreamReader read = new StreamReader(ctx.Request.InputStream))
{
rstring = read.ReadToEnd();
read.Close();
}
//替换网页中的连接地址指向自己的主机地址
var nresponseg = resutlRequest(ctx.Request.Url.AbsoluteUri.Replace("127.0.0.1:9999", "host"), rstring, ctx.Request);
ctx.Response.ContentType = nresponseg.Response.ContentType;
using (StreamWriter writer = new StreamWriter(ctx.Response.OutputStream, Encoding.GetEncoding("GB2312")))
{
writer.Write(nresponseg.Content);
writer.Close();
ctx.Response.Close();
}
}
}
catch (Exception ex)
{
// throw ex;
}
}
}
private static NewWebResponse resutlRequest(string url, string postData, HttpListenerRequest request)
{
try
{
HttpWebRequest nrequest = (HttpWebRequest)WebRequest.Create(url);
if (request.Url.AbsoluteUri == "http://127.0.0.1:9999/")
{
nrequest.Method = "Post";
nrequest.ContentType = "application/x-www-form-urlencoded; charset=gbk";
}
else
{
nrequest.Method = request.HttpMethod;
nrequest.ContentType = request.ContentType;
}
nrequest.Accept = request.Headers["Accept"];
nrequest.Headers["Accept-Encoding"] = request.Headers["Accept-Encoding"];
nrequest.Headers["Accept-Language"] = request.Headers["Accept-Language"];
nrequest.UserAgent = request.UserAgent;
nrequest.AllowAutoRedirect = true;//x运行重定向
nrequest.CookieContainer = cookie;
nrequest.KeepAlive = true;//建立持久性连接
//表单提交
if (nrequest.Method.ToUpper() != "GET")
{
byte[] bytepostData = Encoding.GetEncoding("GB2312").GetBytes(postData);
nrequest.ContentLength = bytepostData.Length;
using (Stream requestStm = nrequest.GetRequestStream())
{
requestStm.Write(bytepostData, 0, bytepostData.Length);
}
}
HttpWebResponse response = (HttpWebResponse)nrequest.GetResponse();
string retString = "";
using (Stream myResponseStream = response.GetResponseStream())
{
StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("GB2312"));
retString = myStreamReader.ReadToEnd();
}
NewWebResponse nresponse = new NewWebResponse();
//替换回原理的地址
retString = retString.Replace("host", "127.0.0.1:9999");
nresponse.Response = response;
nresponse.Content = retString;
return nresponse;
}
catch (Exception ex)
{
Debug.WriteLine(ex.Message);
throw ex;
}
}
}
}
由于编码转为string会破坏图片的格式,所以这里只能正常显示网页的文字信息,如果想中转图片,可以尝试用直接转发字节流,但是这样修改网页中资源的地址可能就比较麻烦了。
C#实现会员内容的转发
最新推荐文章于 2021-12-13 00:13:00 发布