<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)  編輯  收藏 所屬分類: 通訊


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


    網站導航:
     
    主站蜘蛛池模板: 又爽又黄无遮挡高清免费视频| 久久久久国产亚洲AV麻豆| 亚洲高清毛片一区二区| 亚洲国产成人VA在线观看| 成人片黄网站色大片免费观看APP| 亚洲视频免费在线看| 尤物永久免费AV无码网站| 中文字幕人成无码免费视频| 一区二区亚洲精品精华液| 亚洲精品tv久久久久| 最近中文字幕大全免费视频| 久久亚洲精品无码网站| 亚洲a一级免费视频| 中文字幕一区二区免费| 亚洲伊人精品综合在合线| 2022中文字字幕久亚洲| 国产在线观看麻豆91精品免费| 羞羞漫画登录页面免费| 亚洲成AⅤ人影院在线观看| 久久狠狠躁免费观看2020| 亚洲精品乱码久久久久久| 桃子视频在线观看高清免费完整| 丰满少妇作爱视频免费观看| 亚洲精品电影在线| 一本色道久久综合亚洲精品| 24小时日本在线www免费的| 在线观看片免费人成视频无码| 亚洲精品无码久久久久APP| 亚洲欧洲日韩国产综合在线二区| 免费永久在线观看黄网站| 五月婷婷在线免费观看| 国产啪精品视频网站免费尤物| 美女免费视频一区二区三区| 国产精品高清视亚洲精品| 亚洲第一精品在线视频| 亚洲日韩人妻第一页| 国产一区二区三区无码免费| 在线观看成人免费视频不卡| 久久国产精品免费网站| ssswww日本免费网站片| 狠狠入ady亚洲精品|