<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>WilliamGates Blog &#187; WebService</title>
	<atom:link href="http://blog.williamgates.biz/tag/webservice/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.williamgates.biz</link>
	<description>四片叶子的三叶草</description>
	<lastBuildDate>Fri, 11 Nov 2011 14:32:46 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>.Net下HTTP访问穿越多层代理的方法以及代理服务器的验证</title>
		<link>http://blog.williamgates.biz/2006/07/aspdotnet-through-multi-proxy/</link>
		<comments>http://blog.williamgates.biz/2006/07/aspdotnet-through-multi-proxy/#comments</comments>
		<pubDate>Thu, 13 Jul 2006 13:17:06 +0000</pubDate>
		<dc:creator>WG</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[WebService]]></category>
		<category><![CDATA[技术心得]]></category>
		<category><![CDATA[proxy]]></category>
		<category><![CDATA[代理]]></category>
		<category><![CDATA[技术]]></category>

		<guid isPermaLink="false">http://blog.williamgates.biz/2006/07/13/31</guid>
		<description><![CDATA[　　我们学校的网络环境是，所有的出校访问均须通过代理服务器（我们叫作sproxy），并且不能连接国外网站。为此，我想要做一个自动抓取和验证出国代理列表的WebService。我们所有的验证代理的请求，都需要通过两层代理，最终通到用来测试的网站（我使用了www.redhat.com） 　　技术上的思路是，通过连接第一层代理sproxy（支持http tunnel），给第二层代理发送GET指令，从而完成对目标网页的访问。 　　首先，通过普通的匿名透明代理的方法，是直接使用Socket发送GET命令，只不过与GET普通网站稍有不同罢了 直接访问： GET / HTTP/1.1 Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/msword, application/vnd.ms-excel, application/vnd.ms-powerpoint, */* Accept-Language: zh-cn UA-CPU: x86 Accept-Encoding: gzip, deflate If-Modified-Since: Thu, 06 Jul 2006 15:39:53 GMT If-None-Match: &#34;1172d9-381c-44ad2ec9&#34; User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727) Host: www.redhat.com Connection: Keep-Alive Cookie: s_vi=[CS]v1&#124;44AAA05400004577-A170C060000008A[CE]; Apache=61.147.159.196.23241152032846747 [...]]]></description>
			<content:encoded><![CDATA[<p>　　我们学校的网络环境是，所有的出校访问均须通过代理服务器（我们叫作sproxy），并且不能连接国外网站。为此，我想要做一个自动抓取和验证出国代理列表的WebService。我们所有的验证代理的请求，都需要通过两层代理，最终通到用来测试的网站（我使用了www.redhat.com）</p>
<p>　　技术上的思路是，通过连接第一层代理sproxy（支持http tunnel），给第二层代理发送GET指令，从而完成对目标网页的访问。</p>
<p>　　首先，通过普通的匿名透明代理的方法，是直接使用Socket发送GET命令，只不过与GET普通网站稍有不同罢了</p>
<p>直接访问：</p>

<div class="wp_syntax"><div class="code"><pre class="apache" style="font-family:monospace;">GET / HTTP/<span style="color: #ff0000;">1.1</span>
Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/msword, application/vnd.ms-excel, application/vnd.ms-powerpoint, */*
Accept-Language: zh-cn
UA-CPU: x86
Accept-Encoding: gzip, deflate
If-Modified-Since: Thu, 06 Jul <span style="color: #ff0000;">2006</span> <span style="color: #ff0000;">15</span>:<span style="color: #ff0000;">39</span>:<span style="color: #ff0000;">53</span> GMT
If-None-Match: <span style="color: #7f007f;">&quot;1172d9-381c-44ad2ec9&quot;</span>
User-Agent: Mozilla/<span style="color: #ff0000;">4.0</span> (compatible; MSIE <span style="color: #ff0000;">6.0</span>; Windows NT <span style="color: #ff0000;">5.2</span>; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)
Host: www.redhat.com
Connection: Keep-Alive
Cookie: s_vi=[CS]v1|44AAA05400004577-A170C060000008A[CE]; Apache=61.147.159.196.23241152032846747</pre></div></div>

<p>通过本机CCproxy:</p>

<div class="wp_syntax"><div class="code"><pre class="apache" style="font-family:monospace;">GET / HTTP/<span style="color: #ff0000;">1.0</span>
Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/msword, application/vnd.ms-excel, application/vnd.ms-powerpoint, */*
Accept-Language: zh-cn
UA-CPU: x86
Proxy-Connection: Keep-Alive
User-Agent: Mozilla/<span style="color: #ff0000;">4.0</span> (compatible; MSIE <span style="color: #ff0000;">6.0</span>; Windows NT <span style="color: #ff0000;">5.2</span>; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)
Host: www.redhat.com
Cookie: s_vi=[CS]v1|44AAA05400004577-A170C060000008A[CE]; Apache=61.147.159.196.23241152032846747</pre></div></div>

<p>　　可以看出来，只要给代理服务器发送正确的请求地址即可，不需要程序上特殊的变化。<br />
　　事实上，对于不使用代理服务器的场合，你可以直接向某HTTP服务器发送GET /，而这在使用代理时不行，需要说明详细地址或者标明Host: www.redhat.com</p>
<p>对于两层代理，就有所不同了<br />
<span id="more-31"></span><br />
　　首先要给sproxy发送一个CONNECT指令，令其去连接某公众网代理（这里称为proxy2），然后通过这个Socket连接发送GET指令给proxy2。这里对sproxy（以后称proxy1）有一个要求，即必须支持CONNECT，也就是说必须是一个HTTP Tunnel，或者说支持HTTPS。<br />
　　由于一开始设置上的问题，我本以为返回HTTP 1.0的CCProxy不支持这样的方式，因为据说HTTP 1.0就不能支持HTTPS，连接一次，发送了数据一定会断开（一开始的测试中的确如此）。后来查阅资料才知道，HTTP 1.0只是默认不使用Connect: Keep-Alive的参数，事实上也是支持的，只要置上这个参数即可。<br />
　　但实际使用中，发现不加上这个参数也是可以的（我对proxy1没有使用这个参数，可能是.net下的Socket类自动加上了）。</p>
<p>　　经过连续数天的测试，一直不能通过，问题集中在，给proxy2发送一次CONNECT命令，Socket就会断开，无法收到数据。这是典型的不支持HTTP Tunnel的表现，但是我一直怀疑是CCproxy和学校代理服务器的问题，没有发现根源所在。<br />
　　因为我们访问sproxy需要用户名密码验证，我不得已才使用了CCProxy。后来，我又安装了一个代理服务器软件，发现其中只要使用“代理嵌套”（CCproxy叫作二级代理）的时候都需要HTTPS，我突然想到，会不会需要在CCproxy中设置？<br />
　　于是，在CCproxy的设置-高级-二级代理中把代理类型设为HTTPS，使用proxy1做上级代理，果然成功。事实上，直接连接学校代理应该也是可以的，只是我还不知道如何在代理请求中发送用户验证信息（需要将验证信息截断为多个数据包，不能直接发送）。</p>
<p>代码如下，调试通过并成功运行了十多天了：</p>
<p>后台一个类：</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">string</span> Get_Socket_Request_uip<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">string</span> ip<span style="color: #008000;">&#41;</span>
        <span style="color: #008000;">&#123;</span>
&nbsp;
            Encoding ASCII <span style="color: #008000;">=</span> Encoding<span style="color: #008000;">.</span><span style="color: #0600FF; font-weight: bold;">Default</span><span style="color: #008000;">;</span>
&nbsp;
            <span style="color: #008080; font-style: italic;">//给Proxy2发送命令</span>
            <span style="color: #6666cc; font-weight: bold;">string</span> Con <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;CONNECT &quot;</span> <span style="color: #008000;">+</span> ip <span style="color: #008000;">+</span> <span style="color: #666666;">&quot; HTTP/1.1
Connection: Keep-Alive
&nbsp;
&quot;</span><span style="color: #008000;">;</span>
            <span style="color: #6666cc; font-weight: bold;">string</span> Get <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;HEAD http://www.RedHat.com/ HTTP/1.1
Host: www.RedHat.com
Pragma: no-cache
Connection: Close
&nbsp;
&quot;</span><span style="color: #008000;">;</span>
            <span style="color: #6666cc; font-weight: bold;">Byte</span><span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span> ByteCon <span style="color: #008000;">=</span> ASCII<span style="color: #008000;">.</span><span style="color: #0000FF;">GetBytes</span><span style="color: #008000;">&#40;</span>Con<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
            <span style="color: #6666cc; font-weight: bold;">Byte</span><span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span> ByteGet <span style="color: #008000;">=</span> ASCII<span style="color: #008000;">.</span><span style="color: #0000FF;">GetBytes</span><span style="color: #008000;">&#40;</span>Get<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
            <span style="color: #6666cc; font-weight: bold;">Byte</span><span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span> RecvBytes <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> <span style="color: #6666cc; font-weight: bold;">Byte</span><span style="color: #008000;">&#91;</span><span style="color: #FF0000;">256</span><span style="color: #008000;">&#93;</span><span style="color: #008000;">;</span>
            <span style="color: #6666cc; font-weight: bold;">Byte</span><span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span> RecvBytes2 <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> <span style="color: #6666cc; font-weight: bold;">Byte</span><span style="color: #008000;">&#91;</span><span style="color: #FF0000;">256</span><span style="color: #008000;">&#93;</span><span style="color: #008000;">;</span> 
            <span style="color: #6666cc; font-weight: bold;">string</span> strRetPage <span style="color: #008000;">=</span> <span style="color: #0600FF; font-weight: bold;">null</span><span style="color: #008000;">;</span>
            <span style="color: #6666cc; font-weight: bold;">string</span> strRetCon <span style="color: #008000;">=</span> <span style="color: #0600FF; font-weight: bold;">null</span><span style="color: #008000;">;</span>
&nbsp;
            <span style="color: #008080; font-style: italic;">//建立Socket</span>
            Socket s <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> Socket<span style="color: #008000;">&#40;</span>AddressFamily<span style="color: #008000;">.</span><span style="color: #0000FF;">InterNetwork</span>, SocketType<span style="color: #008000;">.</span><span style="color: #0000FF;">Stream</span>, ProtocolType<span style="color: #008000;">.</span><span style="color: #0000FF;">Tcp</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
            <span style="color: #008080; font-style: italic;">//连接本地CCProxy</span>
            IPAddress hostip <span style="color: #008000;">=</span> IPAddress<span style="color: #008000;">.</span><span style="color: #0000FF;">Parse</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;127.0.0.1&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
            IPEndPoint ipend <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> IPEndPoint<span style="color: #008000;">&#40;</span>hostip, <span style="color: #FF0000;">808</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
            <span style="color: #008080; font-style: italic;">//设置超时，超过了就证明这个代理无效</span>
            s<span style="color: #008000;">.</span><span style="color: #0000FF;">SendTimeout</span> <span style="color: #008000;">=</span> <span style="color: #FF0000;">20000</span><span style="color: #008000;">;</span>
            s<span style="color: #008000;">.</span><span style="color: #0000FF;">ReceiveTimeout</span> <span style="color: #008000;">=</span> <span style="color: #FF0000;">20000</span><span style="color: #008000;">;</span>
&nbsp;
            <span style="color: #008080; font-style: italic;">//这句话就是socket的Keep-Alive，我没有使用</span>
            <span style="color: #008080; font-style: italic;">//s.SetSocketOption(SocketOptionLevel.Tcp, SocketOptionName.KeepAlive, true);</span>
            s<span style="color: #008000;">.</span><span style="color: #0000FF;">Connect</span><span style="color: #008000;">&#40;</span>ipend<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
            <span style="color: #0600FF; font-weight: bold;">try</span>
            <span style="color: #008000;">&#123;</span>
                <span style="color: #008080; font-style: italic;">//发送CONNECT命令</span>
                s<span style="color: #008000;">.</span><span style="color: #0000FF;">Send</span><span style="color: #008000;">&#40;</span>ByteCon, ByteCon<span style="color: #008000;">.</span><span style="color: #0000FF;">Length</span>, <span style="color: #FF0000;">0</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
                Int32 bytes <span style="color: #008000;">=</span> s<span style="color: #008000;">.</span><span style="color: #0000FF;">Receive</span><span style="color: #008000;">&#40;</span>RecvBytes, RecvBytes<span style="color: #008000;">.</span><span style="color: #0000FF;">Length</span>, <span style="color: #FF0000;">0</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
                strRetCon <span style="color: #008000;">=</span> strRetCon <span style="color: #008000;">+</span> ASCII<span style="color: #008000;">.</span><span style="color: #0000FF;">GetString</span><span style="color: #008000;">&#40;</span>RecvBytes, <span style="color: #FF0000;">0</span>, bytes<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
                <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>strRetCon<span style="color: #008000;">.</span><span style="color: #0000FF;">Contains</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;200&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span>
                <span style="color: #008000;">&#123;</span>
                    s<span style="color: #008000;">.</span><span style="color: #0000FF;">Send</span><span style="color: #008000;">&#40;</span>ByteGet, ByteGet<span style="color: #008000;">.</span><span style="color: #0000FF;">Length</span>, <span style="color: #FF0000;">0</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
                    Int32 bytes2 <span style="color: #008000;">=</span> s<span style="color: #008000;">.</span><span style="color: #0000FF;">Receive</span><span style="color: #008000;">&#40;</span>RecvBytes2, RecvBytes2<span style="color: #008000;">.</span><span style="color: #0000FF;">Length</span>, <span style="color: #FF0000;">0</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
                    strRetPage <span style="color: #008000;">=</span> strRetPage <span style="color: #008000;">+</span> ASCII<span style="color: #008000;">.</span><span style="color: #0000FF;">GetString</span><span style="color: #008000;">&#40;</span>RecvBytes2, <span style="color: #FF0000;">0</span>, bytes2<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
                    <span style="color: #008080; font-style: italic;">//本来这里会循环接收数据（本来发送的不是HEAD而是GET，取整个页面），后来发现有时候这会导致断线，就去掉了，只用HEAD命令取返回的HTTP Head</span>
                    <span style="color: #008080; font-style: italic;">/*while (bytes2 &gt; 0)
                    {
                        bytes2 = s.Receive(RecvBytes2, RecvBytes2.Length, 0);
                        strRetPage = strRetPage + ASCII.GetString(RecvBytes2, 0, bytes2);
                    }*/</span>
                <span style="color: #008000;">&#125;</span>
            <span style="color: #008000;">&#125;</span>
            <span style="color: #0600FF; font-weight: bold;">catch</span>
            <span style="color: #008000;">&#123;</span>
                <span style="color: #0600FF; font-weight: bold;">break</span><span style="color: #008000;">;</span>
            <span style="color: #008000;">&#125;</span>
            <span style="color: #0600FF; font-weight: bold;">finally</span> 
            <span style="color: #008000;">&#123;</span>
               s<span style="color: #008000;">.</span><span style="color: #0000FF;">Shutdown</span><span style="color: #008000;">&#40;</span>SocketShutdown<span style="color: #008000;">.</span><span style="color: #0000FF;">Both</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
               s<span style="color: #008000;">.</span><span style="color: #0000FF;">Close</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
            <span style="color: #008000;">&#125;</span>
            <span style="color: #0600FF; font-weight: bold;">return</span> strRetPage<span style="color: #008000;">;</span>
        <span style="color: #008000;">&#125;</span></pre></div></div>

<p>前台的调用方法（已经使用证则表达式在别处的网页中抓取到待检测的代理列表，在matches数组中）：</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">void</span> TestProxy<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
        <span style="color: #008000;">&#123;</span>
            <span style="color: #6666cc; font-weight: bold;">int</span> r <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">;</span>
&nbsp;
            <span style="color: #0600FF; font-weight: bold;">try</span>
            <span style="color: #008000;">&#123;</span>
                <span style="color: #0600FF; font-weight: bold;">foreach</span> <span style="color: #008000;">&#40;</span>Match i <span style="color: #0600FF; font-weight: bold;">in</span> matches<span style="color: #008000;">&#41;</span>
                <span style="color: #008000;">&#123;</span>
                    <span style="color: #6666cc; font-weight: bold;">string</span> ip <span style="color: #008000;">=</span> i<span style="color: #008000;">.</span><span style="color: #0000FF;">Result</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;${ip}&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
                    <span style="color: #6666cc; font-weight: bold;">string</span> port <span style="color: #008000;">=</span> i<span style="color: #008000;">.</span><span style="color: #0000FF;">Result</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;${port}&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
                    <span style="color: #0600FF; font-weight: bold;">try</span>
                    <span style="color: #008000;">&#123;</span>
                        <span style="color: #008080; font-style: italic;">//这里的hc是后台那个类，myhttpclient</span>
                        <span style="color: #6666cc; font-weight: bold;">string</span> result <span style="color: #008000;">=</span> hc<span style="color: #008000;">.</span><span style="color: #0000FF;">Get_Socket_Request_uip</span><span style="color: #008000;">&#40;</span>ip <span style="color: #008000;">+</span> <span style="color: #666666;">&quot;:&quot;</span> <span style="color: #008000;">+</span> port<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
                        <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>result<span style="color: #008000;">.</span><span style="color: #0000FF;">Contains</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;200 OK&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span>
                        <span style="color: #008000;">&#123;</span>
                            checkedproxy<span style="color: #008000;">&#91;</span>r<span style="color: #008000;">&#93;</span> <span style="color: #008000;">=</span> ip <span style="color: #008000;">+</span> <span style="color: #666666;">&quot;:&quot;</span> <span style="color: #008000;">+</span> port<span style="color: #008000;">;</span>
                            r <span style="color: #008000;">=</span> r <span style="color: #008000;">+</span> <span style="color: #FF0000;">1</span><span style="color: #008000;">;</span>
                        <span style="color: #008000;">&#125;</span>
                    <span style="color: #008000;">&#125;</span>
                    <span style="color: #0600FF; font-weight: bold;">catch</span> <span style="color: #008000;">&#40;</span>TimeoutException<span style="color: #008000;">&#41;</span>
                    <span style="color: #008000;">&#123;</span>
                        <span style="color: #0600FF; font-weight: bold;">continue</span><span style="color: #008000;">;</span>
                    <span style="color: #008000;">&#125;</span>
                    <span style="color: #0600FF; font-weight: bold;">catch</span>
                    <span style="color: #008000;">&#123;</span>
                        <span style="color: #0600FF; font-weight: bold;">continue</span><span style="color: #008000;">;</span>
                    <span style="color: #008000;">&#125;</span>
                <span style="color: #008000;">&#125;</span>
            <span style="color: #008000;">&#125;</span>
            <span style="color: #0600FF; font-weight: bold;">catch</span>
            <span style="color: #008000;">&#123;</span>
                <span style="color: #0600FF; font-weight: bold;">break</span><span style="color: #008000;">;</span>
            <span style="color: #008000;">&#125;</span>
        <span style="color: #008000;">&#125;</span></pre></div></div>

<p>　　WebService代码就不给出了，就是调用这个方法而已。<br />
　　<br />
　　大家应该可以看出，这里实际上通过了三层代理（CCproxy，proxy1，proxy2），所以，如果要通过n层代理，方法也是一样的，只要一层层的CONNECT下去就行，但要求前n-1层代理都要支持HTTP tunnel。<br />
　　另外，在实际部署中使用这个程序，需要在Web.config中的system.web项下添加一行</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;httpruntime</span> <span style="color: #000066;">executionTimeout</span>=<span style="color: #ff0000;">&quot;600&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span></pre></div></div>

<p>　　否则超过10项的代理列表，几乎一定会超时的</p>
<p>Related posts:<ol>
<li><a href='http://blog.williamgates.biz/2011/09/do-not-edit-hosts-of-android-unless-necessary/' rel='bookmark' title='慎改Android的Hosts文件'>慎改Android的Hosts文件</a></li>
<li><a href='http://blog.williamgates.biz/2011/10/how-to-purchase-android-apps-use-i809-at-mainland-china/' rel='bookmark' title='在国行双网机（如i909/i809）上使用Android Market购买app研究小结'>在国行双网机（如i909/i809）上使用Android Market购买app研究小结</a></li>
<li><a href='http://blog.williamgates.biz/2011/11/do-not-and-do-not-edit-hosts-of-android/' rel='bookmark' title='再谈不要乱改Android的hosts文件'>再谈不要乱改Android的hosts文件</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://blog.williamgates.biz/2006/07/aspdotnet-through-multi-proxy/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>让WebService支持Post和Get方法</title>
		<link>http://blog.williamgates.biz/2006/05/webservice-support-get-post/</link>
		<comments>http://blog.williamgates.biz/2006/05/webservice-support-get-post/#comments</comments>
		<pubDate>Wed, 24 May 2006 01:39:36 +0000</pubDate>
		<dc:creator>WG</dc:creator>
				<category><![CDATA[WebService]]></category>
		<category><![CDATA[技术心得]]></category>
		<category><![CDATA[技术]]></category>

		<guid isPermaLink="false">http://blog.williamgates.biz/2006/05/24/22</guid>
		<description><![CDATA[　　在WebService的测试页面，你看到了什么？SOAP1.1/SOAP1.2/HTTP POST三种方法的测试页面，但是事实上，此时你用post方法是无法访问这个webservice的，更不用说get了。.net 2.0下的所有新建webservice默认关闭了这两种方法，是为了安全考虑。 　　但是我们有的时候不得不使用这两种方法，特别是get方法，几乎由一切软件和编程方法支持，并且可以穿越几乎所有的防火墙（除非连web访问都不让，那是中情局吧……）。那么如何让部署起来的webservice支持这种方法呢？ 　　在webservice的目录下添加Web.config文件（如果已经存在就修改之），最简单的情况，我们需要这样的文件： &#60; ?xml version=&#34;1.0&#34;?&#62; &#60;configuration xmlns=&#34;http://schemas.microsoft.com/.NetConfiguration/v2.0&#34;&#62; &#60;system .web&#62; &#60;compilation defaultLanguage=&#34;c#&#34; debug=&#34;true&#34;/&#62; &#60;webservices&#62; &#60;protocols&#62; &#60;add name=&#34;HttpGet&#34;/&#62; &#60;add name=&#34;HttpPost&#34;/&#62; &#60;/protocols&#62; &#60;/webservices&#62; &#60;/system&#62; &#60;/configuration&#62; 　　如果你已经有了VS生成的Web.config，那么只需要修改或添加这么一段 &#60;webservices&#62; &#60;protocols&#62; &#60;add name=&#34;HttpGet&#34;/&#62; &#60;add name=&#34;HttpPost&#34;/&#62; &#60;/protocols&#62; &#60;/webservices&#62; 　　这样就行了，你就可以在测试页面看到多出来一种HTTP GET的方法示例，并且也可以在程序中使用了 Related posts: 慎改Android的Hosts文件 在国行双网机（如i909/i809）上使用Android Market购买app研究小结 再谈不要乱改Android的hosts文件]]></description>
			<content:encoded><![CDATA[<p>　　在WebService的测试页面，你看到了什么？SOAP1.1/SOAP1.2/HTTP POST三种方法的测试页面，但是事实上，此时你用post方法是无法访问这个webservice的，更不用说get了。.net 2.0下的所有新建webservice默认关闭了这两种方法，是为了安全考虑。<br />
　　但是我们有的时候不得不使用这两种方法，特别是get方法，几乎由一切软件和编程方法支持，并且可以穿越几乎所有的防火墙（除非连web访问都不让，那是中情局吧……）。那么如何让部署起来的webservice支持这种方法呢？<br />
<span id="more-22"></span><br />
　　在webservice的目录下添加Web.config文件（如果已经存在就修改之），最简单的情况，我们需要这样的文件：</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;">&lt; ?xml <span style="color: #000066;">version</span>=<span style="color: #ff0000;">&quot;1.0&quot;</span><span style="color: #000000; font-weight: bold;">?&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;configuration</span> <span style="color: #000066;">xmlns</span>=<span style="color: #ff0000;">&quot;http://schemas.microsoft.com/.NetConfiguration/v2.0&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;system</span> .web<span style="color: #000000; font-weight: bold;">&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;compilation</span> <span style="color: #000066;">defaultLanguage</span>=<span style="color: #ff0000;">&quot;c#&quot;</span> <span style="color: #000066;">debug</span>=<span style="color: #ff0000;">&quot;true&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;webservices<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;protocols<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;add</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;HttpGet&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;add</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;HttpPost&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/protocols<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/webservices<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/system<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/configuration<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>　　如果你已经有了VS生成的Web.config，那么只需要修改或添加这么一段</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;webservices<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;protocols<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;add</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;HttpGet&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;add</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;HttpPost&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/protocols<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/webservices<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>　　这样就行了，你就可以在测试页面看到多出来一种HTTP GET的方法示例，并且也可以在程序中使用了</p>
<p>Related posts:<ol>
<li><a href='http://blog.williamgates.biz/2011/09/do-not-edit-hosts-of-android-unless-necessary/' rel='bookmark' title='慎改Android的Hosts文件'>慎改Android的Hosts文件</a></li>
<li><a href='http://blog.williamgates.biz/2011/10/how-to-purchase-android-apps-use-i809-at-mainland-china/' rel='bookmark' title='在国行双网机（如i909/i809）上使用Android Market购买app研究小结'>在国行双网机（如i909/i809）上使用Android Market购买app研究小结</a></li>
<li><a href='http://blog.williamgates.biz/2011/11/do-not-and-do-not-edit-hosts-of-android/' rel='bookmark' title='再谈不要乱改Android的hosts文件'>再谈不要乱改Android的hosts文件</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://blog.williamgates.biz/2006/05/webservice-support-get-post/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>WebService的缓存机制</title>
		<link>http://blog.williamgates.biz/2006/05/how-webservice-cache-works/</link>
		<comments>http://blog.williamgates.biz/2006/05/how-webservice-cache-works/#comments</comments>
		<pubDate>Tue, 23 May 2006 06:58:33 +0000</pubDate>
		<dc:creator>WG</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[WebService]]></category>
		<category><![CDATA[技术心得]]></category>
		<category><![CDATA[技术]]></category>

		<guid isPermaLink="false">http://blog.williamgates.biz/2006/05/23/21</guid>
		<description><![CDATA[　　WebService的缓存分为两种，一种是简单的输出缓存，一种是强大的数据缓存 　　一、输出缓存 　　输出缓存的使用非常简单，比较适用于WebService的参数比较少，结果比较单一的情况，例如股票信息，可以设置5-10秒的缓存，天气预报，则可以设置30分钟甚至数小时的缓存。使用方法是，在WebMethod属性上指定CacheDuration属性即可，例如 &#91;WebMethod&#40;Description = &#34;Test&#34;,CacheDuration=600&#41;&#93; public string Test&#40;&#41; &#123; return &#34;Test&#34;; &#125; 　　这样，600秒内这个WebService的所有输出数据都将从缓存中读取，不会真正做数据处理，如果事务代码是访问数据库的话，现在这种方法就会比每次都访问数据库快得多。这种缓存适合初接触WebService的新手使用。 　　要注意的是，不是所有服务都适合使用这种缓存，例如每次结果都不一样的，访问数极高的服务，缓存将会变得非常大，占用很多服务器的内存，却没有实际效果。 　　二、数据缓存 　　想将你的WebService某些运行数据保存起来？如果不使用本地的数据库或者文件，那么缓存是最好的选择。这种缓存不同于上面提到的输出缓存，它需要编写代码来实现，但是相对应的，它的功能非常强大，可以存放任何类型的信息，并且你可以在任何时候检索它。 　　虽然也可以使用Application来模拟缓存，但是这需要你自己管理内存释放、用户并发问题，在.net时代已经被抛弃，WebService下的缓存使用Cache这个集合 using System.Web.Caching; &#91;WebMethod&#40;Description = &#34;Test&#34;&#41;&#93; public string Test&#40;&#41; &#123; string Content = &#34;just4test&#34;; &#160; //创建数据缓存 Context.Cache.Insert&#40;&#34;Test&#34;, Content, null, DateTime.MaxValue,TimeSpan.Zero, CacheItemPriority.NotRemovable, null&#41;; &#160; string result = Context.Cache&#91;&#34;Test&#34;&#93;.ToString&#40;&#41;; return result; &#125; &#125; 　　在这里，我们使用了Context.Cache属性，Context.Cache.Insert方法用于将数据加入缓存。这个方法一共有4种重载，在这个例子中，我们使用的是功能最全面的重载版本，我们以此为例：每一个参数分别是键名（使用方法类似于Session），值，依赖性，绝对过期时间，可变过期时间，缓存优先级，缓存项目删除时的委托方法绝对过期时间是固定的，DataTime.MaxValue在这里表示永不过期；可变过期时间是一定时间内该缓存没有使用则自动失效，此处TimeSpan.Zero表示不使用可变过期。注意两者只能设置一项，如果要使用可变过期，绝对过期必须是DataTime.MaxValue，例如 Context.Cache.Insert&#40;&#34;Test&#34;, Content, null, DateTime.MaxValue, TimeSpan.FromMinutes&#40;10&#41;&#41;; 　　缓存优先级是Web服务器清理它的可能性，在此的CacheItemPriority.NotRemovable表示通常不从缓存中删除，可以理解为永久性缓存 [...]]]></description>
			<content:encoded><![CDATA[<p>　　WebService的缓存分为两种，一种是简单的输出缓存，一种是强大的数据缓存</p>
<p>　　一、输出缓存<br />
　　输出缓存的使用非常简单，比较适用于WebService的参数比较少，结果比较单一的情况，例如股票信息，可以设置5-10秒的缓存，天气预报，则可以设置30分钟甚至数小时的缓存。使用方法是，在WebMethod属性上指定CacheDuration属性即可，例如</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #008000;">&#91;</span>WebMethod<span style="color: #008000;">&#40;</span>Description <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;Test&quot;</span>,CacheDuration<span style="color: #008000;">=</span><span style="color: #FF0000;">600</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#93;</span>
<span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">string</span> Test<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
      <span style="color: #0600FF; font-weight: bold;">return</span> <span style="color: #666666;">&quot;Test&quot;</span><span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>　　这样，600秒内这个WebService的所有输出数据都将从缓存中读取，不会真正做数据处理，如果事务代码是访问数据库的话，现在这种方法就会比每次都访问数据库快得多。这种缓存适合初接触WebService的新手使用。<br />
　　要注意的是，不是所有服务都适合使用这种缓存，例如每次结果都不一样的，访问数极高的服务，缓存将会变得非常大，占用很多服务器的内存，却没有实际效果。<br />
<span id="more-21"></span><br />
　　二、数据缓存<br />
　　想将你的WebService某些运行数据保存起来？如果不使用本地的数据库或者文件，那么缓存是最好的选择。这种缓存不同于上面提到的输出缓存，它需要编写代码来实现，但是相对应的，它的功能非常强大，可以存放任何类型的信息，并且你可以在任何时候检索它。<br />
　　虽然也可以使用Application来模拟缓存，但是这需要你自己管理内存释放、用户并发问题，在.net时代已经被抛弃，WebService下的缓存使用Cache这个集合</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008080;">System.Web.Caching</span><span style="color: #008000;">;</span>
<span style="color: #008000;">&#91;</span>WebMethod<span style="color: #008000;">&#40;</span>Description <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;Test&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#93;</span>
<span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">string</span> Test<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
        <span style="color: #6666cc; font-weight: bold;">string</span> Content <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;just4test&quot;</span><span style="color: #008000;">;</span>
&nbsp;
        <span style="color: #008080; font-style: italic;">//创建数据缓存</span>
        Context<span style="color: #008000;">.</span><span style="color: #0000FF;">Cache</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Insert</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;Test&quot;</span>, Content, <span style="color: #0600FF; font-weight: bold;">null</span>, DateTime<span style="color: #008000;">.</span><span style="color: #0000FF;">MaxValue</span>,TimeSpan<span style="color: #008000;">.</span><span style="color: #0000FF;">Zero</span>, CacheItemPriority<span style="color: #008000;">.</span><span style="color: #0000FF;">NotRemovable</span>, <span style="color: #0600FF; font-weight: bold;">null</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
        <span style="color: #6666cc; font-weight: bold;">string</span> result <span style="color: #008000;">=</span> Context<span style="color: #008000;">.</span><span style="color: #0000FF;">Cache</span><span style="color: #008000;">&#91;</span><span style="color: #666666;">&quot;Test&quot;</span><span style="color: #008000;">&#93;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">ToString</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #0600FF; font-weight: bold;">return</span> result<span style="color: #008000;">;</span>
        <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>　　在这里，我们使用了Context.Cache属性，Context.Cache.Insert方法用于将数据加入缓存。这个方法一共有4种重载，在这个例子中，我们使用的是功能最全面的重载版本，我们以此为例：每一个参数分别是键名（使用方法类似于Session），值，依赖性，绝对过期时间，可变过期时间，缓存优先级，缓存项目删除时的委托方法绝对过期时间是固定的，DataTime.MaxValue在这里表示永不过期；可变过期时间是一定时间内该缓存没有使用则自动失效，此处TimeSpan.Zero表示不使用可变过期。注意两者只能设置一项，如果要使用可变过期，绝对过期必须是DataTime.MaxValue，例如</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">Context<span style="color: #008000;">.</span><span style="color: #0000FF;">Cache</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Insert</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;Test&quot;</span>, Content, <span style="color: #0600FF; font-weight: bold;">null</span>, DateTime<span style="color: #008000;">.</span><span style="color: #0000FF;">MaxValue</span>, TimeSpan<span style="color: #008000;">.</span><span style="color: #0000FF;">FromMinutes</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">10</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span></pre></div></div>

<p>　　缓存优先级是Web服务器清理它的可能性，在此的CacheItemPriority.NotRemovable表示通常不从缓存中删除，可以理解为永久性缓存<br />
　　通过依赖性，可以监视某个文件或者其他缓存的改动，如果有变化，则此缓存失效，这非常有实用价值。例如：</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">CacheDependency de <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> CacheDependency<span style="color: #008000;">&#40;</span>Server<span style="color: #008000;">.</span><span style="color: #0000FF;">MapPath</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;1.xml&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
Context<span style="color: #008000;">.</span><span style="color: #0000FF;">Cache</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Insert</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;Test&quot;</span>, Content, de, DateTime<span style="color: #008000;">.</span><span style="color: #0000FF;">MaxValue</span>, TimeSpan<span style="color: #008000;">.</span><span style="color: #0000FF;">Zero</span>, CacheItemPriority<span style="color: #008000;">.</span><span style="color: #0000FF;">NotRemovable</span>, <span style="color: #0600FF; font-weight: bold;">null</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span></pre></div></div>

<p>　　这样，1.xml文件被删除或者更改的时候，缓存就会失效 </p>
<p>　　三、实用举例<br />
　　实际使用中，我使用这样一段代码，当远程的某个文件更新时，必须下载到本地，而缓存负责保存该文件的文件名和修改时间，每次客户端请求文件名和时间的时候，直接从缓存读取。每次定时下载程序（另有代码）启动的时候，getFiles()方法先检查是否有新文件(与本地缓存比对），然后决定是否下载。</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008080;">System</span><span style="color: #008000;">;</span>
<span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008080;">System.Web</span><span style="color: #008000;">;</span>
<span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008080;">System.Web.Services</span><span style="color: #008000;">;</span>
<span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008080;">System.Web.Services.Protocols</span><span style="color: #008000;">;</span>
<span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008080;">System.Xml</span><span style="color: #008000;">;</span>
<span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008080;">System.Xml.Serialization</span><span style="color: #008000;">;</span>
<span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008080;">TestOp</span><span style="color: #008000;">;</span>
<span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008080;">System.Web.Caching</span><span style="color: #008000;">;</span>
<span style="color: #008000;">&#91;</span>WebService<span style="color: #008000;">&#40;</span><span style="color: #0600FF; font-weight: bold;">Namespace</span> <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;Test&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#93;</span>
<span style="color: #008000;">&#91;</span>WebServiceBinding<span style="color: #008000;">&#40;</span>ConformsTo <span style="color: #008000;">=</span> WsiProfiles<span style="color: #008000;">.</span><span style="color: #0000FF;">BasicProfile1_1</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#93;</span>
<span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">class</span> Test<span style="color: #008000;">:</span> <span style="color: #000000;">System.<span style="color: #0000FF;">Web</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Services</span></span><span style="color: #008000;">.</span><span style="color: #0000FF;">WebService</span>
<span style="color: #008000;">&#123;</span>
 <span style="color: #008080; font-style: italic;">//提供下载和分析功能的后台代码类，TestOP</span>
 <span style="color: #0600FF; font-weight: bold;">private</span> TestOp testOp<span style="color: #008000;">;</span>
&nbsp;
 <span style="color: #0600FF; font-weight: bold;">public</span> Test<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
 <span style="color: #008000;">&#123;</span>
 testOp <span style="color: #008000;">=</span> newTestOp<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
 <span style="color: #008000;">&#125;</span>
&nbsp;
 <span style="color: #008000;">&#91;</span>WebMethod<span style="color: #008000;">&#40;</span>Description <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;下载文件&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#93;</span>
 <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">string</span> getFiles<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
 <span style="color: #008000;">&#123;</span>
  <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>Context<span style="color: #008000;">.</span><span style="color: #0000FF;">Cache</span><span style="color: #008000;">&#91;</span><span style="color: #666666;">&quot;FileName&quot;</span><span style="color: #008000;">&#93;</span> <span style="color: #008000;">!=</span> <span style="color: #0600FF; font-weight: bold;">null</span><span style="color: #008000;">&#41;</span>
  <span style="color: #008000;">&#123;</span>
   <span style="color: #6666cc; font-weight: bold;">string</span> FN<span style="color: #008000;">=</span>Context<span style="color: #008000;">.</span><span style="color: #0000FF;">Cache</span><span style="color: #008000;">&#91;</span><span style="color: #666666;">&quot;FileName&quot;</span><span style="color: #008000;">&#93;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">ToString</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
   testOp<span style="color: #008000;">.</span><span style="color: #0000FF;">GetHTML</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
   testOp<span style="color: #008000;">.</span><span style="color: #0000FF;">GetMatch</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
   <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>FN<span style="color: #008000;">.</span><span style="color: #0000FF;">CompareTo</span><span style="color: #008000;">&#40;</span>testOp<span style="color: #008000;">.</span><span style="color: #0000FF;">CheckFileName</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">!=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">&#41;</span>
   <span style="color: #008000;">&#123;</span>
    <span style="color: #0600FF; font-weight: bold;">try</span>
    <span style="color: #008000;">&#123;</span>
     testOp<span style="color: #008000;">.</span><span style="color: #0000FF;">Download</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
     Context<span style="color: #008000;">.</span><span style="color: #0000FF;">Cache</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Insert</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;Time&quot;</span>, testOp<span style="color: #008000;">.</span><span style="color: #0000FF;">CheckTime</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>, <span style="color: #0600FF; font-weight: bold;">null</span>, DateTime<span style="color: #008000;">.</span><span style="color: #0000FF;">MaxValue</span>, TimeSpan<span style="color: #008000;">.</span><span style="color: #0000FF;">Zero</span>, CacheItemPriority<span style="color: #008000;">.</span><span style="color: #0000FF;">NotRemovable</span>, <span style="color: #0600FF; font-weight: bold;">null</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
     Context<span style="color: #008000;">.</span><span style="color: #0000FF;">Cache</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Insert</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;FileName&quot;</span>, testOp<span style="color: #008000;">.</span><span style="color: #0000FF;">CheckFileName</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>, <span style="color: #0600FF; font-weight: bold;">null</span>, DateTime<span style="color: #008000;">.</span><span style="color: #0000FF;">MaxValue</span>, TimeSpan<span style="color: #008000;">.</span><span style="color: #0000FF;">Zero</span>, CacheItemPriority<span style="color: #008000;">.</span><span style="color: #0000FF;">NotRemovable</span>, <span style="color: #0600FF; font-weight: bold;">null</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
 <span style="color: #0600FF; font-weight: bold;">return</span> <span style="color: #666666;">&quot;ok&quot;</span><span style="color: #008000;">;</span>
 <span style="color: #008000;">&#125;</span>
 <span style="color: #0600FF; font-weight: bold;">catch</span>
 <span style="color: #008000;">&#123;</span>
  <span style="color: #0600FF; font-weight: bold;">return</span> <span style="color: #666666;">&quot;Ex&quot;</span><span style="color: #008000;">;</span>
 <span style="color: #008000;">&#125;</span>
 <span style="color: #008000;">&#125;</span>
 <span style="color: #0600FF; font-weight: bold;">else</span>
 <span style="color: #008000;">&#123;</span>
  <span style="color: #0600FF; font-weight: bold;">return</span> <span style="color: #666666;">&quot;ok&quot;</span><span style="color: #008000;">;</span>
 <span style="color: #008000;">&#125;</span>
 <span style="color: #008000;">&#125;</span>
 <span style="color: #0600FF; font-weight: bold;">else</span>
 <span style="color: #008000;">&#123;</span>
  <span style="color: #0600FF; font-weight: bold;">try</span>
  <span style="color: #008000;">&#123;</span>
   testOp<span style="color: #008000;">.</span><span style="color: #0000FF;">GetHTML</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
   testOp<span style="color: #008000;">.</span><span style="color: #0000FF;">GetMatch</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
   testOp<span style="color: #008000;">.</span><span style="color: #0000FF;">Download</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
   Context<span style="color: #008000;">.</span><span style="color: #0000FF;">Cache</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Insert</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;Time&quot;</span>, testOp<span style="color: #008000;">.</span><span style="color: #0000FF;">CheckTime</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>, <span style="color: #0600FF; font-weight: bold;">null</span>, DateTime<span style="color: #008000;">.</span><span style="color: #0000FF;">MaxValue</span>, TimeSpan<span style="color: #008000;">.</span><span style="color: #0000FF;">Zero</span>, CacheItemPriority<span style="color: #008000;">.</span><span style="color: #0000FF;">NotRemovable</span>, <span style="color: #0600FF; font-weight: bold;">null</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
   Context<span style="color: #008000;">.</span><span style="color: #0000FF;">Cache</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Insert</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;FileName&quot;</span>, testOp<span style="color: #008000;">.</span><span style="color: #0000FF;">CheckFileName</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>, <span style="color: #0600FF; font-weight: bold;">null</span>, DateTime<span style="color: #008000;">.</span><span style="color: #0000FF;">MaxValue</span>, TimeSpan<span style="color: #008000;">.</span><span style="color: #0000FF;">Zero</span>, CacheItemPriority<span style="color: #008000;">.</span><span style="color: #0000FF;">NotRemovable</span>, <span style="color: #0600FF; font-weight: bold;">null</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
 <span style="color: #0600FF; font-weight: bold;">return</span> <span style="color: #666666;">&quot;ok&quot;</span><span style="color: #008000;">;</span>
 <span style="color: #008000;">&#125;</span>
 <span style="color: #0600FF; font-weight: bold;">catch</span>
 <span style="color: #008000;">&#123;</span>
  <span style="color: #0600FF; font-weight: bold;">return</span> <span style="color: #666666;">&quot;Ex&quot;</span><span style="color: #008000;">;</span>
 <span style="color: #008000;">&#125;</span>
 <span style="color: #008000;">&#125;</span>
&nbsp;
 <span style="color: #008000;">&#125;</span>
&nbsp;
 <span style="color: #008000;">&#91;</span>WebMethod<span style="color: #008000;">&#40;</span>Description <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;检查最新文件时间&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#93;</span>
 <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">string</span> CheckTime<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
 <span style="color: #008000;">&#123;</span>
  <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>Context<span style="color: #008000;">.</span><span style="color: #0000FF;">Cache</span><span style="color: #008000;">&#91;</span><span style="color: #666666;">&quot;Time&quot;</span><span style="color: #008000;">&#93;</span> <span style="color: #008000;">==</span><span style="color: #0600FF; font-weight: bold;">null</span><span style="color: #008000;">&#41;</span>
  <span style="color: #008000;">&#123;</span>
   <span style="color: #0600FF; font-weight: bold;">try</span>
   <span style="color: #008000;">&#123;</span>
    <span style="color: #0600FF; font-weight: bold;">this</span><span style="color: #008000;">.</span><span style="color: #0000FF;">getFiles</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #6666cc; font-weight: bold;">string</span> result <span style="color: #008000;">=</span> Context<span style="color: #008000;">.</span><span style="color: #0000FF;">Cache</span><span style="color: #008000;">&#91;</span><span style="color: #666666;">&quot;Time&quot;</span><span style="color: #008000;">&#93;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">ToString</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
    DateTime<span style="color: #008000;">.</span><span style="color: #0000FF;">MaxValue</span>, TimeSpan<span style="color: #008000;">.</span><span style="color: #0000FF;">Zero</span>, CacheItemPriority<span style="color: #008000;">.</span><span style="color: #0000FF;">NotRemovable</span>, <span style="color: #0600FF; font-weight: bold;">null</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #0600FF; font-weight: bold;">return</span> result<span style="color: #008000;">;</span>
   <span style="color: #008000;">&#125;</span>
   <span style="color: #0600FF; font-weight: bold;">catch</span>
   <span style="color: #008000;">&#123;</span>
    <span style="color: #0600FF; font-weight: bold;">return</span> <span style="color: #666666;">&quot;Ex&quot;</span><span style="color: #008000;">;</span>
   <span style="color: #008000;">&#125;</span>
  <span style="color: #008000;">&#125;</span>
  <span style="color: #0600FF; font-weight: bold;">else</span>
  <span style="color: #008000;">&#123;</span>
   <span style="color: #6666cc; font-weight: bold;">string</span> result <span style="color: #008000;">=</span> Context<span style="color: #008000;">.</span><span style="color: #0000FF;">Cache</span><span style="color: #008000;">&#91;</span><span style="color: #666666;">&quot;Time&quot;</span><span style="color: #008000;">&#93;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">ToString</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
   <span style="color: #0600FF; font-weight: bold;">return</span> result<span style="color: #008000;">;</span>
  <span style="color: #008000;">&#125;</span>
&nbsp;
 <span style="color: #008000;">&#125;</span>
&nbsp;
 <span style="color: #008000;">&#91;</span>WebMethod<span style="color: #008000;">&#40;</span>Description <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;检查最新文件名&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#93;</span>
 <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">string</span> CheckFileName<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
 <span style="color: #008000;">&#123;</span>
  <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>Context<span style="color: #008000;">.</span><span style="color: #0000FF;">Cache</span><span style="color: #008000;">&#91;</span><span style="color: #666666;">&quot;FileName&quot;</span><span style="color: #008000;">&#93;</span> <span style="color: #008000;">==</span> <span style="color: #0600FF; font-weight: bold;">null</span><span style="color: #008000;">&#41;</span>
  <span style="color: #008000;">&#123;</span>
   <span style="color: #0600FF; font-weight: bold;">try</span>
   <span style="color: #008000;">&#123;</span>
    <span style="color: #0600FF; font-weight: bold;">this</span><span style="color: #008000;">.</span><span style="color: #0000FF;">getFiles</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
    <span style="color: #6666cc; font-weight: bold;">string</span> result <span style="color: #008000;">=</span> Context<span style="color: #008000;">.</span><span style="color: #0000FF;">Cache</span><span style="color: #008000;">&#91;</span><span style="color: #666666;">&quot;FileName&quot;</span><span style="color: #008000;">&#93;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">ToString</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #0600FF; font-weight: bold;">return</span> result<span style="color: #008000;">;</span>
   <span style="color: #008000;">&#125;</span>
   <span style="color: #0600FF; font-weight: bold;">catch</span>
   <span style="color: #008000;">&#123;</span>
    <span style="color: #0600FF; font-weight: bold;">return</span> <span style="color: #666666;">&quot;Ex&quot;</span><span style="color: #008000;">;</span>
   <span style="color: #008000;">&#125;</span>
  <span style="color: #008000;">&#125;</span>
  <span style="color: #0600FF; font-weight: bold;">else</span>
  <span style="color: #008000;">&#123;</span>
   <span style="color: #6666cc; font-weight: bold;">string</span> result <span style="color: #008000;">=</span> Context<span style="color: #008000;">.</span><span style="color: #0000FF;">Cache</span><span style="color: #008000;">&#91;</span><span style="color: #666666;">&quot;FileName&quot;</span><span style="color: #008000;">&#93;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">ToString</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
   <span style="color: #0600FF; font-weight: bold;">return</span> result<span style="color: #008000;">;</span>
  <span style="color: #008000;">&#125;</span>
 <span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>Related posts:<ol>
<li><a href='http://blog.williamgates.biz/2011/10/how-to-purchase-android-apps-use-i809-at-mainland-china/' rel='bookmark' title='在国行双网机（如i909/i809）上使用Android Market购买app研究小结'>在国行双网机（如i909/i809）上使用Android Market购买app研究小结</a></li>
<li><a href='http://blog.williamgates.biz/2011/09/do-not-edit-hosts-of-android-unless-necessary/' rel='bookmark' title='慎改Android的Hosts文件'>慎改Android的Hosts文件</a></li>
<li><a href='http://blog.williamgates.biz/2011/11/do-not-and-do-not-edit-hosts-of-android/' rel='bookmark' title='再谈不要乱改Android的hosts文件'>再谈不要乱改Android的hosts文件</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://blog.williamgates.biz/2006/05/how-webservice-cache-works/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

