<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    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)
       
    {
         
    //構造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 半導體 閱讀(951) 評論(0)  編輯  收藏 所屬分類: 通訊


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


    網站導航:
     
    主站蜘蛛池模板: 欧美a级成人网站免费| 亚洲香蕉久久一区二区三区四区| 免费电视剧在线观看| 成人性生交大片免费看中文| 成a人片亚洲日本久久| 亚洲人成777在线播放| 亚洲视频在线观看一区| 亚洲乱码精品久久久久..| 在线免费观看国产视频| 国产v精品成人免费视频400条| 日本免费在线观看| 国产成人无码精品久久久久免费 | 日本高清不卡中文字幕免费| 456亚洲人成影院在线观| 国产亚洲精品观看91在线| 国产a v无码专区亚洲av| 免费永久看黄在线观看app| 免费无码又爽又高潮视频| 免费99精品国产自在现线| 免费无码毛片一区二区APP| 中文字幕免费不卡二区| 国产在线观看免费av站| a免费毛片在线播放| 乱淫片免费影院观看| 三年片在线观看免费观看大全中国| 亚洲成a人片在线观看天堂无码| 精品亚洲成A人无码成A在线观看 | 99久久久国产精品免费牛牛 | 久久精品国产精品亚洲蜜月| 久久亚洲精品中文字幕三区| 中国亚洲女人69内射少妇| 亚洲午夜久久久久久久久久| 亚洲人成影院在线无码按摩店| 亚洲一区二区三区无码影院| 亚洲一区二区三区乱码A| 亚洲中文字幕无码一区 | 午夜爽爽爽男女免费观看影院| 日本免费电影一区二区| 99久久久国产精品免费牛牛四川| 18禁成人网站免费观看| 波多野结衣中文字幕免费视频|