簡略引見Java收集編程中的HTTP要求。本站提示廣大學習愛好者:(簡略引見Java收集編程中的HTTP要求)文章只能為提供參考,不一定能成為您想要的結果。以下是簡略引見Java收集編程中的HTTP要求正文
HTTP要求的細節——要求行
要求行中的GET稱之為要求方法,要求方法有:POST、GET、HEAD、OPTIONS、DELETE、TRACE、PUT,經常使用的有: GET、 POST
用戶假如沒有設置,默許情形下閱讀器向辦事器發送的都是get要求,例如在閱讀器直接輸地址拜訪,點超鏈接拜訪等都是get,用戶如想把要求方法改成post,可經由過程更改表單的提交方法完成。
不論POST或GET,都用於向辦事器要求某個WEB資本,這兩種方法的差別重要表示在數據傳遞上:假如要求方法為GET方法,則可以在要求的URL地址後以?的情勢帶上交給辦事器的數據,多個數據之間以&停止分隔,例如:GET /mail/1.html?name=abc&password=xyz HTTP/1.1
GET方法的特色:在URL地址後附帶的參數是無限制的,其數據容量平日不克不及跨越1K。
假如要求方法為POST方法,則可以在要求的實體內容中向辦事器發送數據,Post方法的特色:傳送的數據量無窮制。
HTTP要求的細節——新聞頭
HTTP要求中的經常使用新聞頭
accept:閱讀器經由過程這個頭告知辦事器,它所支撐的數據類型
Accept-Charset: 閱讀器經由過程這個頭告知辦事器,它支撐哪一種字符集
Accept-Encoding:閱讀器經由過程這個頭告知辦事器,支撐的緊縮格局
Accept-Language:閱讀器經由過程這個頭告知辦事器,它的說話情況
Host:閱讀器經由過程這個頭告知辦事器,想拜訪哪台主機
If-Modified-Since: 閱讀器經由過程這個頭告知辦事器,緩存數據的時光
Referer:閱讀器經由過程這個頭告知辦事器,客戶機是哪一個頁面來的 防盜鏈
Connection:閱讀器經由過程這個頭告知辦事器,要求完後是斷開鏈接照樣何持鏈接
例:
http_get
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
public class Http_Get {
private static String URL_PATH = "http://192.168.1.125:8080/myhttp/pro1.png";
public Http_Get() {
// TODO Auto-generated constructor stub
}
public static void saveImageToDisk() {
InputStream inputStream = getInputStream();
byte[] data = new byte[1024];
int len = 0;
FileOutputStream fileOutputStream = null;
try {
fileOutputStream = new FileOutputStream("C:\\test.png");
while ((len = inputStream.read(data)) != -1) {
fileOutputStream.write(data, 0, len);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
if (inputStream != null) {
try {
inputStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (fileOutputStream != null) {
try {
fileOutputStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
/**
* 取得辦事器真個數據,以InputStream情勢前往
* @return
*/
public static InputStream getInputStream() {
InputStream inputStream = null;
HttpURLConnection httpURLConnection = null;
try {
URL url = new URL(URL_PATH);
if (url != null) {
httpURLConnection = (HttpURLConnection) url.openConnection();
// 設置銜接收集的超不時間
httpURLConnection.setConnectTimeout(3000);
httpURLConnection.setDoInput(true);
// 表現設置本次http要求應用GET方法要求
httpURLConnection.setRequestMethod("GET");
int responseCode = httpURLConnection.getResponseCode();
if (responseCode == 200) {
// 從辦事器取得一個輸出流
inputStream = httpURLConnection.getInputStream();
}
}
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return inputStream;
}
public static void main(String[] args) {
// 從辦事器取得圖片保留到當地
saveImageToDisk();
}
}
Http_Post
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;
public class Http_Post {
// 要求辦事器真個url
private static String PATH = "http://192.168.1.125:8080/myhttp/servlet/LoginAction";
private static URL url;
public Http_Post() {
// TODO Auto-generated constructor stub
}
static {
try {
url = new URL(PATH);
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* @param params
* 填寫的url的參數
* @param encode
* 字節編碼
* @return
*/
public static String sendPostMessage(Map<String, String> params,
String encode) {
// 作為StringBuffer初始化的字符串
StringBuffer buffer = new StringBuffer();
try {
if (params != null && !params.isEmpty()) {
for (Map.Entry<String, String> entry : params.entrySet()) {
// 完成轉碼操作
buffer.append(entry.getKey()).append("=").append(
URLEncoder.encode(entry.getValue(), encode))
.append("&");
}
buffer.deleteCharAt(buffer.length() - 1);
}
// System.out.println(buffer.toString());
// 刪除失落最有一個&
System.out.println("-->>"+buffer.toString());
HttpURLConnection urlConnection = (HttpURLConnection) url
.openConnection();
urlConnection.setConnectTimeout(3000);
urlConnection.setRequestMethod("POST");
urlConnection.setDoInput(true);// 表現從辦事器獲得數據
urlConnection.setDoOutput(true);// 表現向辦事器寫數據
// 取得上傳信息的字節年夜小和長度
byte[] mydata = buffer.toString().getBytes();
// 表現設置要求體的類型是文本類型
urlConnection.setRequestProperty("Content-Type",
"application/x-www-form-urlencoded");
urlConnection.setRequestProperty("Content-Length",
String.valueOf(mydata.length));
// 取得輸入流,向辦事器輸入數據
OutputStream outputStream = urlConnection.getOutputStream();
outputStream.write(mydata,0,mydata.length);
outputStream.close();
// 取得辦事器呼應的成果和狀況碼
int responseCode = urlConnection.getResponseCode();
if (responseCode == 200) {
return changeInputStream(urlConnection.getInputStream(), encode);
}
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return "";
}
/**
* 將一個輸出流轉換成指定編碼的字符串
*
* @param inputStream
* @param encode
* @return
*/
private static String changeInputStream(InputStream inputStream,
String encode) {
// TODO Auto-generated method stub
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
byte[] data = new byte[1024];
int len = 0;
String result = "";
if (inputStream != null) {
try {
while ((len = inputStream.read(data)) != -1) {
outputStream.write(data, 0, len);
}
result = new String(outputStream.toByteArray(), encode);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return result;
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Map<String, String> params = new HashMap<String, String>();
params.put("username", "admin");
params.put("password", "123");
String result = Http_Post.sendPostMessage(params, "utf-8");
System.out.println("--result->>" + result);
}
}