比如说一个SERVLET的地址是
http://www.sina.com.cn/test/servlet
(这个SERVLET是我自己可以修改的)
现在我这里的程序要测试这个连结是否正常
我在JAVA里面的程序应该怎么写才好?
这个还有跨域的问题,不能用AJAX去试
我想在JAVA里面测试
redstarofsleep的没错,我来个完整的
[code="java"]
import java.io.IOException;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.StringRequestEntity;
public class HttpSender {
public String send(String url, String request) throws Exception {
String response = null;
HttpClient httpClient = new HttpClient();
PostMethod method = new PostMethod(url);
method.addRequestHeader("charset", "GBK");
method.addRequestHeader("content-type", "application/x-www-form-urlencoded");
method.setRequestEntity(new StringRequestEntity(request, null, "GBK"));
try {
int retcode = httpClient.executeMethod(method);
if (retcode == HttpStatus.SC_OK) {
byte[] responseBody = new byte[10240];
java.io.InputStream istream = method.getResponseBodyAsStream();
int npos = 0;
int nread = 0;
while ((nread = istream.read(responseBody, npos, responseBody.length - npos)) >= 0) {
npos += nread;
if (npos >= responseBody.length) {
byte[] tmpBuf = new byte[npos + 51200];
System.arraycopy(responseBody, 0, tmpBuf, 0, npos);
responseBody = tmpBuf;
}
}
response = new String(responseBody, 0, npos);
} else {
throw new IOException("发送失败: retcode: " + retcode);
}
method.releaseConnection();
} catch (Exception e) {
e.printStackTrace();
}
return response;
}
public static void main(String[] args) throws Exception {
String reqStr = "<?xml version=\"1.0\" encoding=\"GBK\"?><request><params><param><curDate>20101010</curDate></param></params></request>";
HttpSender sender = new HttpSender();
String reponse = sender.send("http://www.sina.com.cn/test/servlet", reqStr);
System.out.println("返回报文"+reponse);
}
}
[/code]
当然,你的web.xml要配置
[code="java"]
servlet
com.xx.YourServlet
<servlet-mapping>
<servlet-name>servlet</servlet-name>
<url-pattern>/servlet</url-pattern>
</servlet-mapping>
[/code]
[code="java"]
URL url = new URL("http://www.hrtsea.com/down/soft/45.htm");
//返回一个 URLConnection 对象,它表示到 URL 所引用的远程对象的连接。
URLConnection uc = url.openConnection();
//打开的连接读取的输入流。
InputStream in = uc.getInputStream();
int c;
while ((c = in.read()) != -1)
System.out.print(c);
in.close();
[/code]
直接用浏览器输入地址不就行了么,要搞那么麻烦干嘛
用Java发送Http请求
可以用Apache的Httpclient包
[code="java"]
HttpClient httpClient = new DefaultHttpClient();
// HttpGet get = new HttpGet("http://local:8084");
HttpPost get = new HttpPost("http://localhost:8080");
HttpResponse response = httpClient.execute(get);
HttpEntity entity = response.getEntity();
String xmlString = null;
if (entity != null) {
entity = new BufferedHttpEntity(entity);
InputStream in = entity.getContent();
byte[] read = new byte[1024];
byte[] all = new byte[0];
int num;
while ((num = in.read(read)) > 0) {
byte[] temp = new byte[all.length + num];
System.arraycopy(all, 0, temp, 0, all.length);
System.arraycopy(read, 0, temp, all.length, num);
all = temp;
}
System.out.println(new String(all));
xmlString = new String(all);
in.close();
}
get.abort();
[/code]
使用HttpClient
最简单的方式就是通过浏览器直接敲地址验证。
非要用Java的话,可以模拟Http的请求去验证。可用的第三方插件例如apache的http插件。
window.location就可以了吗?楼主如果只是单独的测试这个问题肯定搞定。