Java 与 PHP 发送get请求并获取远程内容
既然两种语言都玩,就有得对比了,先看JAVA实现,再来看PHP,当然实现的方法有很多种特别是JAVA对于同一种功能的实现绝对比PHP多很多,这点是毫无疑问的!
JAVA实现方法一:
package com.jiucool.www.struts.action;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
public class doget_http_request{
public String doget_http_request(){
StringBuffer readOneLineBuff = new StringBuffer();
String content ="";
try {
URL url = new URL("http://www.jiucool.com/sendemail.php?key=j0r53nmbbd78x7m1" + "&activatecode=2QyiF0SXXTq8");
URLConnection conn = url.openConnection();
BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String line = "";
while ((line = reader.readLine()) != null) {
readOneLineBuff.append(line);
}
content = readOneLineBuff.toString();
reader.close();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e2) {
e2.printStackTrace();
}
return content;
}
}对于PHP的实现,简单得让人要跳楼,看代码:
<?php echo file_get_contents("http://www.jiucool.com/sendemail.php?key=j0r53nmbbd78x7m1&activatecode=2QyiF0SXXTq8");?>
PHP实在太简单了!不过有一点,PHP的弱类型语言让人实在有点不习惯,变量不声明即可使用,太不严谨了!aspx">
补充:Web开发 , Jsp ,