java HTTP基本框架

Posted 耀yao

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java HTTP基本框架相关的知识,希望对你有一定的参考价值。


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.InetSocketAddress;
import java.net.Proxy;
import java.net.URL;

public class LoginUtil {

public static String Get(String url,String key) {
{
String proxyHost="";
int proxyPort=111;
boolean isproxy=false;
String result = "";
BufferedReader in = null;
try {
URL realUrl = new URL(url);
HttpURLConnection conn = null;
if (isproxy) {
//使用代理IP
@SuppressWarnings("static-access")
Proxy proxy = new Proxy(Proxy.Type.DIRECT.HTTP, new InetSocketAddress(proxyHost, proxyPort));
conn = (HttpURLConnection) realUrl.openConnection(proxy); //使用代理IP,打开和URL之间的连接
}else {
//不使用代理IP
conn = (HttpURLConnection) realUrl.openConnection(); //不使用代理IP,打开和URL之间的连接
}
// 发送POST请求必须设置如下两行
conn.setDoOutput(true);
conn.setDoInput(true);
// 设置通用的请求属性
conn.setConnectTimeout(2 * 1000);
conn.setRequestProperty("accept", "*/*");
conn.setRequestProperty("connection", "Keep-Alive");
conn.setRequestProperty("user-agent","Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
//conn.setRequestProperty("Referer", referer);
conn.setRequestProperty("key", key);
// 建立真实HTTP连接
conn.connect();
// 定义BufferedReader输入流来读取URL的响应
in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "utf-8"));
String line;
while ((line = in.readLine()) != null) {
result = result + line + "\r\n";
}
} catch (Exception e) {
System.out.println("发送 GET 请求出现异常!" + e);
e.printStackTrace();
} finally {
try{
if(in!=null){
in.close();
}
}
catch(IOException ex){
ex.printStackTrace();
}
}
return result;
}
}
public static String Sendgift(String url, String param,String key) {
String result = "";
int proxyPort=111;
String proxyHost="1.1.1.1";
boolean isproxy=false;
OutputStreamWriter out = null;
BufferedReader in = null;
try {
URL realUrl = new URL(url);
HttpURLConnection conn = null;
if (isproxy) {
//使用代理IP
@SuppressWarnings("static-access")
Proxy proxy = new Proxy(Proxy.Type.DIRECT.HTTP, new InetSocketAddress(proxyHost, proxyPort));
conn = (HttpURLConnection) realUrl.openConnection(proxy); //使用代理IP,打开和URL之间的连接
}else {
//不使用代理IP
conn = (HttpURLConnection) realUrl.openConnection(); //不使用代理IP,打开和URL之间的连接
}
// 发送POST请求必须设置如下两行
conn.setDoOutput(true);
conn.setDoInput(true);
// POST方法
conn.setRequestMethod("POST");
// 设置通用的请求属性
conn.setConnectTimeout(5 * 1000);
conn.setRequestProperty("Content-Type", "application/json");
conn.setRequestProperty("User-Agent","Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (Khtml, like Gecko) Chrome/39.0.2171.71 Safari/537.36");
conn.setRequestProperty("Host", "www.sjlive.cn");
conn.setRequestProperty("key", key);
// 建立真实HTTP连接
conn.connect();
// 获取URLConnection对象对应的输出流
out = new OutputStreamWriter(conn.getOutputStream(), "UTF-8");
// 发送请求参数
out.write(param);
// flush输出流的缓冲
out.flush();
// 定义BufferedReader输入流来读取URL的响应
in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "utf-8"));
String line;
while ((line = in.readLine()) != null) {
result = result + line + "\r\n";
}
} catch (Exception e) {
System.out.println("发送 POST 请求出现异常!" + e);
e.printStackTrace();
} finally {
try{
if(out != null){
out.close();
}
if(in != null){
in.close();
}
}
catch(IOException ex){
ex.printStackTrace();
}
}
return result;
}
}

以上是关于java HTTP基本框架的主要内容,如果未能解决你的问题,请参考以下文章

java集合框架

eclipse怎么搭建ssh框架

Play 框架 2.4 的 HTTP 基本身份验证

HTTP基本身份验证背后的Django Rest框架

java.lang.IllegalArgumentException: SessionContext must be an HTTP compatible implementation.:模块化本地测

Golang web 框架对比