我而已!開始學習相當多的PHP後(接近於零OOP的經驗,但與OOP的基本理解不過)學習Java。我一直在尋找的示例代碼用於製作HTTP GET和Java的崗位要求和無法理解在某些行的語法。JAVA GET和POST請求
下面是示例代碼我上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());
}
}
我無法理解在主函數
1)
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
以下
什麼是(HttpURLConnection類)上obj中的開放式連接功能做之前被調用。我不知道它是什麼意思。我應該在哪裏閱讀以進一步瞭解。
2)
HttpURLConnectionExample http = new HttpURLConnectionExample();
似乎與在相同的類中創建的類的實例。爲什麼?這個概念叫什麼?我應該閱讀哪些內容才能理解這一點?
3)爲什麼sendGet和sendPost函數調用在主函數中,如果他們不前,主要申報?這難道不該理想扔的路線錯誤「功能沒有定義」
*!「我剛開始學習Java」 *其次是*「我看着示例代碼製作HTTP在Java中獲取和發佈請求「*是完全矛盾的。首先實際學習Java,然後開始運行。難怪你不明白。 – Gimby
@Gimby:我不同意。除非提問者對編程絕對陌生,否則不會產生矛盾。對於任何學生來說,HTTP get和post並不是最高級的主題。我在網頁中使用AJAX。我不會在VBA或PHP中編寫自己的類,但我確實瞭解OOP範例。例如,當在一個字符串上調用一個字符串函數的時候,它確實使我明白,字符串函數是字符串核心類的成員函數。這種理解對於我看起來不像綠色或拉丁語的示例代碼就足夠了,這兩者我都不明白。 –