2015-10-22 192 views
0

我而已!開始學習相當多的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函數調用在主函數中,如果他們不前,主要申報?這難道不該理想扔的路線錯誤「功能沒有定義」

+0

*!「我剛開始學習Java」 *其次是*「我看着示例代碼製作HTTP在Java中獲取和發佈請求「*是完全矛盾的。首先實際學習Java,然後開始運行。難怪你不明白。 – Gimby

+0

@Gimby:我不同意。除非提問者對編程絕對陌生,否則不會產生矛盾。對於任何學生來說,HTTP get和post並不是最高級的主題。我在網頁中使用AJAX。我不會在VBA或PHP中編寫自己的類,但我確實瞭解OOP範例。例如,當在一個字符串上調用一個字符串函數的時候,它確實使我明白,字符串函數是字符串核心類的成員函數。這種理解對於我看起來不像綠色或拉丁語的示例代碼就足夠了,這兩者我都不明白。 –

回答

0
HttpURLConnectionExample http = new HttpURLConnectionExample(); 

你需要創建一個類的對象訪問其非靜態方法。所以要運行sendGet,sendPost你需要這個類的對象。

「?爲什麼sendGet和sendPost函數調用在主函數中,如果他們不前主宣稱這難道不該理想扔的線‘功能不定義的’錯誤」

- >在然而,在Java中調用方法之前需要聲明C方法。

0

1)這是一個鑄件。由於Java是類型安全的,因此有時需要明確地告訴編譯器您正在處理的對象類型。 https://howtoprogramwithjava.com/java-cast/

2)發生這種情況是因爲「main」方法是「靜態」。它不能訪問非靜態實例字段和方法。 所以慣用的伎倆是建立在main方法的對象,然後調用它的一些「運行」功能(在這種情況下,「sendGet」) http://docs.oracle.com/javase/tutorial/java/javaOO/classvars.html

3)未在Java中的問題。您在類中定義方法和變量的順序無關緊要。但是,順序在方法中很重要。

我可以推薦Java教程。這是一個非常良好的基礎,並已完成後,你就會明白的代碼片段的一切: http://docs.oracle.com/javase/tutorial/java/