代码:
package testRedBag;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
public class HttpURLConnectionTest {
public static final String GET_URL = “http://www.baidu.com”;
public static final String POST_URL = “http://www.baidu.com”;
/**
* 接口调用 GET
*/
public static void httpURLConectionGET() {
try {
URL url = new URL(GET_URL); // 把字符串转换为URL请求地址
HttpURLConnection connection = (HttpURLConnection) url.openConnection();// 打开连接
connection.connect();// 连接会话
connection.setRequestProperty(“Content-Type”, “text/html; charset=UTF-8″);
// 获取输入流
BufferedReader br = new BufferedReader(new InputStreamReader(connection
.getInputStream(),”UTF-8”));
String line;
StringBuilder sb = new StringBuilder();
while ((line = br.readLine()) != null) {// 循环读取流
sb.append(line);
}
br.close();// 关闭流
connection.disconnect();// 断开连接
System.out.println(sb.toString());
} catch (Exception e) {
e.printStackTrace();
System.out.println(“失败!”);
}
}
/**
* 接口调用 POST
*/
public static void httpURLConnectionPOST () {
try {
URL url = new URL(POST_URL);
// 将url 以 open方法返回的urlConnection 连接强转为HttpURLConnection连接 (标识一个url所引用:
在家可以访问,在公司不可以访问。公司封了很多网站,但百度还可以上,这样的话,myeclipse可以访问的到吗
封网就没啥办法了。
20
Authenticator.setDefault(new Authenticator() { protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication("用户名", "密码".toCharArray()); } }); // https System.setProperty("https.proxySet", "true"); System.getProperties().put("https.proxyHost", "代理地址"); System.getProperties().put("https.proxyPort", "代理端口"); // http System.setProperty("http.proxySet", "true"); System.getProperties().put("http.proxyHost", "代理地址"); System.getProperties().put("http.proxyPort", "代理端口");
来设置代理, https选用上面https的设置.
20
本人不在上海,但是公司上海有办公场所,并且也是张江。