JAVA获取和发布请求

时间:2015-10-22 08:46:51

标签: java oop http

我只是!在学习了相当多的PHP之后开始学习Java(虽然接近零OOP经验但是对OOP有基本的了解)。我正在查看使用Java进行HTTP 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();

在调用obj上的open连接函数之前,(HttpURLConnection)做了什么。我无法弄清楚它意味着什么。我应该在哪里阅读以进一步了解。

2)

    HttpURLConnectionExample http = new HttpURLConnectionExample();

似乎在同一个类中创建一个类的实例。为什么?这个概念叫什么?我应该阅读什么/在哪里理解这一点?

3)如果在main函数之前未声明sendGet和sendPost函数,为什么在main函数中调用它们?理想情况下,不应该在'function not defined'

的行上抛出错误

2 个答案:

答案 0 :(得分:0)

HttpURLConnectionExample http = new HttpURLConnectionExample();

您需要创建一个类的对象来访问其非静态方法。所以要运行sendGet,sendPost你需要该类的对象。

“为什么sendMet和sendPost函数在main函数中调用,如果它们没有在main之前声明?不应该理想地在'函数未定义'的行上抛出错误”

- >在C方法中,需要在java中调用method on之前声明。

答案 1 :(得分:0)

1)这是一个演员。由于Java是类型安全的,因此有时需要明确告诉编译器您正在处理的对象类型。 https://howtoprogramwithjava.com/java-cast/

2)这是因为"主要"方法是"静态"。它无法访问非静态实例字段和方法。 所以通常的技巧是在main方法中构造对象并调用一些" run"它上面的功能(在这种情况下" sendGet") http://docs.oracle.com/javase/tutorial/java/javaOO/classvars.html

3)这不是Java中的问题。在类中定义方法和变量的顺序并不重要。但是,顺序在方法中很重要。

我可以建议Java教程。它是一个非常好的基础,在您完成它之后,您将了解代码片段中的所有内容: http://docs.oracle.com/javase/tutorial/java/