【发布时间】:2015-10-22 08:46:50
【问题描述】:
我只是!在学习了相当多的 PHP 之后开始学习 Java(OOP 经验接近于零,但对 OOP 有基本的了解)。我正在查看用于在 Java 中进行 HTTP 获取和发布请求的示例代码,但无法理解某些行中的语法。
下面是我在http://www.mkyong.com/java/how-to-send-http-request-getpost-in-java/找到的示例代码
HttpURLConnectionExample.java
package com.mkyong;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import javax.net.ssl.HttpsURLConnection;
public class HttpURLConnectionExample {
private final String USER_AGENT = "Mozilla/5.0";
public static void main(String[] args) throws Exception {
HttpURLConnectionExample http = new HttpURLConnectionExample();
System.out.println("Testing 1 - Send Http GET request");
http.sendGet();
System.out.println("\nTesting 2 - Send Http POST request");
http.sendPost();
}
// HTTP GET request
private void sendGet() throws Exception {
String url = "http://www.google.com/search?q=mkyong";
URL obj = new URL(url);
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
// optional default is GET
con.setRequestMethod("GET");
//add request header
con.setRequestProperty("User-Agent", USER_AGENT);
int responseCode = con.getResponseCode();
System.out.println("\nSending 'GET' request to URL : " + url);
System.out.println("Response Code : " + responseCode);
BufferedReader in = new BufferedReader(
new InputStreamReader(con.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
//print result
System.out.println(response.toString());
}
// HTTP POST request
private void sendPost() throws Exception {
String url = "https://selfsolve.apple.com/wcResults.do";
URL obj = new URL(url);
HttpsURLConnection con = (HttpsURLConnection) obj.openConnection();
//add reuqest header
con.setRequestMethod("POST");
con.setRequestProperty("User-Agent", USER_AGENT);
con.setRequestProperty("Accept-Language", "en-US,en;q=0.5");
String urlParameters = "sn=C02G8416DRJM&cn=&locale=&caller=&num=12345";
// Send post request
con.setDoOutput(true);
DataOutputStream wr = new DataOutputStream(con.getOutputStream());
wr.writeBytes(urlParameters);
wr.flush();
wr.close();
int responseCode = con.getResponseCode();
System.out.println("\nSending 'POST' request to URL : " + url);
System.out.println("Post parameters : " + urlParameters);
System.out.println("Response Code : " + responseCode);
BufferedReader in = new BufferedReader(
new InputStreamReader(con.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
//print result
System.out.println(response.toString());
}
}
main函数下面的我看不懂
1)
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
在调用 obj 上的打开连接函数之前 (HttpURLConnection) 做了什么。我想不通这是什么意思。我应该在哪里阅读以进一步了解。
2)
HttpURLConnectionExample http = new HttpURLConnectionExample();
似乎创建了一个与在同一个类中的类的实例。为什么 ?这个概念叫什么?我应该阅读什么/在哪里理解这一点?
3) 如果 sendGet 和 sendPost 函数没有在 main 之前声明,为什么还要在 main 函数中调用它们?理想情况下不应该在“未定义函数”的行上引发错误
【问题讨论】:
-
“我刚刚!开始学习 Java”,接着是 “我正在查看用 Java 进行 HTTP 获取和发布请求的示例代码”是完全矛盾的。首先实际学习Java,然后开始运行。难怪你不明白。
-
@Gimby:我不敢苟同。除非提问者对编程完全陌生,否则不会产生矛盾。对于任何学生来说,HTTP get 和 post 并不是真正的最高级主题。我在网页中大量使用 AJAX。我不会用 VBA 或 PHP 编写自己的类,但我确实了解 OOP 范式。例如,在对字符串调用字符串函数时,字符串函数确实是字符串核心类的成员函数。这种理解足以让我发现的示例代码看起来不像绿色或拉丁文,这两个我都不理解。
-
@Gimby :我同意你的观点,你会希望人们阅读“完整参考”之类的关于 java 的书,逐页、从头到尾,然后编写他们的程序。好吧,这是专业程序员的理想期望,但我不是专业的。我也没有运行那个程序。只需阅读它,就可以对它有一定的了解。如果“每个人都必须学习编码”,即使是律师、银行家、外科医生或金融分析师之类的人,这也不是不公平的做法。
-
我不希望人们在开始跑步之前给自己时间学习如何走路。如果你不这样做,你就会冒着提出因多种原因而被关闭的问题的巨大风险。例如,一次性提出三个问题,其中每个问题都有很大的重复风险,这是获得封闭式问题的好方法。请注意,我无法说服您停止查看随机代码并开始研究高质量的信息源。