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 半导体 阅读(860) 评论(0)  编辑  收藏 所属分类: 通讯


只有注册用户登录后才能发表评论。


网站导航: