public class HttpPlugin{
private static Log log = LogFactory.getLog(HttpPlugin.class);
@Override
public void init() {
}
public void sendGet(String url , Parameter param)
{
StringBuffer p=new StringBuffer();
boolean first=true;
for(Map.Entry<String, String> pair : param.parameters.entrySet()){
if(first) first=false;
else p.append('&');
p.append(pair.getKey());
p.append('=');
p.append(pair.getValue());
}
try
{
String urlName = url + "?" + p.toString();
URL realUrl = new URL(urlName);
URLConnection conn = realUrl.openConnection();
conn.connect();
//获取所有响应头字段
Map<String,List<String>> map = conn.getHeaderFields();
for (String key : map.keySet())
{
System.out.println(key + "--->" + map.get(key));
}
Scanner scanner=new Scanner(conn.getInputStream(),"gb2312");//以gb2312格式接收,避免中文乱码
StringBuffer str=new StringBuffer();
while(scanner.hasNextLine()) {
str.append(scanner.nextLine());
str.append("/n");
}
System.out.println(str.toString());
}catch(Exception e){
log.error(e.getMessage());
}
}
public void sendPost(String url,Parameter param)
{
PrintWriter out = null;
String result = "";
try{
URL realUrl = new URL(url);
URLConnection conn = realUrl.openConnection();
//发送POST请求设置
conn.setDoOutput(true);
conn.setDoInput(true);
out = new PrintWriter(conn.getOutputStream());
boolean first=true;
for(Map.Entry<String, String> pair : param.parameters.entrySet()){
if(first) first=false;
else out.print('&');
out.print(pair.getKey());
out.print('=');
out.print(pair.getValue());
}
out.flush();
Scanner scanner=new Scanner(conn.getInputStream(),"gb2312");//以gb2312格式接收
StringBuffer str=new StringBuffer();
while(scanner.hasNextLine()) {
str.append(scanner.nextLine());
str.append("/n");
}
System.out.println(str.toString());
if (out != null){
out.close();
}
}catch(Exception e){
log.error(e.getMessage());
}
System.out.println(result);
}
public boolean getBinaryFile(String url){
try{
URL u = new URL(url);
URLConnection uc = u.openConnection();
String contentType = uc.getContentType();
int contentLength = uc.getContentLength();
if (contentType.startsWith("text/") || (contentLength == -1))
{
log.error("getBinaryFile-->not a binary file");
return false;
}
InputStream raw = uc.getInputStream();
InputStream in = new BufferedInputStream(raw);
byte[] data = new byte[contentLength];
int bytesRead = 0;
int offset = 0;
while (offset < contentLength)
{
bytesRead = in.read(data,offset,data.length-offset);
if(bytesRead == -1) break;
offset +=bytesRead;
}
in.close();
if (offset != contentLength)
{
log.error("getBinaryFile-->Only read "+offset+" bytes;Expected "+contentLength+" bytes.");
return false;
}
String fileName = u.getFile();
String filePath = "D://";
fileName = fileName.substring(fileName.lastIndexOf('/')+1);
FileOutputStream fout = new FileOutputStream(filePath+fileName);
fout.write(data);
fout.flush();
fout.close();
}catch(Exception e){
log.error(e.getMessage());
return false;
}
return true;
}
public Parameter getParameter(){
return new Parameter();
}
public class Parameter{
private Map<String,String> parameters=new HashMap<String,String>();
private Parameter(){
parameters=new HashMap<String,String>();
}
public boolean addParam(String name,String value){
if(parameters!=null){
parameters.put(name, value);
return true;
}else{
return false;
}
}
public boolean delParam(String name){
if(parameters!=null){
if(parameters.remove(name)==null){
return false;
}else{
return true;
}
}else{
return false;
}
}
}
}