JBOSS 點滴

          豐豐的博客

          導航

          <2010年10月>
          262728293012
          3456789
          10111213141516
          17181920212223
          24252627282930
          31123456

          統計

          公告

          我想成功,在老之前!

          常用鏈接

          留言簿(6)

          隨筆分類

          隨筆檔案

          文章分類

          文章檔案

          相冊

          收藏夾

          blogjava

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          WebService 客戶端代碼

          JDK1.4 --http
          需要的包:
          1、commons-httpclient-3.0.1.jar
          2、commons-logging-1.1.jar
          3、commons-codec-1.3.jar

          程序:
          GetSample.java

          import java.io.*;
          import java.io.IOException;
          import org.apache.commons.httpclient.*;
          import org.apache.commons.httpclient.methods.GetMethod;
          import org.apache.commons.httpclient.params.HttpMethodParams;
          public class GetSample
          {
             
          public static void main(String[] args)
             
          {
               
          //構造HttpClient的實例
               HttpClient httpClient = new HttpClient();
               
          //創建GET方法的實例
               GetMethod getMethod = new GetMethod("http://www.baidu.com");
               
          //GetMethod getMethod = new GetMethod("http://10.164.80.52/dav/5000/moban.rar");
               
          //使用系統提供的默認的恢復策略
               getMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,new DefaultHttpMethodRetryHandler());
               
          try
               
          {
                   
          //執行getMethod
                   int statusCode = httpClient.executeMethod(getMethod);
                   
          if (statusCode != HttpStatus.SC_OK)
                   
          {
                       System.err.println(
          "Method failed: " + getMethod.getStatusLine());
                   }

                   
          //讀取內容
                   byte[] responseBody = getMethod.getResponseBody();
                  
                  
                   String serverfile 
          = "d:\\Test_baidu.html";
                   
          //String serverfile = "d:\\moban.rar";
                   OutputStream serverout = new FileOutputStream(serverfile);
                  
                   serverout.write(responseBody);   
                   serverout.flush();   
                   serverout.close();   

                   
          //處理內容
                   
          //System.out.println(new String(responseBody));
                   System.out.println("OK!");
               }

               
          catch (HttpException e)
               
          {
                   
          //發生致命的異常,可能是協議不對或者返回的內容有問題
                   System.out.println("Please check your provided http address!");
                   e.printStackTrace();
               }

               
          catch (IOException e)
               
          {
                   
          //發生網絡異常
                   e.printStackTrace();
               }

               
          finally
               
          {
                   
          //釋放連接
                   getMethod.releaseConnection();
               }

             }

          }



          JDK1.5及以上--http
          需要的包:
          httpclient-4.0.3.jar
          httpmine-4.0.3.jar
          httpcore-4.0.1.jar
          httpcore-nio-4.0.1.jar
          common-logging-1.0.4.jar

          import java.io.IOException;

          import org.apache.http.client.ClientProtocolException;
          import org.apache.http.client.HttpClient;
          import org.apache.http.client.ResponseHandler;
          import org.apache.http.client.methods.HttpGet;
          import org.apache.http.impl.client.BasicResponseHandler;
          import org.apache.http.impl.client.DefaultHttpClient;
          import org.apache.http.params.HttpProtocolParams;
          public class HttpTask {

              
          /**
               * 
          @param args
               
          */

                  
          public static void main(String[] args) throws Exception
                  
          // TODO Auto-generated method stub   
                            HttpTask  ht = new HttpTask();
                            System.out.println(ht.getTasklist());
                         }
             /**
                       * 
          @throws IOException */
           
              
          public String getTasklist()  throws ClientProtocolException,IOException{
                  String url 
          = "/web/gettask";       
                  String host 
          = "92.168.1.3";    
                  String param 
          = "usercode=3&password=123";   
                  HttpClient httpclient 
          = new DefaultHttpClient();     
                  httpclient.getParams().setParameter(HttpProtocolParams.HTTP_CONTENT_CHARSET,
          "UTF-8");    
            
                  HttpGet httpget 
          = new HttpGet("http://"+host+url+"?"+param); 
                  System.out.println(
          "http://"+host+url+"?"+param);
            
                  ResponseHandler responseHandler 
          = new BasicResponseHandler();      
                  String responseBody 
          = (String)httpclient.execute(httpget, responseHandler);    
                  
          //System.out.println(responseBody);  /***/   
                  httpclient.getConnectionManager().shutdown();
                    
          return responseBody;
              }


              }



          獲得XML代碼:
          需要的包:
          jdom.jar
          commons-logging-1.1.1.jar
          commons-discover-0.2.jar
          需要引入的類:
          java.io.StringReader,org.jdom.Document,org.jdom.Element,org.jdom.JDOMException,org.jdom.input.SAXBuilder,java.net.Socket


                  try{

                      GetHttp  ht 
          = new GetHttp();
                      out.println(ht.getTaskList());
                      

                      SAXBuilder sb 
          = new SAXBuilder(false);
                      StringReader read 
          = new StringReader(taskstr); 
                      Document doc 
          = sb.build(read);
              
                      Element rootElt 
          = doc.getRootElement();
                      java.util.List Allstudents 
          = rootElt.getChildren("Item"); // 得到根元素所有子元素的集合 
                      Element student = null;
                      
                     
          for (int i = 0; i < Allstudents.size(); i++{
                          student 
          = (Element) Allstudents.get(i);  
                          
          //  下面代碼根據節點名稱獲得xml文件節點內的信息并輸出。 
                          out.println("<a href="+student.getChild("LinkUrl").getText()+">"+student.getChild("TITLE").getText()+"</a>"); 
                          out.println(
          "地址為:"+student.getChild("LinkUrl").getText()); 
                        }
           /***/
                    }
          catch(Exception ex) 
                      
          {
                          ex.printStackTrace();
                      }

          posted on 2010-10-21 09:00 半導體 閱讀(959) 評論(0)  編輯  收藏 所屬分類: 通訊


          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 牡丹江市| 格尔木市| 缙云县| 崇信县| 琼中| 兴文县| 塔河县| 奉新县| 九寨沟县| 姜堰市| 义马市| 开远市| 渭南市| 张掖市| 临西县| 汉沽区| 时尚| 同德县| 那坡县| 萨迦县| 邵东县| 渑池县| 黑山县| 福贡县| 阿坝县| 永修县| 河间市| 宜君县| 莱州市| 娱乐| 明光市| 噶尔县| 仪征市| 元阳县| 崇明县| 肇庆市| 南华县| 留坝县| 岚皋县| 房产| 马公市|