补充 宝玉 之《URL欺骗之以假乱真!》之 POST 方法提交数据 之 处理!
補(bǔ)充 寶玉 之《URL欺騙之以假亂真!》之 POST 方法提交數(shù)據(jù) 之 處理!
(之乎者也,怪別扭的!)
我們首先搭建一個(gè)環(huán)境:
IIS 代理一下 Resin 下的 jsp 頁(yè)面! (Resin 乃是 Java 應(yīng)用服務(wù)器)
1.Resin 獲取:
http://www.caucho.com/download/resin-2.1.16.zip
下載后解壓某目錄,如:
E:/Resin/resin-2.1.16/
2.配置 Resin? (Java SDK 的配置就不贅述)
用文本編輯器打開(kāi):
E:/Resin/resin-2.1.16/conf/resin.conf
<http port='8080'/>
可配置端口,resin 默認(rèn)的是 8080 !
如果可用就不改!
<welcome-file-list>simplepost.jsp ,index.xtp, index.jsp, index.html </welcome-file-list>
可配置默認(rèn)歡迎頁(yè)面!
3.在 E:/Resin/resin-2.1.16/webapps/ 下創(chuàng)建新目錄 test:
編寫(xiě) E:/Resin/resin-2.1.16/webapps/test/index.jsp 并保存!
E:/Resin/resin-2.1.16/webapps/test/? 就是一個(gè) Application
<%@ page import='java.util.*'%>
<HTML>
<head>
<title>test</title>
</head>
<h1>Form Values:</h1>
<table>
<%
Enumeration e = request.getParameterNames();
while (e.hasMoreElements())
{
?String name = (String) e.nextElement();
?String[] values = (String[]) request.getParameterValues(name);
?%>
?<tr><td>
?<%= name %><td><%= values[0] %>
?<%
?if (values.length > 1)
?{
??for (int i = 1; i < values.length; i++)
??{
???%>,<%= values[i] %><%
??}
?}
}
%>
</table>
<form action='http://localhost' method='post'>
<input name='Comment' type="text" value="<%= request.getParameter("Comment") %>">
<input name='Comment2' type="text" value="<%= request.getParameter("Comment2") %>">
<input type=submit>
</form>
</body>
</HTML>
4.編寫(xiě) C# Openlab.UrlCheat.cs : (修改寶玉的代碼基礎(chǔ)上完成POST操作的處理)
//Openlab.UrlCheat.cs
//命令行編譯: csc /t:library Openlab.UrlCheat.cs
//生成的 Openlab.UrlCheat.dll 存放在 IIS 主目錄下建一個(gè) bin 目錄!
//如: C:/Inetpub/wwwroot/bin/Openlab.UrlCheat.dll
namespace Openlab.UrlCheat
{
?using System;
?using System.Configuration;
?using System.Web;
?using System.IO;
?using System.Net;
?using System.Text;
?//原 Globals.cs
?/// <summary>
?/// Summary description for Globals.
?/// </summary>
?public class Globals
?{
??// 默認(rèn)域名為博客堂
??private static string defaultDomain = "blog.joycode.com";
??public static bool IsNullorEmpty(string text)
??{
???if (text != null)
???{
????return (text.Trim() == string.Empty);
???}
???return true;
??}
??/// <summary>
??/// 根據(jù)Url返回HttpWebResponse對(duì)象
??/// </summary>
??/// <param name="url"></param>
??/// <returns></returns>
??public static HttpWebResponse WebResponse(string url)
??{
???HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
???HttpWebResponse response = (HttpWebResponse)request.GetResponse();
???return response;
??}
??/// <summary>
??/// 獲取網(wǎng)頁(yè)的編碼信息
??/// </summary>
??/// <param name="response"></param>
??/// <returns></returns>
??/// <remarks>如果是網(wǎng)頁(yè),則其ConentType中含有編碼信息,如"text/html; charset=utf-8"</remarks>
??public static Encoding GetEncoding(HttpWebResponse response)
??{
???string name = response.ContentEncoding;
???Encoding code = Encoding.Default;
???if (name == "")
???{
????string contentType = response.ContentType;
????if (contentType.ToLower().IndexOf("charset") != -1)
????{
?????name = contentType.Substring(contentType.ToLower().IndexOf("charset=") + "charset=".Length);
????}
???}
???if (name != "")
???{
????try
????{
?????code = Encoding.GetEncoding(name);
????}
????catch{}
???}
???return code;
??}
??/// <summary>
??/// 獲取文本內(nèi)容
??/// </summary>
??/// <param name="response"></param>
??/// <returns></returns>
??public static string TextContent(HttpWebResponse response)
??{
???string buffer = "",line;
???Stream stream = response.GetResponseStream();
???StreamReader reader = new StreamReader(stream, GetEncoding(response));
??
???//buffer = "<base href=http://localhost:1080 />";
???while( (line = reader.ReadLine())!=null )
???{
???
????buffer += line + "/r/n";
???}
???stream.Close();
???return buffer;
??}
??//post Copy & paste from QuickStart by playyuer $ at $ Microshaoft.com
??//http://chs.gotdotnet.com/quickstart/util/srcview.aspx?path=/quickstart/howto/samples/net/WebRequests/clientPOST.src&file=CS/clientpost.cs&font=3
??//另外本人還有一篇老帖可參考
??// 《C# 寫(xiě)的 HttpRequsetResponse 類(lèi),異步、事件... 還熱乎著呢!》
??// http://blog.csdn.net/playyuer/archive/2003/07/03/2856.aspx
??public static string getPage(string url,string payload)
??{
???System.Net.WebResponse result = null;
???string s = "";
???try
???{
????WebRequest req = WebRequest.Create(url);
????req.Method = "POST";
????req.ContentType = "application/x-www-form-urlencoded";
????StringBuilder UrlEncoded = new StringBuilder();
????Char[] reserved = {'?', '=', '&'};
????byte[] SomeBytes = null;
????if (payload != null)
????{
?????int i=0, j;
?????while(i<payload.Length)
?????{
??????j=payload.IndexOfAny(reserved, i);
??????if (j==-1)
??????{
???????UrlEncoded.Append(HttpUtility.UrlEncode(payload.Substring(i, payload.Length-i)));
???????break;
??????}
??????UrlEncoded.Append(HttpUtility.UrlEncode(payload.Substring(i, j-i)));
??????UrlEncoded.Append(payload.Substring(j,1));
??????i = j+1;
?????}
?????SomeBytes = Encoding.UTF8.GetBytes(UrlEncoded.ToString());
?????req.ContentLength = SomeBytes.Length;
?????Stream newStream = req.GetRequestStream();
?????newStream.Write(SomeBytes, 0, SomeBytes.Length);
?????newStream.Close();
????}
????else
????{
?????req.ContentLength = 0;
????}
????result = req.GetResponse();
????Stream ReceiveStream = result.GetResponseStream();
????Encoding encode = System.Text.Encoding.GetEncoding("utf-8");
????StreamReader sr = new StreamReader( ReceiveStream, encode );
????Console.WriteLine("/r/n已接收到響應(yīng)流");
????Char[] read = new Char[256];
????int count = sr.Read( read, 0, 256 );
????Console.WriteLine("HTML.../r/n");
????while (count > 0)
????{
?????String str = new String(read, 0, count);
?????Console.Write(str);
?????// add by playyuer
?????s += str;
?????count = sr.Read(read, 0, 256);
????}
????Console.WriteLine("");
????return s;
???}
???catch(Exception e)
???{
????Console.WriteLine( e.ToString());
????Console.WriteLine("/r/n找不到請(qǐng)求 URI,或者它的格式不正確");
????return s;
???}
???finally
???{
????if ( result != null )
????{
?????result.Close();
????}
???
???}
??}
??/// <summary>
??/// 域名
??/// </summary>
??/// <remarks>如果支持泛域名解析,那么也可以做到1bu.com那樣的效果:)</remarks>
??public static string Domain
??{
???get
???{
????string domain = ConfigurationSettings.AppSettings["Domain"];
????if (domain == null || domain == "")
?????domain = defaultDomain;
????return domain;
???}
??}
??/// <summary>
??/// 域名的URL
??/// </summary>
??public static string DomainUrl
??{
???get
???{
????string url = Domain.ToLower();
????if (!url.StartsWith("http://"))
????{
?????url = "http://" + url;
????}
????if (!url.EndsWith("/"))
????{
?????url = url + "/";
????}
????return url;
???}
??}
??/// <summary>
??/// 真實(shí)地址
??/// </summary>
??/// <param name="rawUrl"></param>
??/// <returns></returns>
??public static string RealUrl(string rawUrl)
??{
???string realUrl;
???realUrl = Globals.DomainUrl + rawUrl.TrimStart('/');
???return realUrl;
??}
??static public string ApplicationPath
??{
???get
???{
????string applicationPath = HttpContext.Current.Request.ApplicationPath;
????if (applicationPath == "/")
????{
?????return string.Empty;
????}
????else
????{
?????return applicationPath.ToLower();
????}
???}
??}
?}
?//原 Redirect.cs
?/// <summary>
?/// 轉(zhuǎn)向URL
?/// </summary>
?public class Redirect : IHttpHandler
?{
??public Redirect()
??{
???
??}
??public void ProcessRequest(HttpContext context)
??{
???string rawUrl = context.Request.RawUrl.ToLower();
???string realUrl = rawUrl;
???if (!Globals.IsNullorEmpty(rawUrl))
???{
????if (!rawUrl.StartsWith("http://"))
????{
?????realUrl = Globals.RealUrl(rawUrl);
????}
???}
???context.Response.Redirect(realUrl);
???context.Response.End();
??}
??// Properties
??public bool IsReusable
??{
???get
???{
????return false;
???}
??}
?}
?//原 WebResponse.cs
?/// <summary>
?/// 輸出內(nèi)容
?/// </summary>
?public class WebResponse : IHttpHandler
?{
??
??public void ProcessRequest(HttpContext context)
??{
???string rawUrl = context.Request.RawUrl.ToLower();
???if (!Globals.IsNullorEmpty(rawUrl))
???{
????// 如果是"http://"開(kāi)頭則表示是絕對(duì)路徑,直接跳轉(zhuǎn)即可
????if (!rawUrl.StartsWith("http://"))
????{
?????string realUrl = Globals.RealUrl(rawUrl);
?????//add by playyuer $ at $ Microshaoft.com
?????if (context.Request.HttpMethod.ToLower() == "get")
?????{
??????HttpWebResponse response = Globals.WebResponse(realUrl);
??????// 如果不是文本類(lèi)型的,就跳轉(zhuǎn)
??????if (!response.ContentType.ToLower().StartsWith("text/"))
???????context.Response.Redirect(realUrl);
??????// 文本類(lèi)型則先獲取文本內(nèi)容,然后直接輸出到瀏覽.
??????string content = Globals.TextContent(response);
??????context.Response.Write(content);
?????}
??????//add by playyuer $ at $ Microshaoft.com
?????else //post
?????{
??????string S = "";
??????foreach (string s in context.Request.Form.AllKeys)
??????{
???????if (S.Length > 0)
????????S += "&";
???????S += s + "=" + context.Request.Form[s];
??????}
??????//context.Response.Write(S);
??????context.Response.Write(Globals.getPage(realUrl,S));
?????}
????}
????else
????{
?????context.Response.Redirect(rawUrl);
????}
???}
???else
???{
????//
???}
???context.Response.End();
??}
??public bool IsReusable
??{
???get
???{
????return false;
???}
??}
?}
}
4.IIS 主目錄下的 web.Config ,如: C:/Inetpub/wwwroot/web.Config :
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
?<system.web>
??<compilation defaultLanguage="c#" debug="true" />
??<httpHandlers>
???<add verb="*" path="*.gif" type="Openlab.UrlCheat.Redirect, Openlab.UrlCheat" />
???<add verb="*" path="*.jpg" type="Openlab.UrlCheat.Redirect, Openlab.UrlCheat" />
???<add verb="*" path="*.jpeg" type="Openlab.UrlCheat.Redirect, Openlab.UrlCheat" />
???<add verb="*" path="*.rar" type="Openlab.UrlCheat.Redirect, Openlab.UrlCheat" />
???<add verb="*" path="*.zip" type="Openlab.UrlCheat.Redirect, Openlab.UrlCheat" />
???<add verb="*" path="*.webinfo" type="System.Web.HttpForbiddenHandler"/>
???<add verb="GET" path="*" type="Openlab.UrlCheat.WebResponse, Openlab.UrlCheat" />
???<add verb="POST" path="*" type="Openlab.UrlCheat.WebResponse, Openlab.UrlCheat" />
??</httpHandlers>
?</system.web>
?<appSettings>
??<!-- 您可以修改這個(gè)域名 -->
??<add key="Domain" value="http://localhost:8080/test/" />
?</appSettings>
</configuration>
5.運(yùn)行 Resin 及 IIS
E:/Resin/resin-2.1.16/bin/httpd.exe
6.在 IE 地址欄訪(fǎng)問(wèn): http://localhost
提交一些數(shù)據(jù)測(cè)試!看看回顯是否正確?!
我測(cè)的夠慢的!
另外寶玉和我都沒(méi)考慮替換 href? 的 url 處理!
轉(zhuǎn)載于:https://www.cnblogs.com/Microshaoft/archive/2005/02/25/2485739.html
總結(jié)
以上是生活随笔為你收集整理的补充 宝玉 之《URL欺骗之以假乱真!》之 POST 方法提交数据 之 处理!的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: IronSoft ASP系列组件,年前最
- 下一篇: 好香,方便面