2015-10-22 193 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/