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

熱點內容
傳票翻打在電腦上下什麼app 瀏覽:39
db2查看資料庫字元集 瀏覽:449
小米私密文件移出後找不到 瀏覽:775
紅底白色的心是什麼app的標志 瀏覽:163
小冤家APP角色怎麼變回家長 瀏覽:822
夢幻西遊合寵模擬器網站是什麼 瀏覽:420
諾基亞930最新版本 瀏覽:201
ps製作主kv文件過大 瀏覽:884
車端面如何編程 瀏覽:279
win10u盤備份時間長 瀏覽:617
文件夾怎麼轉換為pdf 瀏覽:502
2008打開登錄密碼忘記了 瀏覽:771
蘋果7如何授權應用程序 瀏覽:899
怎樣把舊的文檔保存到桌面文件夾 瀏覽:827
wps雲數據如何恢復 瀏覽:496
微信發送過來文件 瀏覽:300
怎麼改合同網站 瀏覽:73
網路鬥地主記牌器怎麼實現的 瀏覽:377
ps鏡像文件製作教程 瀏覽:45
系統分頁文件大小設置多少 瀏覽:447

友情鏈接