導航:首頁 > 編程語言 > 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請求相關的資料

熱點內容
最新版本手機qq 瀏覽:463
簡述在word 瀏覽:528
qq怎麼清楚歷史記錄防止被盜 瀏覽:263
發送手機里的錄音文件在哪裡 瀏覽:866
js獲取下一個兄弟元素 瀏覽:293
js模板引擎原理 瀏覽:72
linuxo文件運行 瀏覽:713
什麼免費備份數據 瀏覽:342
測量大師導入底圖找不到文件 瀏覽:313
小紅傘安裝程序要求版本6 瀏覽:799
全民k歌pcm文件夾 瀏覽:224
c圖片寫入資料庫 瀏覽:466
c串口程序 瀏覽:500
中農金融注冊app有哪些 瀏覽:629
回收站文件清理不在電腦上有痕跡 瀏覽:886
dx版本更新 瀏覽:738
主機集群教程 瀏覽:939
蘋果6英版好不好 瀏覽:959
nodejs抓取網站音頻 瀏覽:772
app上下載的軟體在哪裡 瀏覽:36

友情鏈接