创建http对象
Posted 和xin
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了创建http对象相关的知识,希望对你有一定的参考价值。
package test;
import java.net.HttpURLConnection;
import java.net.URL;
import javax.servlet.http.HttpServlet;
public class MainTest extends HttpServlet{
private static final long serialVersionUID = 1L;
public static void main(String[] args) {
//HttpServletRequest request = ServletActionContext.getRequest();
String locationUrl = "http://www.baidu.com";
URL url;
try {
url = new URL(locationUrl);
HttpURLConnection connection;
connection = (HttpURLConnection) url.openConnection();
connection.setDoInput(true);
//超时设置
connection.setReadTimeout(10000);
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type", "text/html");
connection.setRequestProperty("Accept-Charset", "utf-8");
connection.setRequestProperty("contentType", "utf-8");
} catch (Exception e) {
e.printStackTrace();
}
}
}
以上是关于创建http对象的主要内容,如果未能解决你的问题,请参考以下文章