導航:首頁 > 編程語言 > java獲取http請求

java獲取http請求

發布時間:2023-03-05 06:15:12

java 怎麼接收http請求

HttpURLConnection mHttpURLConnection = null;
URL mUrl = null;
InputStream inputStream = null;

try {
String url = UrlEncode(mFileType.getStrUrl(), "UTF-8");

//mUrl = new URL(mFileType.getStrUrl());
mUrl = new URL(url);
mHttpURLConnection = (HttpURLConnection) mUrl.openConnection();
mHttpURLConnection.setAllowUserInteraction(true);
mHttpURLConnection.setRequestMethod("GET");
mHttpURLConnection.setRequestProperty("Range", "bytes=" + startPos
+ "-" + endPos);
mHttpURLConnection.setRequestProperty("Connection", "Keep-Alive");
int responseCode = mHttpURLConnection.getResponseCode();
long length = mHttpURLConnection.getContentLength();
// 判斷請求是否成功處理
if (responseCode == HttpStatus.SC_OK||responseCode == HttpStatus.SC_PARTIAL_CONTENT) {
inputStream = mHttpURLConnection.getInputStream();
}catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

② 如何通過抓包工具fiddler獲取java程序的http請求

抓包工具fidder是一個很輕巧的可以獲取瀏覽器,程序的http,https請求的軟體。
網路地址:http://ke..com/view/868685.htm
官網地址:http://fiddler2.com/
firefox的fidder插件

而Java程序需要設置proxy才能生效:

[java] view plain
Proxy proxy = new Proxy(java.net.Proxy.Type.HTTP,new InetSocketAddress("127.0.0.1", 8888));
URL serverUrl = new URL(url);
HttpURLConnection conn = (HttpURLConnection) serverUrl.openConnection(proxy);
詳細代碼請下載。
[java] view plain
package com.zuidaima.http;

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

/**
*@author www.zuidaima.com
**/
public class Http302Redirect {

public static void main(String[] args) {
try {
String url = "http://www.zuidaima.com/";
System.out.println("訪問地址:" + url);

Proxy proxy = new Proxy(java.net.Proxy.Type.HTTP,
new InetSocketAddress("127.0.0.1", 8888));
URL serverUrl = new URL(url);
HttpURLConnection conn = (HttpURLConnection) serverUrl
.openConnection(proxy);
conn.setRequestMethod("GET");

conn.addRequestProperty("Accept-Charset", "UTF-8;");
conn.addRequestProperty("User-Agent",
"Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9.2.8) Firefox/3.6.8");
conn.addRequestProperty("Referer", "http://javaniu.com/");
conn.connect();
BufferedReader reader = new BufferedReader(new InputStreamReader(
conn.getInputStream()));
StringBuffer buffer = new StringBuffer();
String line = reader.readLine();
while (line != null) {
buffer.append(line).append("\r\n");
line = reader.readLine();
}
reader.close();
conn.disconnect();
System.out.println(buffer);
} catch (Exception e) {
e.printStackTrace();
}
}

}

③ java怎樣獲取http請求的body

讀取Body使用request.getReader(),但getReader獲取的是,需要把它轉換成字元串,下面是轉換的方法
public class TestController {
@RequestMapping("/a")
protected void doPost(HttpServletRequest request,
HttpServletResponse response, BufferedReader br)
throws ServletException, IOException {
//Header部分
System.out.print(request.getHeaderNames());
Enumeration<?> enum1 = request.getHeaderNames();
while (enum1.hasMoreElements()) {
String key = (String) enum1.nextElement();
String value = request.getHeader(key);
System.out.println(key + "\t" + value);
}
//body部分
String inputLine;
String str = "";
try {
while ((inputLine = br.readLine()) != null) {
str += inputLine;
}
br.close();
} catch (IOException e) {
System.out.println("IOException: " + e);
}
System.out.println("str:" + str);
}

④ java 接受http請求

使用servlet


public class Test extends HttpServlet {

private static final long serialVersionUID = 1L;

/**

* @see HttpServlet#HttpServlet()

*/

public Test() {

super();

// TODO Auto-generated constructor stub

}


/**

* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)

*/

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {//接收get請求

// 這里寫你接收request請求後要處理的操作

}


/**

* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)

*/

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {//接收post請求

// 這里寫你接收request請求後要處理的操作

}


}


⑤ java http請求

可能是這個網站做了許可權校驗啥的吧,你在瀏覽器是先登錄以後再請求的這個介面,但是你的JAVA客戶端沒有做登錄,直接請求,被認為是非法請求,所以就不給你返回數據了

⑥ 如何通過抓包工具fiddler獲取java程序的http請求

fiddler實際上是通過代理的方式來工作的。默認埠是8888,所以用來請求http的客戶端,需要設定代理,就能被截獲並記錄。這種方式必須依賴fiddler進程正常工作。

以下是java設置代理的一種方式。具體要看使用什麼客戶端程序了。不同的客戶端程序設置代理的方式肯定不一樣。

Proxyproxy=newProxy(java.net.Proxy.Type.HTTP,newInetSocketAddress("127.0.0.1",8888));
URLserverUrl=newURL(url);
HttpURLConnectionconn=(HttpURLConnection)serverUrl.openConnection(proxy);

樣例:

packagecom.http;

importjava.io.BufferedReader;
importjava.io.InputStreamReader;
importjava.net.HttpURLConnection;
importjava.net.InetSocketAddress;
importjava.net.Proxy;
importjava.net.URL;

publicclassHttp302Redirect{

publicstaticvoidmain(String[]args){
try{
Stringurl="http://weibo.com/1253711052";
System.out.println("訪問地址:"+url);

Proxyproxy=newProxy(java.net.Proxy.Type.HTTP,
newInetSocketAddress("127.0.0.1",8888));
URLserverUrl=newURL(url);
HttpURLConnectionconn=(HttpURLConnection)serverUrl
.openConnection(proxy);
conn.setRequestMethod("GET");

conn.addRequestProperty("Accept-Charset","UTF-8;");
conn.addRequestProperty("User-Agent",
"Mozilla/5.0(Windows;U;WindowsNT5.1;zh-CN;rv:1.9.2.8)Firefox/3.6.8");
conn.addRequestProperty("Referer","http://weibo.com");
conn.connect();
BufferedReaderreader=newBufferedReader(newInputStreamReader(
conn.getInputStream()));
StringBufferbuffer=newStringBuffer();
Stringline=reader.readLine();
while(line!=null){
buffer.append(line).append(" ");
line=reader.readLine();
}
reader.close();
conn.disconnect();
System.out.println(buffer);
}catch(Exceptione){
e.printStackTrace();
}
}

}

⑦ java中獲取http請求中參數的值

public void getShopShow(HttpServletRequest request){
// 源 獲取一個mapper的集合
Map<String, String[]> parameterMap = request.getParameterMap();
// 獲取單個的數據
String name = request.getParameter("name");

}

閱讀全文

與java獲取http請求相關的資料

熱點內容
win10創建讀取文件 瀏覽:211
win10激活文件夾可以刪除嗎 瀏覽:719
佳能5D4照片文件名 瀏覽:953
冷門旅遊景點app哪個好 瀏覽:606
如何用qq郵箱發送文件 瀏覽:792
柚子音樂下載的音樂找不到文件夾 瀏覽:179
linuxarm配置文件 瀏覽:423
c盤oem文件夾 瀏覽:776
一人我飲酒醉多版本的 瀏覽:845
海克斯康三次元如何編程循環測量 瀏覽:518
app無法下載是為什麼蘋果手機 瀏覽:518
右擊此電腦管理找不到此文件 瀏覽:264
圖像文件格式百科 瀏覽:523
沒有網路怎麼定位 瀏覽:972
剪切到優盤文件找不到了 瀏覽:764
電腦管家的文件名 瀏覽:910
如何將掃描文件轉換為電子版 瀏覽:4
u盤的h246文件怎麼播放 瀏覽:203
如何用阿里雲伺服器學習資料庫 瀏覽:84
華為文件管理找不到下載文件 瀏覽:409

友情鏈接