顯示具有 網路相關 標籤的文章。 顯示所有文章
顯示具有 網路相關 標籤的文章。 顯示所有文章

2013年10月4日 星期五

Google Map 離線地圖台灣地區完整下載


在实现一个WIFI无线连接设置中需要注意的问题

http://blog.csdn.net/ymyss4/article/details/7288741

首先,要实现一个简单的WIFI连接设置,我们需要掌握和WIFI功能相关的一些类,比如WIfiManager,WifiInfo,ScanResult,WifiConfiguration等,提供了WIFI连接相关的基本的API.

比如:
打开关闭网关:wifiManager.setWifiEnabled(true/false);
扫描周边网络:wifiManager.getScanResults();
连接指定网络:wifiManager.enableNetwork(networkId,true);
添加网络:wifiManager.addNetwork(wcg);
移除网络:wifiManager.removeNetwork(netId);
获取网卡状态:wifiManager.getWifiState()
……
2 扫描的网络将会被保存在一个List<ScanResult>中,同时WifiManager会为我们维护一个List<WifiConfiguration>,这个List中保存了我们已经连接过的配置好的网络连接.
当我们选择一个网络时,判断它是否存在于这个List中,存在即可直接连接,否则需要用户输入密码创建一个新的WifiConfiguration.
3 获得的ScanResult中将会保存有该无线连接的相关信息,包括SSID,BSSID,capabilities,level等属性,其中SSID号是该连接的一个标识符,比如我们经常看到的TP_LINKXXX.
capabilities中保存了相关加密信息,比如WEB和WPA等.level则表示信号度.
4 在获取连接状态时,即调用wifiManager.getWifiState()或者wifiInfo.getSupplicantState()时,通 常在用户已经授权成功后,我们获得的状态值就为COMPLETED,此时不管网络是否已经连接成功,我们都无法获得新的状态. 所以要判断WIFI网络是否已经真的连接成功需要用到如下方法判断:

 ConnectivityManager connManager = (ConnectivityManager) WifiConnection.this
             .getSystemService(CONNECTIVITY_SERVICE);
// 获取代表联网状态的NetWorkInfo对象
NetworkInfo networkInfo = connManager.getActiveNetworkInfo();

if(networkInfo != null && networkInfo.getType() == 1
&& wifiAdmin.getWifiInfo().getSSID()!=null)
{

//WIFI网络连接成功
}
5 获取本地IP地址的方法:
public String getLocalIpAddress() {    
       try {    
           for (Enumeration<NetworkInterface> en = NetworkInterface    
                   .getNetworkInterfaces(); en.hasMoreElements();) {    
               NetworkInterface intf = en.nextElement();    
               for (Enumeration<InetAddress> enumIpAddr = intf    
                       .getInetAddresses(); enumIpAddr.hasMoreElements();) {    
                   InetAddress inetAddress = enumIpAddr.nextElement();    
                   if (!inetAddress.isLoopbackAddress()) {    
                       return inetAddress.getHostAddress().toString();    
                   }    
               }    
           }    
       } catch (SocketException ex) {    
           Log.e("WifiPreference IpAddress", ex.toString());    
       }    
       return null;    
   }    

6 在创建一个新的WifiConfiguration时,切记SSID和preSharedKey必须添加双引号,否则必将会导致连接失败.正确写法如下:
//创建一个新的WifiConfiguration
WifiConfiguration wcg = new WifiConfiguration();
wcg.BSSID = mBSSID;
//SSID和preSharedKey必须添加双引号,否则将会导致连接失败
wcg.SSID = "\"" + mSSID + "\"";
wcg.hiddenSSID = false;
wcg.status = WifiConfiguration.Status.ENABLED;
wcg.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.OPEN);
wcg.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.SHARED);
wcg.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP);
wcg.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);
wcg.allowedProtocols.set(WifiConfiguration.Protocol.RSN);
wcg.allowedProtocols.set(WifiConfiguration.Protocol.WPA);
//如果加密模式为WEP
if(mSecurity.equals("WEP"))
{
   wcg.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
   wcg.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP40);
   wcg.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP104);
   wcg.wepKeys[0] ="\"" + editText.getText().toString() + "\""; //This is the WEP Password
   wcg.wepTxKeyIndex = 0;
}
//如果加密模式为WPA EPA
else if(mSecurity.equals("WPA EAP"))
{
wcg.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_EAP);
wcg.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
wcg.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
wcg.preSharedKey = "\"" + editText.getText().toString() + "\"";
}
//如果加密模式为WPA PSK
else if(mSecurity.equals("WPA PSK"))
{
wcg.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);
wcg.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
wcg.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
wcg.preSharedKey = "\"" + editText.getText().toString() + "\"";
}
//无加密
else
{
wcg.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
}




其他问题将会即使进行补充.

什么是WEP,WPA,WPA2,TKIP,AES,PSK,EAP?

http://www.paxonpilot.com/web/shownews.aspx?id=31

现在的无线路由器主要使用WiFi的标准,即WLan(Wireless Lan)无线局域网的通用称法。也属于802.11 a/b/g/i/n/x的标准。
IEEE 802.11 所制定的是技术性标准 ,Wi-Fi 联盟所制定的是商业化标准 , 而 Wi-Fi 所制定的商业化标准基本上也都符合 IEEE 所制定的技术性标准。
在设置无线路由器的加密配置时,主要会看到以下设置和名词:
SSID 无线网络名称
WEP Wired Equivalent Privacy加密技术,RC4静态密钥
WPA Wi-Fi Protected Access加密技术,用户认证,比如LEAP,PEAP
WPA2 Wi-Fi Protected Access 2加密技术,主要是AES
TKIP Temporal Key Integrity Protocol临时密钥集成协议
AES Advanced Encryption Standard高级加密协议标准
PSK Pre-Share Key或passphrase预设密钥,等同于PMK
EAP Extensible Authentication Protocol 扩展认证协议,一般是使用Radius拨号服务器来认证,即Enterprise模式,最常见的EAP方式包括:LEAP、EAP-TLS、EAP-TTLS、PEAP
ASCII American Standard Code for Information Interchange美国信息交换标准码,包括数字、字符
HEX 即16进制,包括0-9,A-F 16个字符

一般家庭建议如下设置(没有外来使用者):
1、SSID(随便起个名)
2、加密方式选择使用WPA2-PSK,加密协议可以使用AES,设一串大于12个字符的英文字母即可。
3、如果觉得还是不放心可以把SSID隐藏起来,同时加入MAC地址绑定(在安全设置中增加只能符合的MAC地址才能通过路由器。


Apache Common HttpClient使用之七种武器

http://laohuang.iteye.com/blog/55613

1.拳头之Get/Post
拳头是最基本的一重武器,也是最重要的,好比练武之人必须先扎稳马步。


java 代码
 
  1. HttpClient httpclient=new HttpClient();//创建一个客户端,类似打开一个浏览器  
  2. GetMethod getMethod=new GetMethod("http://www.blablabla.com");//创建一个get方法,类似在浏览器地址栏中输入一个地址  
  3. int statusCode=httpclient.executeMethod(getMethod);//回车——出拳!  
  4. System.out.println("response=" + getMethod.getResponseBodyAsString());//察看拳头命中情况,可以获得的东西还有很多,比如head, cookies等等  
  5. getMethod.releaseConnection();//释放,记得收拳哦  


2.孔雀翎之支持https

如何支持https?

java 代码
 
  1. static{  
  2.     Protocol easyhttps = new Protocol("https", new EasySSLProtocolSocketFactory(), 443);  
  3.     Protocol.registerProtocol("https", easyhttps);  
  4. }  

在执行具体的http method之前,暗中将https协议注册一把,如孔雀翎暗藏玄机,毙敌于无形。记住,官方的binary发行版本没有ssl的contribute包,方法一是下载源代码版本来打造你的孔雀翎。

3.多情环之cookies
常道人老多情,其实是记忆太多,所以情之所至,正如cookies甜心,无论你走到那,总把你牵挂:

java 代码
 
  1. HttpClient httpclient=new HttpClient();  
  2. httpclient.getParams().setCookiePolicy(CookiePolicy.RFC_2109);//RFC_2109是支持较普遍的一个,还有其他cookie协议  
  3. HttpState initialState = new HttpState();  
  4. Cookie cookie=new Cookie();  
  5. cookie.setDomain("www.balblabla.com");  
  6. cookie.setPath("/");  
  7. cookie.setName("多情环");  
  8. cookie.setValue("多情即无情");  
  9. initialState.addCookie(cookie);  
  10. httpclient.setState(initialState);  
  11. ...  


4.离别钩之解构cookies

多情环的反面即离别钩,钩出,敌之身体某个部件即要与身体别离,端的是无情:

java 代码
 
  1. ...//执行了某些get/post方法后  
  2. Cookie[] cookies = httpclient.getState().getCookies();  
  3. System.out.println("Present cookies: ");  
  4. for (int i = 0; i < cookies.length; i++) {//循环结构零部件  
  5.     System.out.println(" - " + cookies[i].toExternalForm());  
  6.     System.out.println(" - domain=" + cookies[i].getDomain());  
  7.     System.out.println(" - path=" + cookies[i].getPath());  
  8.     ...            
  9. }  


5.霸王抢之post参数
枪,长兵器之王,诸多名将均使一杆或金或银命名的名枪,比如岳飞。post方法在提交参数对时,犹如灵蛇出洞:

java 代码
 
  1. PostMethod postMethod = new PostMethod("http://www.saybot.com/postme");  
  2. NameValuePair[] postData = new NameValuePair[2];  
  3. postData[0] = new NameValuePair("武器", "枪");  
  4. postData[1] = new NameValuePair("什么枪", "神枪");  
  5. postMethod.addParameters(postData);  
  6. ...//出枪吧  


6.七星碧玉刀之支持代理(proxy)

代理,非常重要,尤其在局域网横行的年头,没有代理,你在公司上不了QQ,没有代理,google不了网页快照,代理之威,可比七星碧玉刀,无刀,在局域网和开发当中,一切白搭:

java 代码
 
  1. HttpClient httpclient=new HttpClient();  
  2. httpClient.getHostConfiguration().setProxy("192.168.0.1", 9527);  
  3. httpClient.getParams().setAuthenticationPreemptive(true);//重要!!!告诉httpclient,使用抢先认证,否则你会收到“你没有资格”的恶果  
  4. /* 
  5.   这一步也至关重要,MyProxyCredentialsProvider实现了org.apache.commons.httpclient.auth.CredentialsProvider接口, 
  6.   返回代理的credential(username/password)*/  
  7. httpClient.getParams().setParameter(CredentialsProvider.PROVIDER, new MyProxyCredentialsProvider());  
  8. httpClient.getState().setProxyCredentials(  
  9.     new AuthScope("192.168.0.1",  
  10.         AuthScope.ANY_PORT, //任意端口哦,可要小心  
  11.         AuthScope.ANY_REALM),//任意域哦,可要小心  
  12. new UsernamePasswordCredentials("username",//proxy的用户名  
  13.                         "password"));//proxy的密码  

...

7.长生剑之天下第一
看httpclient的官方文档:jakarta.apache.org/commons/httpclient/userguide.html
看Hilton网友写的小葵花宝典笔记:www.cnjm.net/tech/article1155.html
通读后,你会有种拔剑四顾心茫然的高手感觉。

七种武器,打完收工!   

httpClient 带验证码登录实现

http://lzj0470.iteye.com/blog/1401444

在实现之前,需要注意一个问题。关于Cookie,如果能很好跟踪上到Cookie,基本上,实现都是小事一碟。
在网络上,有提到过验证码老是提示有错。貌似思路流程都是正确的。兄弟,如果你在半个小时还没搞清楚,先休息一下,听听歌,打打游戏。再想过。
推断为什么会出现这种问题。问题是你登录验证码页面(jsp,aspx),实际上就是一个会话或者Cookie记录。此时,你有去连接登录页面,有可能已经重新生成一个新的验证码啦。你用了之前的验证码,指定会提示验证码错误。
在这里,我只能说一下我遇到的这种问题的解决办法。如果还有其他情况,可以私下M我,我们继续过。
一、抓取一个农业网站思路步骤如下:
1、首先需要获取验证码页面,把它以图片的格式保证下来。(图片格式需要通过抓包的工具查看。如:httpWatch)并跟踪到的cookie保存下来。cookie必须要跟抓包工具看到的cookie一致。
2、提交登录页面所需要的字段和验证码,字段尽可能全部提交上去。
二、代码实现。

Java代码  
  1. import java.io.BufferedReader;  
  2. import java.io.File;  
  3. import java.io.FileNotFoundException;  
  4. import java.io.FileOutputStream;  
  5. import java.io.IOException;  
  6. import java.io.InputStreamReader;  
  7.   
  8. import org.apache.commons.httpclient.Cookie;  
  9. import org.apache.commons.httpclient.Header;  
  10. import org.apache.commons.httpclient.HttpClient;  
  11. import org.apache.commons.httpclient.HttpException;  
  12. import org.apache.commons.httpclient.NameValuePair;  
  13. import org.apache.commons.httpclient.cookie.CookiePolicy;  
  14. import org.apache.commons.httpclient.methods.GetMethod;  
  15. import org.apache.commons.httpclient.methods.PostMethod;  
  16. import org.apache.commons.httpclient.params.DefaultHttpParams;  
  17.   
  18. public class __10086 {  
  19.   
  20.     public void test() throws HttpException, IOException{  
  21.         String url = "http://12582.10086.cn/user/login/verifycode.aspx";  
  22.         HttpClient httpClient = new HttpClient();  
  23.         DefaultHttpParams.getDefaultParams().setParameter("http.protocol.cookie-policy", CookiePolicy.BROWSER_COMPATIBILITY);  
  24.         GetMethod getMethod = new GetMethod("http://12582.10086.cn/user/login/verifycode.aspx");    
  25.         getMethod.setRequestHeader("Host", "12582.10086.cn");  
  26.         getMethod.setRequestHeader("User-Agent", "Mozilla/5.0 (Windows NT 5.2; rv:5.0.1) Gecko/20100101 Firefox/5.0.1");  
  27.         getMethod.setRequestHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");  
  28.         getMethod.setRequestHeader("Accept-Language", "zh-cn,zh;q=0.5");  
  29.         getMethod.setRequestHeader("Accept-Charset", "GB2312,utf-8;q=0.7,*;q=0.7");  
  30.         getMethod.setRequestHeader("Connection", "keep-alive");  
  31. //      getMethod.setRequestHeader("Cookie", "Hm_lvt_6e06bb5a029d6c5473951d1079638828=1328777184942; Hm_lvt_e64244e1e591d0337e17a12b714c0996=1328777186856; WT_FPC=id=183.16.35.230-1530895312.30204303:lv=1328174810886:ss=1328174810886; ASP.NET_SessionId=gj25p555exiqjd45kdcqoq55; BIGipServernxt-wz=369797312.20480.0000; Hm_lpvt_6e06bb5a029d6c5473951d1079638828=1328777184942; Hm_lpvt_e64244e1e591d0337e17a12b714c0996=1328777186856; .12582portals=4CF97704261E34DBE12913CBA18211005E960282A771D94FF3709BAFC99610A7397BE8293ADB2E876A0A7380AC4A158016419745F27511D6E79F82A408D009519D6DDFE18A578E5CFC5F48025C75B33B6EBD43953A7DB05AEBAAB856E0AA29112818B4910350AADACD2012F5DE56297B1F6622ED7F4959D31E19C474E48F7773D7966437");  
  32.         getMethod.setRequestHeader("Cache-Control", "max-age=0");  
  33.         int code = httpClient.executeMethod(getMethod);  
  34.         Header header = getMethod.getResponseHeader("Set-cookie");   
  35.         System.out.println(header.getValue());  
  36.         String headerCookie = header.getValue();  
  37.         String SessionId = headerCookie.substring(headerCookie.indexOf("NET_SessionId=") + "NET_SessionId=".length(), headerCookie.indexOf(";"));  
  38.         System.out.println(SessionId);  
  39.         String BIGipServernxt = headerCookie.substring(headerCookie.indexOf("BIGipServernxt-wz=") + "BIGipServernxt-wz=".length(), headerCookie.length());  
  40.         BIGipServernxt = BIGipServernxt.substring(0, BIGipServernxt.indexOf(";"));  
  41.         StringBuffer sb = new StringBuffer();  
  42.         sb.append("Hm_lvt_6e06bb5a029d6c5473951d1079638828=1328778307106; ");  
  43.         sb.append("Hm_lvt_e64244e1e591d0337e17a12b714c0996=1328778308090; ");  
  44.         sb.append("WT_FPC=id=183.16.35.230-1530895312.30204303:lv=1328174810886:ss=1328174810886; ");  
  45.         sb.append("ASP.NET_SessionId=").append(SessionId).append("; ");  
  46.         sb.append("BIGipServernxt-wz=").append(BIGipServernxt).append("; ");  
  47.         sb.append("Hm_lpvt_6e06bb5a029d6c5473951d1079638828=1328778307106; ");  
  48.         sb.append("Hm_lpvt_e64244e1e591d0337e17a12b714c0996=1328778308090");  
  49.         System.out.println(sb.toString());  
  50.         String ValidCode = savegif(getMethod);  
  51.         getMethod.releaseConnection();  
  52.         // 第二次链接  
  53.         httpClient.getHostConfiguration().setHost("12582.10086.cn", 80, "http");  
  54.         PostMethod method = getPostMethod(ValidCode);  
  55.         method.setRequestHeader("User-Agent", "Mozilla/5.0 (Windows NT 5.2; rv:5.0.1) Gecko/20100101 Firefox/5.0.1");  
  56.         method.setRequestHeader("Host", "12582.10086.cn");  
  57.         method.setRequestHeader("Accept", "application/json, text/javascript, */*");  
  58.         method.setRequestHeader("Accept-Language", "zh-cn,zh;q=0.5");  
  59. //      method.setRequestHeader("Accept-Encoding", "gzip, deflate");  
  60.         method.setRequestHeader("Accept-Charset", "GB2312,utf-8;q=0.7,*;q=0.7");  
  61.         method.setRequestHeader("Connection", "keep-alive");  
  62.         method.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");  
  63.         method.setRequestHeader("X-Requested-With", "XMLHttpRequest");  
  64.         method.setRequestHeader("Referer", "http://12582.10086.cn/user/login/");  
  65.         method.setRequestHeader("Cookie", sb.toString());   
  66.         httpClient.executeMethod(method);  
  67.         System.out.println(method.getStatusCode());  
  68.         System.out.println(method.getResponseBodyAsString());  
  69.         // 第三次链接  
  70.         Header header1 = method.getResponseHeader("Set-cookie");   
  71.         System.out.println("dd=" + header1.getValue());  
  72.         sb.append("; " + header1.getValue());  
  73.         Cookie[] cookies = httpClient.getState().getCookies();  
  74.         method.releaseConnection();  
  75.         String my = "http://12582.10086.cn/my";  
  76.         System.out.println(sb.toString());  
  77.         getMethod = new GetMethod(my);  
  78.         getMethod.setRequestHeader("Host", "12582.10086.cn");  
  79.         getMethod.setRequestHeader("User-Agent", "Mozilla/5.0 (Windows NT 5.2; rv:5.0.1) Gecko/20100101 Firefox/5.0.1");  
  80.         getMethod.setRequestHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");  
  81.         getMethod.setRequestHeader("Accept-Language", "zh-cn,zh;q=0.5");  
  82.         getMethod.setRequestHeader("Accept-Charset", "GB2312,utf-8;q=0.7,*;q=0.7");  
  83.         getMethod.setRequestHeader("Connection", "keep-alive");  
  84.         getMethod.setRequestHeader("Referer", "http://12582.10086.cn/user/login");  
  85.         getMethod.setRequestHeader("Cookie", sb.toString());  
  86.         code = httpClient.executeMethod(getMethod);  
  87.         System.out.println(getMethod.getStatusCode());  
  88.         System.out.println(getMethod.getResponseBodyAsString());  
  89.         File storeFile = new File("c:/2008sohu.html");    
  90.         FileOutputStream output = new FileOutputStream(storeFile);    
  91.         //得到网络资源的字节数组,并写入文件    
  92.         output.write(getMethod.getResponseBody());    
  93.         output.close();   
  94.     }  
  95.       
  96.     public String savegif(GetMethod getMethod) throws IOException {  
  97.         File storeFile = new File("c:/2008sohu.gif");    
  98.         FileOutputStream output = new FileOutputStream(storeFile);    
  99.         //得到网络资源的字节数组,并写入文件    
  100.         output.write(getMethod.getResponseBody());    
  101.         output.close();    
  102.         InputStreamReader is = new InputStreamReader(System.in);   
  103.         BufferedReader br = new BufferedReader(is);  
  104.         String ValidCode = "";  
  105.         try {  
  106.             ValidCode = br.readLine();  
  107.             br.close();  
  108.             is.close();  
  109.         } catch(Exception e) {  
  110.             e.printStackTrace();  
  111.         }  
  112.         return ValidCode;  
  113.     }  
  114.       
  115.     private PostMethod getPostMethod(String ValidCode) {  
  116.         PostMethod post = new PostMethod("/ajax/postlogin");  
  117.         NameValuePair[] simcard = {  
  118.                 new NameValuePair("email", "XXX"),  
  119.                 new NameValuePair("password", "XXX"),  
  120.                 new NameValuePair("ValidCode", ValidCode),  
  121.                 new NameValuePair("rme", "0"),  
  122.                 };  
  123.         post.setRequestBody(simcard);  
  124.         return post;  
  125.     }  
  126.       
  127.     public static void main(String args[]) {  
  128.         __10086 _10086 = new __10086();  
  129.         try {  
  130.             _10086.test();  
  131.         } catch (HttpException e) {  
  132.             // TODO Auto-generated catch block  
  133.             e.printStackTrace();  
  134.         } catch (IOException e) {  
  135.             // TODO Auto-generated catch block  
  136.             e.printStackTrace();  
  137.         }  
  138.     }  
  139. }  

来源于:http://www.agrilink.cn/

采用apache 的httpclient工具进行http请求(保持session会话)

http://www.heiqu.com/show-71029-1.html

采用apache 的httpclient工具进行http请求(保持session会话)

23小时前

  /**

  * 采用apache httpclient工具,依赖jar包 common-codec common-log和 common-httpclient 都是apache的jar包

  * cookice 原理,默认情况下,服务器会把session写在cookie里,所以禁用cookie会导致session丢失,若果吧session记录下来,

  * 会保持session,当发现session过期时候,更新session,就能保证继续访问

  * @author 于堃

  *

  */

  import java.io.IOException;

  import java.util.HashMap;

  import java.util.Map;

  import org.apache.commons.httpclient.Cookie;

  import org.apache.commons.httpclient.DefaultHttpMethodRetryHandler;

  import org.apache.commons.httpclient.HttpClient;

  import org.apache.commons.httpclient.HttpException;

  import org.apache.commons.httpclient.HttpStatus;

  import org.apache.commons.httpclient.methods.GetMethod;

  import org.apache.commons.httpclient.params.HttpMethodParams;

  public class HttpUtil {

  private static String DEFAULT_ENCODE="UTF-8";

  private HttpClient httpClient = new HttpClient();

  private Map<String,Cookie[]>history=new HashMap<String,Cookie[]>();

  private Map<String,Cookie>sessions=new HashMap<String,Cookie>();

  public String doGet(String url) throws HttpException, IOException

  {

  return doGet(url,DEFAULT_ENCODE);

  }

  public synchronized String doGet(String url,String encode)throws HttpException,IOException

  {

  String host=url.replaceFirst("\\w+://","");//正则表达式

  int offset=host.indexOf("/");

  if(offset!=-1)

  {

  host=host.substring(0,offset);

  }

  Cookie session=sessions.get(host);

  GetMethod doGET =new GetMethod(url);

  doGET.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,

  new DefaultHttpMethodRetryHandler());

  if(session!=null)//保持会话

  {

  httpClient.getState().addCookie(session);

  }

  int statusCode = httpClient.executeMethod(doGET);

  if (statusCode !=HttpStatus.SC_OK)

  {

  return null;

  }

  byte[] response = doGET.getResponseBody();

  String response_str=new String(response,encode);

  Cookie[] cookies =httpClient.getState().getCookies();

  for(Cookie cookie:cookies)

  {

  String name=cookie.getName();

  if(name.endsWith("SESSIONID"))

  {

  if(session==null||!session.getValue().equals(cookie.getValue()))

  {

  sessions.put(host, cookie); //会话过期,续期

  }

  }

  }

  doGET.releaseConnection();

  return response_str;

  }

  }



原文地址:http://user.qzone.qq.com/253620527/blog/1303655583

Java实现FTP上传下载功能

http://lavasoft.blog.51cto.com/62575/93883

Java FTP客户端工具包很多,在此我选用的Apache的FTPClient。这个包的获取可以通过[url]http://commons.apache.org/net/[/url]来获取,我使用的是最新的commons-net-1.4.1.zip。其中包含了众多的java网络编程的工具包,官方文档列举如下:
    支持网络协议如下:
        FTP
        NNTP
        SMTP
        POP3
        Telnet
        TFTP
        Finger
        Whois
        rexec/rcmd/rlogin
        Time (rdate) and Daytime
        Echo
        Discard
        NTP/SNTP

都很有用。在此我用到的是FTP相关的一些包。
 
在编写程序之前,先配置一个简单的FTP服务器,如下图:
 

下面是一个使用FTPClient来实现的FTP上传和下载的功能,主要是对此包有个感性的认识。例程如下:
 
import org.apache.commons.io.IOUtils;
import org.apache.commons.net.ftp.FTPClient;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.FileOutputStream;

/**
* Apache commons-net 试用一把,看看FTP客户端工具做的好用不
*
* @author : leizhimin,2008-8-20 14:00:38。

*/
public class FtpTest {
    public static void main(String[] args) {
        testUpload();
        testDownload();
    }

    /**
     * FTP上传单个文件测试
     */
    public static void testUpload() {
        FTPClient ftpClient = new FTPClient();
        FileInputStream fis = null;

        try {
            ftpClient.connect("192.168.14.117");
            ftpClient.login("admin", "123");

            File srcFile = new File("C:\\new.gif");
            fis = new FileInputStream(srcFile);
            //设置上传目录
            ftpClient.changeWorkingDirectory("/admin/pic");
            ftpClient.setBufferSize(1024);
            ftpClient.setControlEncoding("GBK");
            //设置文件类型(二进制)
            ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
            ftpClient.storeFile("3.gif", fis);
        } catch (IOException e) {
            e.printStackTrace();
            throw new RuntimeException("FTP客户端出错!", e);
        } finally {
            IOUtils.closeQuietly(fis);
            try {
                ftpClient.disconnect();
            } catch (IOException e) {
                e.printStackTrace();
                throw new RuntimeException("关闭FTP连接发生异常!", e);
            }
        }
    }

    /**
     * FTP下载单个文件测试
     */
    public static void testDownload() {
        FTPClient ftpClient = new FTPClient();
        FileOutputStream fos = null;

        try {
            ftpClient.connect("192.168.14.117");
            ftpClient.login("admin", "123");

            String remoteFileName = "/admin/pic/3.gif";
            fos = new FileOutputStream("c:/down.gif");

            ftpClient.setBufferSize(1024);
            //设置文件类型(二进制)
            ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
            ftpClient.retrieveFile(remoteFileName, fos);
        } catch (IOException e) {
            e.printStackTrace();
            throw new RuntimeException("FTP客户端出错!", e);
        } finally {
            IOUtils.closeQuietly(fos);
            try {
                ftpClient.disconnect();
            } catch (IOException e) {
                e.printStackTrace();
                throw new RuntimeException("关闭FTP连接发生异常!", e);
            }
        }
    }
}

 
测试结果表明,文件上传、下载均成功!
 

感觉Apache做的不错,就用这个了。

使用Apache HttpClient突破J2EE站点认证

http://developer.51cto.com/art/201203/321336.htm

商业性 Web 站点大都提供站点认证功能以保护某些受限资源,HTTP 协议和 J2EE 规范对 Web 站点的认证过程都已有了详尽的定义,常见浏览器都能根据相应协议提供对应的界面形式帮助用户完成站点的认证过程。但在某些情况下,我们需要编写程序直接获 取站点的受保护资源,在这类情况下,就不能利用浏览器给定的界面去完成认证,而需要我们根据不同的协议人工地发送相应请求以完成整个认证过程。本文根据这 种需求给出一个基于 Apache HttpClient 应用包的解决方案。

J2EE 站点认证简介
出于安全性的需要和用户授权管理的考虑,常见的 J2EE 站点对特定资源都会加入认证/授权机制。例如一个公网上的论坛,一个只对特定用户开放的 RSS 或 Atom Feed,这些资源都必须在确信访问者为被授权用户时才能向访问者开放。为了实现这样的功能,J2EE 站点通常会采用某种站点认证机制,其中常见的有 HTTP Basic 认证和 J2EE Form-Based 认证。
HTTP Basic 认证
HTTP Basic 认证是 HTTP 认证协议(rfc2617)所定义的标准认证方式。要求 HTTP Basic 认证的服务器会在客户端访问受保护资源时向客户端发出请求,要求客户端上传用户名和密码对。服务器在收到用户名/密码并验证通过后,才将保护资源的内容返 回给客户端。它的工作机制如下图:
图 1. HTTP Basic 认证原理
 
由于是 HTTP 规范,因而常见的浏览器,如 Internet Explorer,Mozilla Firefox,在 步骤 2 中收到服务器对用户名和密码的请求时会弹出认证对话框,供用户输入用户名/密码。
图 2. Firefox 在收到步骤 2 中请求时弹出的用户名/密码输入框
 
HTTP Basic 认证方式使用 base64 编码方式传送用户名和密码,而 base64 仅仅是一种公开的编码格式而非加密措施,因而如果信道本身不使用 SSL 等安全协议,用户密码较容易被截获。
J2EE Form-Based 认证
Form-Based 认证不同于 HTTP Basic 认证,它是 J2EE 对于认证方式的一种扩展。它使用自定义的 HTML 表单(通常为 login.jsp)作为输入用户名和密码的用户界面,最终将用户在表单上填入的用户名/密码提交至服务器。它的工作机制如下:
图 3. Form-Based 认证原理
 
Form-Based 认证方式在 J2EE 站点中更为常见。这一方面是由于它提供了自定义的用户名密码输入界面;另一方面它的传输也更为安全,通常情况下 login.jsp 会被配置为需要使用 SSL 信道访问,这样在步骤 2、3 中对用户名和密码的传送就被安全信道所保护,而较难被非法截取。
Apache HttpClient 认证功能简介
Apache HttpClient 是 Apache 开源组织提供的纯 Java 实现的 HTTP 开源包。它能模拟各类 HTTP 客户端所需功能,例如 HTTP/HTTPS 连接,GET/PUT 请求,甚至提供了超时重试的功能。
HttpClient 也提供了对标准 HTTP 认证的接口,在最新的 HttpClient 3.1 中,支持的认证方式有:
  • Basic 认证:即前面提到的 rfc2716 规范中定义的 HTTP Basic 认证方式。
  • Digest 认证:一种基于摘要的更为安全的认证协议,虽然它的应用没有 Basic 认证方式广泛。
  • NTLM 认证:微软制定的认证协议规范,然而此项标准的细节却并不公开。
我们可以注意到 Form-Based 认证并不在其中,这是因为 Form-Based 认证方式并非 HTTP 协议标准,而是 J2EE 提供的一种特殊的认证方式,因而开发者需要在 HttpClient 基础上另行开发适合 Form-Based 认证的方案。
使用 Apache HttpClient 通过 HTTP Basic 认证
由于 HttpClient 内置支持 HTTP Basic 认证方式,因而使用 HttpClient 通过 HTTP Basic 认证的步骤显得较为简单。
  1. 为 HttpClient 的状态对象添加用户名/密码对。可以注意到在 setCredentials 方法中的另一个参数为 AuthScope 对象。事实上我们添加的每个用户名/密码对都与一个 AuthScope 对象相关联。AuthScope 对象确定了此用户名/密码对的适用站点,在示例中所给出的用户名/密码对将只适用于 www.sample.com 位于 80 端口上的资源。HttpClient 在与其他站点交互时将不会使用此用户名/密码对,这样有效地防止了机密数据被传送至不必要的站点。
  2. 开 启 HttpClient 提供的占先式(Preemptive)认证功能。开启了这个功能后,HttpClient 对于那些处在之前请求过的URI空间范围内的资源,会主动地随请求一起向服务器发送 Basic 认证数据,而不是等待服务器返回是否需要认证的响应后再提交认证。在多数情况下,能够减少请求-响应传递的次数,从而间接提高了服务器的响应能力。值得注 意的是在这种情况下必须在 AuthScope 对象中明确指定适用站点,以避免向不相关的站点泄漏敏感数据。
  3. 创建 GetMethod 对象,此对象将使用 GET 方式对保护资源发出 HTTP 请求。
  4. setDoAuthentication(true) 语句将告知 HttpClient 在服务器端发回需要认证的请求后,自动将我们在步骤 1 中设置的用户名/密码对发送至服务器,以完成认证过程。
执行 GET 请求,获取和处理受保护资源的内容。
清单 1. Basic 认证示例
  1.                   
  2. HttpClient client = new HttpClient();  
  3.       
  4. // 1  
  5. client.getState().setCredentials(  
  6.     new AuthScope("www.sample.com", 80, AuthScope.ANY_REALM),  
  7.     new UsernamePasswordCredentials("username", "password")  
  8. );  
  9.            
  10. // 2  
  11. client.getParams().setAuthenticationPreemptive(true);  
  12.  
  13. // 3  
  14. GetMethod get = new GetMethod("http://www.sample.com/protected.html");  
  15.  
  16. // 4  
  17. get.setDoAuthentication( true );  
  18.  
  19. try {  
  20.     // 5  
  21.     int status = client.executeMethod( get );  
  22.  
  23.     // process the content from the response  
  24.     …  
  25.  
  26. } finally {  
  27.     get.releaseConnection();  
  28. } 
由 于 Basic 认证方式直接向服务器发送未经加密的用户名/密码对,导致这些敏感数据很容易在网络传输过程中被截取,因此安全性很低。所幸 HttpClient 对基于安全套接字层(SSL)的 HTTP 协议(HTTPS)提供了足够的支持,而且使用起来也很简单。不过之前需确保本地机器已经安装好 JSSE(Sun 提供的 JDK 1.4 及之后的版本已集成 JSSE)。
使用 HttpClient 进行标准的 SSL 连接对用户来说是透明的。参照清单 1,用户只需用符合 HTTPS 协议的 URL 作为参数生成 GetMethod 对象即可。除此之外,HttpClient 还允许用户定制 SSL 使得客户端程序能够自动接受不同类型的证书。
利用 HttpClient 实现一个自定义的 SSL 协议包括以下 3 个关键步骤:
  1. 定制一个实现了 org.apache.commons.httpclient.protocol.SecureProtocolSocketFactory 接口的工厂类。这个工厂类的作用是开启一个与服务器通讯的 Socket 并进行必需的初始化动作。关于实现该接口的具体细节,HttpClient 项目的主页上有详细的代码实例和注释说明。
  2. 利用之前创建的工厂类对象、HTTPS 协议名称和默认端口号实例化一个新的 org.apache.commons.httpclient.protocol.Protocol 对象。
  3. 注册这个自定义的 Protocol 对象使其与某个协议名绑定,当 HttpClient 处理此类协议时,将默认调用这个自定义 Protocol 对象。
清单 2. 在 HttpClient 中自定义 SSL 示例
  1. // 1  
  2. SecureProtocolSocketFactory sampleSSLSocketFactory = new SampleSSLSocketFactory();  
  3.       
  4. // 2  
  5. Protocol httpsProtocol = new Protocol("https", sampleSSLSocketFactory, 443);  
  6.  
  7. // 3  
  8. Protocol.registerProtocol("https", httpsProtocol);  
  9.  
  10. HttpClient client = new HttpClient();  
  11.  
  12. client.getState().setCredentials(  
  13.     new AuthScope("www.sample.com", 80, AuthScope.ANY_REALM),  
  14.     new UsernamePasswordCredentials("username", "password")  
  15. );  
  16.            
  17. // Request the protected resource via SSL  
  18. GetMethod get = new GetMethod("https://www.sample.com/protected.html");  
  19.  
  20. get.setDoAuthentication( true );  
  21.  
  22. try {  
  23.     int status = client.executeMethod( get );  
  24.  
  25.     // process the content from the response  
  26.     …  
  27. } finally {  
  28.     get.releaseConnection();  
  29. } 
使用 Apache HttpClient 通过 Form-Based 认证
Form-Based 认证相对 HTTP Basic 认证而言过程较为复杂,需要开发者记录下相关的 cookie 信息和部分 header 字段并多次向站点发出请求。它的大致原理如下:
注意:不同的应用可能有不同的配置方式,开发者可以先在浏览器中手动访问受保护资源,获取 login.jsp。进行分析后即可获知对应的认证服务资源 j_security_check 的位置以及对应的用户名与密码在表单中的字段。
假 定我们需要访问的受保护资源为 http://www.sample.com/sampleApp/sample.rss。首先我们需要向此保护资源发出请求。而由 Form-Based 认证原理一节中可知,J2EE 服务器会将此请求重定向至 login.jsp。如果仔细分析 login.jsp 我们能发现它仅仅是一个 HTML 表单,其中有两个字段 j_username 和 j_password 分别记录用户名和密码,而提交的目标则是 j_security_check。通常情况下,J2EE 构架会在每个站点应用的根节点定义一个 j_security_check 的资源。而我们的站点的应用程序根(Application Root)为 sampleApp。因而,通过将用户名,密码以及相关 cookie 和 header 字段以 POST 方式发送至 http://www.sample.com/sampleApp/j_security_check 即可通过站点认证。在通过站点认证后,服务器端将给出一个新的重定向,通常它将指向了用户最初试图访问的受保护资源(本例中也就是 http://www.sample.com/sampleApp/sample.rss)。我们只需要再次创建访问对象向此资源发出请求即可获得其内 容。
以下给出一个示例:
清单 3. Form-Based 认证示例
  1.                   
  2. HttpClient client = new HttpClient();  
  3. client.getState().setCookiePolicy(CookiePolicy.COMPATIBILITY);  
  4.  
  5. // 1  
  6. GetMethod authget = new GetMethod("httpwww.sample.comsampleAppsample.rss");  
  7. try {  
  8.     client.executeMethod(authget);  
  9. }  
  10. catch (HttpException httpe) {  
  11.     httpe.printStackTrace();  
  12. }  
  13. catch (IOException ioe) {  
  14.     ioe.printStackTrace();  
  15. }  
  16.  
  17. // 2  
  18. NameValuePair[] data = new NameValuePair[2];  
  19. data[0] = new NameValuePair("j_username", username);  
  20. data[1] = new NameValuePair("j_password", password);  
  21.  
  22. PostMethod authpost = new PostMethod("http://www.sample.com/sampleApp/j_security_check");  
  23. authpost.setRequestBody(data);  
  24.  
  25. // 3  
  26. Header hCookie = authget.getRequestHeader("Cookie");  
  27. Header hHost = authget.getRequestHeader("Host");  
  28. Header hUserAgent = authget.getRequestHeader("User-Agent");  
  29. if (hCookie == null || hHost == null || hUserAgent == null) {  
  30.     return null;  
  31. }  
  32.  
  33. authpost.setRequestHeader(hCookie);  
  34. authpost.setRequestHeader(hHost);  
  35. authpost.setRequestHeader(hUserAgent);  
  36.  
  37. authget.releaseConnection();  
  38.  
  39. try {  
  40.     client.executeMethod(authpost);  
  41.  
  42.     // 4  
  43.     Header header = authpost.getResponseHeader("location");  
  44.     if (header != null) {  
  45.         String newuri = header.getValue();   
  46.         GetMethod redirect = new GetMethod(newuri);  
  47.  
  48.         client.executeMethod(redirect);   
  49.         // process the content from the response  
  50.         redirect.releaseConnection();              
  51.     }  
  52. } catch (HttpException httpe) {  
  53.     httpe.printStackTrace();  
  54.     return null;  
  55. } catch (IOException ioe) {  
  56.     ioe.printStackTrace();  
  57.     return null;  
  58. }  
  59. authpost.releaseConnection(); 
其中各个步骤解释如下:
  1. 使用 GET 方式请求 sample.rss。服务器收到连接后将在响应中给出连接信息,HttpClient 在接收到响应后会将其保存至 cookie 中。
  2. 准备第二次对 j_security_check 的连接,将用户名和密码填入新的 POST 请求的正文。
  3. 将 cookie 和部分 header 字段拷贝至新请求的报头中,并发送请求。
  4. 从认证成功的响应中获取重定向,并对重定向指向的资源发出请求,获取并处理内容。
小  结
随着 Web 2.0 时代的到来,Web 站点的数据和内容显得愈加重要。而为了收集这些数据,人们需要利用计算机本身的搜集能力,通过后台请求,而不是浏览器交互的方式去获取站点的数据。而商业 站点中普遍存在的认证/授权机制显然成为了开发此类数据收集程序的一道屏障。Apache HttpClient 根据这些需求,提供了多种 HTTP 认证机制的实现方案。开发人员也可以利用 HttpClient 强大的底层功能,设计特定方案以通过 J2EE 站点的认证体系。

原文链接:http://www.ibm.com/developerworks/cn/java/j-lo-httpclient-j2ee/

運用 Apache HttpClient 實作 Get 與 Post 動作

samsharehome.blogspot.tw/2008/11/apache-httpclientgetpost.html?showComment=1264661053805_AIe9_BHFZY__ebfCZrBGIz-GE-Fb1z-sDxo4Axz90FMokCgF2jhXmJme4cHteyjAITYOg-hW6NnMAqEGG5uBU0-UVUWFE15aneBG0kjRD9R6OfL4h1Y3IiSK_Y3juk5aXnc6HB-o0gnyU7MZCxREOxEtcLxEFTOtKTviSNVkXI0nE0Ropm8A8OhPebty7DUZlhYKFlihOb7w_1jm4kLs9TDbnVFF43F_CHu8hcv4wPiXjm1hKNCuif0#c2678734266814512896

HttpClient 簡介:

HTTP 協定是現在 Internet 上使用得最多、最重要的協定,越來越多的 Java 應用程序需要直接通過 HTTP 協定來訪問網路資源。 雖然在 JDK 的 java.net 包中已經提供了訪問 HTTP 協定的基本功能,但是對於大部分應用程序來說,JDK 類別庫本身提供的功能還不夠豐富和靈活。 HttpClient 是 Apache Jakarta Common 下的子項目,用來提供高效能、最新、功能豐富的支持 HTTP 協定的client端開發工具,並且它支持 HTTP 協定最新的版本和建議。 HttpClient 已經應用在很多的項目中,比如 Apache Jakarta 上很著名的另外兩個開源項目 Cactus 和 HTMLUnit 都使用了 HttpClient,更多使用 HttpClient 的應用可以參見http://wiki.apache.org/jakarta-httpclient/HttpClientPowered。 HttpClient 項目非常活躍,使用的人還是非常多的。目前 HttpClient 正式版本是 3.1。
HttpClient 功能介紹
以下列出的是 HttpClient 提供的主要的功能,要知道更多詳細的功能可以參見 HttpClient 的主頁。
1. 實現了所有 HTTP 的方法(GET,POST,PUT,HEAD 等)
2. 支持自動轉向
3. 支持 HTTPS 協議
4. 支持代理服務器

下面將逐一介紹怎樣使用這些功能。首先,我們必須安裝好 HttpClient。
HttpClient 可以在http://hc.apache.org/downloads.cgi下載
HttpClient用到了logging,你可以從這個地址http://commons.apache.org/downloads/download_logging.cgi下載到 common-logging,從下載後的壓縮包中取出 commons-logging.jar 加到 CLASSPATH 中
HttpClient用到了codec,你可以從這個地址http://commons.apache.org/downloads/download_codec.cgi 下載到最新的 common-codec,從下載後的壓縮包中取出 commons-codec-1.x.jar 加到 CLASSPATH 中

Get Method:

import java.io.IOException;

import org.apache.commons.httpclient.*;
import org.apache.commons.httpclient.methods.*;
import org.apache.commons.httpclient.params.HttpMethodParams;

public class HttpGet
{
    private static String url = "http://www.apache.org/"; // 目標網址.

    public static void main(String[] args)
    {
        // 建立HttpClient實體.
        HttpClient client = new HttpClient();

        // 建立GetMethod實體, 並指派網址, GetMethod會自動處理該網轉址動作, 如果不想自動轉址請呼叫 setFollowRedirects(false).
        GetMethod method = new GetMethod(url);

        // 這段代碼用意為連接不到時自動重新��試三次.
        method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler(3, false));

        try {
            // 返回狀態值.
            int statusCode = client.executeMethod(method);
            if (statusCode != HttpStatus.SC_OK) {
                System.err.println("Method failed: " + method.getStatusLine());
            }

            // 取得回傳資訊.
            byte[] responseBody = method.getResponseBody();
            System.out.println(new String(responseBody));

        } catch (HttpException httpexc) {
            System.err.println("Fatal protocol violation: " + httpexc.getMessage());
            httpexc.printStackTrace();
        } catch (IOException ioexc) {
            System.err.println("Fatal transport error: " + ioexc.getMessage());
            ioexc.printStackTrace();
        } finally {

            // ** 無論如何都必須釋放連接.
            method.releaseConnection();
        }
    }
}


由於是執行在網路上的程序,運行executeMethod方法時,需要處理兩個異常,分別是HttpException和IOException。 第一種異常的原因主要可能是在建立GetMethod的時候輸入網址錯誤,比如不小心將"http"寫成"htp",或者遠端返回的資訊內容不正常等,並 且該異常發生是不可恢復的。
第二種異常一般是由於網路原因引起的異常,對於這種異常 (IOException),HttpClient會根據你指定的恢復策略自動試著重新執行executeMethod方法。
HttpClient的恢復策略可以自定義(通過實現接口HttpMethodRetryHandler來實現)。通過httpClient的方法 setParameter設置你實現的恢復策略,這裡使用的是系統提供的預設恢復方式,該方式在碰到第二類異常的時候將自動重試3次。
executeMethod返回值是一個整數,表示了執行該方法後服務器返回的狀態碼,該狀態碼能表示出該方法執行是否成功、需要認證或者頁面轉址(預設狀態下GetMethod是自動處理轉址)等。
POST Method:
import java.io.IOException;
import org.apache.commons.httpclient.*;
import org.apache.commons.httpclient.methods.*;

public class HttpPost
{
    private static String url = "http://www.apache.org/"; // 目標網址.

    public static void main(String[] args)
    {
        // 建立HttpClient實體.
        HttpClient client = new HttpClient();

        // 建立PostMethod實體, 並指派網址
        PostMethod post = new PostMethod(url);

        // 建立NameValuePair陣列�儲欲傳送的資料, 對照為 (名稱, 內容)
        NameValuePair[] data = { new NameValuePair("Name", "Sam Wang"), new NameValuePair("Passwd", "Test1234") };

        // 將NameValuePair陣列設置到請求內容中
        post.setRequestBody(data);

        try {
            // 返回狀態值.
            int statusCode = client.executeMethod(post);
            if (statusCode != HttpStatus.SC_OK) {
                System.err.println("Method failed: " + post.getStatusLine());
            }

            // 取得回傳資訊.
            byte[] responseBody = post.getResponseBody();
            System.out.println(new String(responseBody));

        } catch (HttpException httpexc) {
            System.err.println("Fatal protocol violation: " + httpexc.getMessage());
            httpexc.printStackTrace();
        } catch (IOException ioexc) {
            System.err.println("Fatal transport error: " + ioexc.getMessage());
            ioexc.printStackTrace();
        } finally {

            // ** 無論如何都必須釋放連接.
            post.releaseConnection();
        }
    }
}



PostMethod運作重點大致上和GetMethod相同, 唯一不同的是傳送變數必須用NameValuePair[]陣列儲存, 再設置到requestBody中。