我知道呀,但是取不成功?
URL urls = new URL("http://www.livescore.com/");
HttpURLConnection conn = (HttpURLConnection)urls.openConnection();
conn.setDoOutput(true);
OutputStreamWriter out=new OutputStreamWriter(conn.getOutputStream());
out.close();
BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String line;
while ((line = in.readLine()) != null) {
System.out.println(line);
}
in.close();上面是我的源代码,返回 Http Response 405 错误
URL urls = new URL("http://www.livescore.com/");
HttpURLConnection conn = (HttpURLConnection)urls.openConnection();
conn.setDoOutput(true);
OutputStreamWriter out=new OutputStreamWriter(conn.getOutputStream());
out.close();
BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String line;
while ((line = in.readLine()) != null) {
System.out.println(line);
}
in.close();上面是我的源代码,返回 Http Response 405 错误
The request contained bad syntax or cannot be fulfilled. 403 HTTP_FORBIDDEN
Request was understood, however access is forbidden. 说明网站拒绝这样子来访问。有何办法?
General
1xx HTTP_INFORMATIONAL
Request was received; processing can continue.
2xx HTTP_SUCCESSFUL
The request was successfully received, understood, and accepted.
3xx HTTP_REDIRECTION
Further action must be taken in order to complete the request.
4xx HTTP_CLIENT_ERROR
The request contained bad syntax or cannot be fulfilled.
5xx HTTP_SERVER_ERROR
The server failed to fulfill an apparently valid request.
Detail
100 HTTP_CONTINUE
Request was received and processing can continue.
101 HTTP_SWITCHING_PROTOCOLS
Request to switch protocols accepted.
200 HTTP_OK
Request completed successfully.
201 HTTP_CREATED
Request fulfilled and resulted in new resource being created.
202 HTTP_ACCEPTED
Request accepted, but processing has not completed.
203 HTTP_NON_AUTHORITATIVE_INFORMATION
Information in Header is not from original server.
204 HTTP_NO_CONTENT
Request received; no information exists.
205 HTTP_RESET_CONTENT
Request fulfilled; reset document view.
206 HTTP_PARTIAL_CONTENT
The server has fulfilled the partial GET request for the resource.
300 HTTP_MULTIPLE_CHOICES
Requested address refers to more than one entity.
301 HTTP_MOVED_PERMANENTLY
Page has been moved permanently; new URL available.
302 HTTP_FOUND
Page has been moved temporarily; new URL available.
303 HTTP_SEE_OTHER
Requested data is somewhere else; GET method should be used to retrieve it.
304 HTTP_NOT_MODIFIED
Requested resource has not been modified.
305 HTTP_USE_PROXY
Requested resource must be accessed through proxy given by Location field.
307 HTTP_TEMPORARY_REDIRECT
Requested data temporarily resides at a new location.
400 HTTP_BAD_REQUEST
Request could not be processed due to syntax error.
401 HTTP_UNAUTHORIZED
Client request did not include proper authentication data.
402 HTTP_PAYMENT_REQUIRED
Payment is required. (Not implemented at this time. Reserved for future use.)
403 HTTP_FORBIDDEN
Request was understood, however access is forbidden.
404 HTTP_NOT_FOUND
Requested document could not be found.
405 HTTP_METHOD_NOT_ALLOWED
The method being used to access the file is not allowed.
406 HTTP_NOT_ACCEPTABLE
Request document does not exist in a format the client can accept.
407 HTTP_PROXY_AUTHENTICATION_REQUIRED
Client must first authenticate with proxy for access.
408 HTTP_REQUEST_TIMEOUT
The client did not produce a request within the time that the server was prepared to wait.
409 HTTP_CONFLICT
Request could not be completed due to a conflict with the resource.
410 HTTP_GONE
Requested resource no longer exists and no forwarding address is known.
411 HTTP_LENGTH_REQUIRED
Request is missing required Content-Length header.
412 HTTP_PRECONDITION_FAILED
Request failed due to precondition not being met.
413 HTTP_REQUEST_ENTITY_TOO_LARGE
Request was too big to process.
414 HTTP_REQUEST_URI_TOO_LARGE
Request refused due to URL being longer than server is willing to process.
415 HTTP_UNSUPPORTED_MEDIA_TYPE
Requested resource format is not supported.
416 HTTP_REQUESTED_RANGE_NOT_SATISFIABLE
Request includes a Range header and none of the ranges specified are contained within the requested resource.
417 HTTP_EXPECTATION_FAILED
The server is unable to meet the demands of the Expect header given by the client.
500 HTTP_INTERNAL_SERVER_ERROR
The server encountered an unexpected condition which prevented it from fulfilling the request.
501 HTTP_NOT_IMPLEMENTED
The server does not support the functionality required to fulfill the request.
502 HTTP_BAD_GATEWAY
The server, while acting as a gateway or proxy, received an invalid response from an upstream provider.
503 HTTP_SERVICE_UNAVAILABLE
The server is currently unable to handle the request due to a temporary overloading or maintenance of the server.
504 HTTP_GATEWAY_TIMEOUT
The server, while acting as a gateway or proxy, did not receive a timely response from an upstream provider.
505 HTTP_VERSION_NOT_SUPPORTED
The server does not support, or refuses to support, the HTTP protocol version that was used in the request.
是啊,这是为什么?
HttpURLConnection uc = (HttpURLConnection) u.openConnection();
int code = uc.getResponseCode();
String response = uc.getResponseMessage();
System.out.println("HTTP/1.x " + code + " " + response);
for (int j = 1; ; j++)
{
String header = uc.getHeaderField(j);
String key = uc.getHeaderFieldKey(j);
if (header == null || key == null) break;
System.out.println(uc.getHeaderFieldKey(j) + ": " + header);
}
InputStream in = new BufferedInputStream(uc.getInputStream());
// chain the InputStream to a Reader
Reader r = new InputStreamReader(in);
int c;
while ((c = r.read()) != -1)
{
System.out.print((char) c);
}
输出类似如下结果:
---------------------------------------------
HTTP/1.x 200 OK
Server: Microsoft-IIS/5.0
Date: Sun, 05 Oct 2003 05:10:26 GMT
Set-Cookie: SID=0310Lm; path=/; expires=Fri, 05 Oct 2007 07:10:26 GMT;
Expires: Thu, 14 Oct 1965 00:00:00 GMT
Last-Modified: Mon, 06 Oct 2003 07:10:26 GMT
P3P: CP="NOI DSP COR NID NOR"
Cache-Control: no-cache, must-revalidate
Pragma: no-cache
Content-Type: text/html
Content-Length: 351
Content:
X-Cache: MISS from xxx.xxx.xxx
Proxy-Connection: keep-alive
<html><head><meta NAME="robots" CONTENT="noindex,noarchive"><meta HTTP-EQUIV="refresh" content="0;URL=/default.dll/stop?page=home&sid=0310Lm"></head><body bgcolor="#000000" onLoad="JavaScript:document.check.submit();"><form name="check" method="post" action="/default.dll?page=home"><input type="hidden" name="sid" value="0310Lm"></form></body></html>分析其中的html,然后改用以下代码连续发出两次请求,第二次请求的url加上刚刚获得的sid,结果成功了(调试了很久:()// ReadLiveScore.java
import java.net.*;
import java.io.*;public class ReadLiveScore
{
public static void main(String[] args) throws Exception
{ System.getProperties().setProperty("http.proxyHost", "yourHost");
System.getProperties().setProperty("http.proxyPort", "yourPort");
URL url1 = new URL("http://www.livescore.com/default.dll?page=home");
HttpURLConnection urlConnection = (HttpURLConnection) url1.openConnection();
String cookie = urlConnection.getHeaderField("Set-Cookie");
String query = cookie.substring(0, cookie.indexOf(";")).trim(); URL url2 = new URL("http://www.livescore.com/default.dll?page=home&" + query);
HttpURLConnection con = (HttpURLConnection) url2.openConnection();
BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
String inputLine;
while ((inputLine = in.readLine()) != null)
System.out.println(inputLine);
in.close();
}
}