本文目錄一覽:
- 1、如何使用HttpClient包實現JAVA發起HTTP請求
- 2、怎樣用JAVA實現模擬HTTP請求,得到伺服器的響應時間等參數
- 3、怎樣編寫http請求的java程序
- 4、怎麼用java模擬http請求
如何使用HttpClient包實現JAVA發起HTTP請求
public class HttpClientUtil {
public static String doGet(String url, MapString, String param) {
// 創建Httpclient對象
CloseableHttpClient httpclient = HttpClients.createDefault();
String resultString = “”;
CloseableHttpResponse response = null;
try {
// 創建uri
URIBuilder builder = new URIBuilder(url);
if (param != null) {
for (String key : param.keySet()) {
builder.addParameter(key, param.get(key));
}
}
URI uri = builder.build();
// 創建http GET請求
HttpGet httpGet = new HttpGet(uri);
// 執行請求
response = httpclient.execute(httpGet);
// 判斷返回狀態是否為200
if (response.getStatusLine().getStatusCode() == 200) {
resultString = EntityUtils.toString(response.getEntity(), “UTF-8”);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (response != null) {
response.close();
}
httpclient.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return resultString;
}
public static String doGet(String url) {
return doGet(url, null);
}
public static String doPost(String url, MapString, String param) {
// 創建Httpclient對象
CloseableHttpClient httpClient = HttpClients.createDefault();
CloseableHttpResponse response = null;
String resultString = “”;
try {
// 創建Http Post請求
HttpPost httpPost = new HttpPost(url);
// 創建參數列表
if (param != null) {
ListNameValuePair paramList = new ArrayList();
for (String key : param.keySet()) {
paramList.add(new BasicNameValuePair(key, param.get(key)));
}
// 模擬表單
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(paramList);
httpPost.setEntity(entity);
}
// 執行http請求
response = httpClient.execute(httpPost);
System.out.println(response.getStatusLine());
resultString = EntityUtils.toString(response.getEntity(), “utf-8”);
System.out.println(resultString);
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
response.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return resultString;
}
public static String doPost(String url) {
return doPost(url, null);
}
public static String doPostJson(String url, String json) {
// 創建Httpclient對象
CloseableHttpClient httpClient = HttpClients.createDefault();
CloseableHttpResponse response = null;
String resultString = “”;
try {
// 創建Http Post請求
HttpPost httpPost = new HttpPost(url);
// 創建請求內容
StringEntity entity = new StringEntity(json, ContentType.APPLICATION_JSON);
httpPost.setEntity(entity);
// 執行http請求
response = httpClient.execute(httpPost);
resultString = EntityUtils.toString(response.getEntity(), “utf-8”);
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
response.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return resultString;
}
}
怎樣用JAVA實現模擬HTTP請求,得到伺服器的響應時間等參數
java.net.*;
public class HttpDemo{
public static void main(String[] args)throws Exception{
URL url = new URL(‘地址’);
HttpURLConnection http = (HttpURLConnection)url.openConnection();
//獲取網頁的源碼
BufferedReader br = new BufferedReader(new InputStreamReader(http.getInputStream()));
String line = “”;
while((line=br.readLine())!=null){
System.out.println(line);
}
br.close();
//獲取參數:
String value = getRequestProperty(String key);
}
}
怎樣編寫http請求的java程序
目前web上的消息通訊方式主要有以下幾種。輪詢,長連接,websocket輪詢:隔一段時間訪問伺服器,伺服器不管有沒有新消息都立刻返回。長連接:頁面向伺服器發出請求,由伺服器決定什麼時候返回。(如果有新消息則立刻返回,沒有的話就保持連接,直到有新消息才返回)websocket:類似JavaSocket,由Http請求模擬實現的socket。要實現長連接的關鍵就是:由伺服器端決定什麼時候返回數據。比如在servlet中。doGet(){Thread.sleep(30000);return}這就是一個長連接的例子,只是沒有任何意義而已。你要做的就是在doGet中阻塞住,while(!hasNewMsg){sleep(500)}returnnewMsg當然你的ajax超時時間要設置長一點。如果可以的話,最好可以使用websocket。
怎麼用java模擬http請求
/*
* 得到返回的內容
*/
public static String getResult(String urlStr, String content) {
URL url = null;
HttpURLConnection connection = null;
try {
url = new URL(urlStr);
connection = (HttpURLConnection) url.openConnection();
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setRequestMethod(“POST”);
connection.setUseCaches(false);
connection.connect();
DataOutputStream out = new DataOutputStream(connection.getOutputStream());
out.writeBytes(content);
out.flush();
out.close();
BufferedReader reader = new BufferedReader(new InputStreamReader(connection
.getInputStream(), “utf-8”));
StringBuffer buffer = new StringBuffer();
String line = “”;
while ((line = reader.readLine()) != null) {
buffer.append(line);
}
reader.close();
return buffer.toString();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (connection != null) {
connection.disconnect();
}
}
return null;
}
追問:
沒注釋嗎?
追答:
/*
* 得到返回的內容
*/
public static String getResult(String urlStr, String content) {
URL url = null;
HttpURLConnection connection = null;
try {
url = new URL(urlStr);
connection = (HttpURLConnection) url.openConnection();//新建連接實例
connection.setDoOutput(true);//是否打開輸出流 true|false
connection.setDoInput(true);//是否打開輸入流true|false
connection.setRequestMethod(“POST”);//提交方法POST|GET
connection.setUseCaches(false);//是否緩存true|false
connection.connect();//打開連接埠
DataOutputStream out = new DataOutputStream(connection.getOutputStream());//打開輸出流往對端伺服器寫數據
out.writeBytes(content);//寫數據,也就是提交你的表單 name=xxxpwd=xxx
out.flush();//刷新
out.close();//關閉輸出流
BufferedReader reader = new BufferedReader(new InputStreamReader(connection
.getInputStream(), “utf-8”));//往對端寫完數據 對端伺服器返回數據 ,以BufferedReader流來讀取
StringBuffer buffer = new StringBuffer();
String line = “”;
while ((line = reader.readLine()) != null) {
buffer.append(line);
}
reader.close();
return buffer.toString();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (connection != null) {
connection.disconnect();//關閉連接
}
}
return null;
}
原創文章,作者:AOADG,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/325088.html