我而已!开始学习相当多的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范例。例如,当在一个字符串上调用一个字符串函数的时候,它确实使我明白,字符串函数是字符串核心类的成员函数。这种理解对于我看起来不像绿色或拉丁语的示例代码就足够了,这两者我都不明白。 –