java Source

  BlogJava :: 首页 :: 联系 :: 聚合  :: 管理
  14 Posts :: 24 Stories :: 8 Comments :: 0 Trackbacks
package org.lambdasoft.http;

import java.util.Map;

/**
 * 
@author lei.tang (justinlei@gmail.com)
 * @date 
 * 
@version
 
*/
public interface HttpRequest {
    String execute(String url,Map
<String, String> params) throws Exception;
}

package org.lambdasoft.http;

import java.util.Map;
import java.util.Set;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.GetMethod;

/**
 * 
@author lei.tang (justinlei@gmail.com)
 * @date 
 * 
@version
 
*/
public class GetRequest implements HttpRequest{

    @Override
    
public String execute(String url, Map<String, String> params) throws Exception {
        HttpClient client 
= new HttpClient();
        GetMethod getMethod 
= new GetMethod(url);
        
if(params != null && params.size() != 0) {
            Set
<String> keySet = params.keySet();
            NameValuePair[] nameValuePairs 
= new NameValuePair[params.size()];
            
int i = 0;
            
for (String key : keySet) {
                nameValuePairs[i] 
= new NameValuePair(key, params.get(key));
                i
++;
            }
            getMethod.setQueryString(nameValuePairs);
        }
        
int statusCode = client.executeMethod(getMethod);
        
if (statusCode != HttpStatus.SC_OK) {
            
throw new Exception(getMethod.getStatusLine().toString());
        }
        
return new String(getMethod.getResponseBody());
    }
}




package org.lambdasoft.http;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.PostMethod;

/**
 * 
@author lei.tang (justinlei@gmail.com)
 * @date 
 * 
@version
 
*/
public class PostRequest implements HttpRequest{

    @Override
    
public String execute(String url, Map<String, String> params)
            
throws Exception {
        HttpClient client 
= new HttpClient();
        PostMethod postMethod 
= new PostMethod(url);
        
        
if(params != null && params.size() != 0) {
            Set
<String> keySet = params.keySet();
            NameValuePair[] nameValuePairs 
= new NameValuePair[params.size()];
            
int i = 0;
            
for (String key : keySet) {
                nameValuePairs[i] 
= new NameValuePair(key, params.get(key));
                i
++;
            }
            postMethod.setQueryString(nameValuePairs);
        }
        
int statusCode = client.executeMethod(postMethod);
        
if (statusCode != HttpStatus.SC_OK) {
            
throw new Exception(postMethod.getStatusLine().toString());
        }
        
return new String(postMethod.getResponseBody());
    }

}
posted on 2010-04-06 16:29 JustinLei 阅读(2244) 评论(2)  编辑  收藏

Feedback

# re: apache commons httpclient[未登录] 2010-04-06 20:55 dd
你这是httpclient 3.0的代码,这个版本有很多制限,还是不要用这个版本,有误导人的嫌疑 。  回复  更多评论
  

# re: apache commons httpclient 2010-04-07 00:26 JustinLei
详细?  回复  更多评论
  


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


网站导航: