📄 httpsender.java
字号:
/**
* HttpSender.java
*
* Created on 2003-7-5 12:30:01
*
*/
package com.liuyang.ejb.cmp.node.servlet;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.methods.EntityEnclosingMethod;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;
/**
* @author
* <a href="mailto:jdcyang@yahoo.com.cn">刘洋</a>
*/
public class HttpSender {
public static void main(String[] args){
//post("http://localhost:8080/node/node?title=title01","text01.txt");
//get("http://localhost:8080/node/node?title=title01");
//get("http://localhost:8080/node/nodebook?title=title01");
//post("http://localhost:8080/node/xmlnode","node.xml");
get("http://localhost:8080/node/xmlnode?title=this is a title");
}
public static int get(String url){
GetMethod get = new GetMethod(url);
HttpClient httpclient = new HttpClient();
int result = 0;
try {
result = httpclient.executeMethod(get);
InputStreamReader inreader = new InputStreamReader(get.getResponseBodyAsStream());
BufferedReader breader = new BufferedReader(inreader);
String d = breader.readLine();
while(d!=null){
System.out.println(d);
d = breader.readLine();
}
} catch (HttpException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return result;
}
public static int post(String desturl,String srcurl){
InputStream is = null;
URL xmlurl;
try {
xmlurl = new URL(srcurl);
is = xmlurl.openStream();
} catch (Exception e) {
File file = new File(srcurl);
if(file.canRead()){
try {
is = new FileInputStream(file);
} catch (FileNotFoundException e3) {
System.out.println("the source you input error");
return 0;
}
}else{
System.out.println("the source you input error");
return 0;
}
}
PostMethod post = new PostMethod(desturl);
try {
post.setRequestBody(is);
} catch (IllegalArgumentException e2) {
e2.printStackTrace();
}
post.setRequestContentLength(EntityEnclosingMethod.CONTENT_LENGTH_CHUNKED);
HttpClient httpclient = new HttpClient();
int result = 0;
try {
result = httpclient.executeMethod(post);
if(result==200)
System.out.println("post 成功");
InputStreamReader inreader = new InputStreamReader(post.getResponseBodyAsStream());
BufferedReader breader = new BufferedReader(inreader);
String d = breader.readLine();
while(d!=null){
System.out.println(d);
d = breader.readLine();
}
} catch (HttpException e1) {
e1.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
}
return result;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -