1

我是 Java 的新手。我刚刚下载了 Eclipse 和最新的 JDK。

现在我想在专门为这个 http 请求构建的 Java 应用程序中执行基于 REST 的 HTTP 请求,几乎就像一个“hello world”类型的应用程序。我遇到了以下有关如何执行此操作的教程,但我不确定如何在 Eclipse 中将其实际实现为项目和工作 Java 应用程序。

http://www.mkyong.com/java/how-to-send-http-request-getpost-in-java/

http://www.xyzws.com/Javafaq/how-to-use-httpurlconnection-post-data-to-web-server/139


现在,我所做的如下:

我从第二个链接示例中获取代码,并将其保存到一个名为:

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 request 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());

    }

}

我将它导入 Eclipse:File>Open File。所以现在我右键单击右侧“大纲”窗口中的类(HttpURLConnectionExample.java),并在“运行配置”下为它指定了一个“项目”,称为http_request

在此处输入图像描述

现在,当我右键单击“http_requests”上的左侧窗口时,我会:运行为>java 应用程序。它给了我以下错误:

Launch Error: Selection does not contain a main type

我的问题:我需要为它指定什么主要类型?如上面发布的代码所示,我如何实际执行 http 请求?我该如何运行?

4

0 回答 0