JBOSS 點滴

          豐豐的博客

          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)
             
          {
               
          //構(gòu)造HttpClient的實例
               HttpClient httpClient = new HttpClient();
               
          //創(chuàng)建GET方法的實例
               GetMethod getMethod = new GetMethod("http://www.baidu.com");
               
          //GetMethod getMethod = new GetMethod("http://10.164.80.52/dav/5000/moban.rar");
               
          //使用系統(tǒng)提供的默認(rèn)的恢復(fù)策略
               getMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,new DefaultHttpMethodRetryHandler());
               
          try
               
          {
                   
          //執(zhí)行g(shù)etMethod
                   int statusCode = httpClient.executeMethod(getMethod);
                   
          if (statusCode != HttpStatus.SC_OK)
                   
          {
                       System.err.println(
          "Method failed: " + getMethod.getStatusLine());
                   }

                   
          //讀取內(nèi)容
                   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();   

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

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

               
          catch (IOException e)
               
          {
                   
          //發(fā)生網(wǎng)絡(luò)異常
                   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);  
                          
          //  下面代碼根據(jù)節(jié)點名稱獲得xml文件節(jié)點內(nèi)的信息并輸出。 
                          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 半導(dǎo)體 閱讀(959) 評論(0)  編輯  收藏 所屬分類: 通訊


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


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 麻城市| 民权县| 武安市| 仲巴县| 什邡市| 桐梓县| 类乌齐县| 北碚区| 泰和县| 拉萨市| 安乡县| 新津县| 鄂伦春自治旗| 岳池县| 兰坪| 香格里拉县| 柘城县| 那曲县| 汉中市| 邯郸市| 郓城县| 保康县| 炎陵县| 九台市| 衡水市| 吴旗县| 余干县| 绥芬河市| 清镇市| 嘉兴市| SHOW| 北流市| 浏阳市| 石嘴山市| 蕉岭县| 兴城市| 石城县| 焉耆| 永仁县| 广宗县| 凤阳县|