81,091
社区成员
发帖
与我相关
我的任务
分享
我是linux的服务器上 部署的java程序,公网客户端访问系统,在服务器上测试 www.189.cn,www.youku.com网站都正常,而且速度还挺快,测试结果如下:-bash-3.2$ wget 'www.189.cn'
--2012-05-07 09:17:32-- http://www.189.cn/
正在解析主机 www.189.cn... 118.85.207.18
Connecting to www.189.cn|118.85.207.18|:80... 已连接。
已发出 HTTP 请求,正在等待回应... 200 OK
长度:41881 (41K) [text/html]
Saving to: `index.html.13'
100%[==========================================================================================>] 41,881 217K/s in 0.2s
2012-05-07 09:17:32 (217 KB/s) - `index.html.13' saved [41881/41881]
-bash-3.2$ wget 'www.youku.com'
--2012-05-07 09:19:26-- http://www.youku.com/
正在解析主机 www.youku.com... 121.9.204.234
Connecting to www.youku.com|121.9.204.234|:80... 已连接。
已发出 HTTP 请求,正在等待回应... 200 OK
长度:390160 (381K) [text/html]
Saving to: `index.html.14'
100%[==========================================================================================>] 390,160 120K/s in 3.2s
2012-05-07 09:19:29 (120 KB/s) - `index.html.14' saved [390160/390160]
String ip = "1.1.1.1";//此处为dns解析出来的网站IP地址
Socket socket = new Socket();
socket.connect(new InetSocketAddress(ip, 80), 15000);
boolean autoflush = true;
PrintWriter out = new PrintWriter(socket.getOutputStream(), autoflush);
BufferedReader in = new BufferedReader(new InputStreamReader(socket
.getInputStream()));
// send an HTTP request to the web server
out.println("GET / HTTP/1.1");
out.println("Accept: */*");
out.println("User-Agent: cdut-boy");
out.println("Host: " + load_url);//此处为网站网址
out.println();
logger.info("获取HTTP页面首包时延发包结束,网站网址:load_url=" + load_url);
boolean loop = true;
while (loop)
{
if (in.ready())//此处 优酷 189 从来就读取不到首包,高手解答
{
//..........do something...under...
loop = false;
}
}
public class SiteSpeed {
public static void main(String[] args) throws Exception {
String url = "www.youku.com";
Socket socket = new Socket();
socket.connect(new InetSocketAddress(InetAddress.getByName(url), 80), 15000);
PrintWriter out = new PrintWriter(socket.getOutputStream());
InputStream in = socket.getInputStream();
// send an HTTP request to the web server
out.println("GET / HTTP/1.1");
out.println("Accept: */*");
out.println("User-Agent: cdut-boy");
out.println("Host: " + url);//此处为网站网址
out.println();
out.flush();
long timer = System.currentTimeMillis();
log("获取HTTP页面首包时延发包结束,网站网址:" + url);
in.read();
timer = System.currentTimeMillis() - timer;
log("获取HTTP页面首包时延:" + timer + "ms");
timer = System.currentTimeMillis();
while (in.read(new byte[8192]) > 0){}
timer = System.currentTimeMillis() - timer;
log("全部下载完毕:" + timer + "ms");
}
private static void log(Object msg) {
System.out.println(msg);
}
}
package com.tom.labs;
import java.net.*;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.io.*;
public class JavaHttp {
public static void main(String[] args) throws Exception {
File data = new File("D:\\in.txt");
File result = new File("D:\\out.txt");
FileOutputStream out = new FileOutputStream(result);
OutputStreamWriter writer = new OutputStreamWriter(out);
Reader reader = new InputStreamReader(new FileInputStream(data));
//postData(reader,new URL("http://google.com"),writer);//Not working
postData(reader,new URL("http://yahoo.com"),writer);//Not working
//sendGetRequest("http://google.com/search", "q=Hello");//Works properly
head("http://google.com");
}
public static String sendGetRequest(String endpoint,
String requestParameters) {
String result = null;
if (endpoint.startsWith("http://")) {
// Send a GET request to the servlet
try {
// Send data
String urlStr = endpoint;
if (requestParameters != null && requestParameters.length() > 0) {
urlStr += "?" + requestParameters;
}
URL url = new URL(urlStr);
URLConnection conn = url.openConnection();
// Get the response
BufferedReader rd = new BufferedReader(new InputStreamReader(
conn.getInputStream()));
StringBuffer sb = new StringBuffer();
String line;
while ((line = rd.readLine()) != null) {
sb.append(line);
}
rd.close();
result = sb.toString();
} catch (Exception e) {
e.printStackTrace();
}
}
System.out.println(result);
return result;
}
/**
* Reads data from the data reader and posts it to a server via POST
* request. data - The data you want to send endpoint - The server's address
* output - writes the server's response to output
*
* @throws Exception
*/
public static void postData(Reader data, URL endpoint, Writer output)
throws Exception {
HttpURLConnection urlc = null;
try {
urlc = (HttpURLConnection) endpoint.openConnection();
try {
urlc.setRequestMethod("POST");
} catch (ProtocolException e) {
throw new Exception(
"Shouldn't happen: HttpURLConnection doesn't support POST??",
e);
}
urlc.setDoOutput(true);
urlc.setDoInput(true);
urlc.setUseCaches(false);
urlc.setAllowUserInteraction(false);
urlc.setRequestProperty("Content-type", "text/xml; charset=UTF-8");
OutputStream out = urlc.getOutputStream();
try {
Writer writer = new OutputStreamWriter(out, "UTF-8");
pipe(data, writer);
writer.close();
} catch (IOException e) {
throw new Exception("IOException while posting data", e);
} finally {
if (out != null)
out.close();
}
InputStream in = urlc.getInputStream();
try {
Reader reader = new InputStreamReader(in);
pipe(reader, output);
reader.close();
} catch (IOException e) {
throw new Exception("IOException while reading response", e);
} finally {
if (in != null)
in.close();
}
} catch (IOException e) {
e.printStackTrace();
throw new Exception("Connection error (is server running at "
+ endpoint + " ?): " + e);
} finally {
if (urlc != null)
urlc.disconnect();
}
}
static void head(String endpoint){
URL url;
try {
url = new URL(endpoint);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
// conn.setDoOutput(true);
conn.setRequestMethod("HEAD");
Map<String, List<String>> headerMap = conn.getHeaderFields();
Iterator<String> iterator = headerMap.keySet().iterator();
while (iterator.hasNext()) {
String key = iterator.next();
List<String> values = headerMap.get(key);
System.out.println(key + ":" + values.toString());
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* Pipes everything from the reader to the writer via a buffer
*/
private static void pipe(Reader reader, Writer writer) throws IOException {
char[] buf = new char[1024];
int read = 0;
while ((read = reader.read(buf)) >= 0) {
writer.write(buf, 0, read);
}
writer.flush();
}
}