當前位置:首頁 » 編程語言 » java發送請求

java發送請求

發布時間: 2023-02-26 07:27:13

java發送http請求500異常

是你請求的那個url服務出問題了 正常返回200狀態碼 但是服務返回500,請求的服務發生異常。去看看調用服務的日誌

Ⅱ 如何使用java發送post請求

/**
* 向指定 URL 發送POST方法的請求
*
* @param url
* 發送請求的 URL
* @param param
* 請求參數,請求參數應該是 name1=value1&name2=value2 的形式。
* @return 所代表遠程資源的響應結果
*/
public static String sendPost(String url, String param) {
PrintWriter out = null;
BufferedReader in = null;
String result = "";
try {
URL realUrl = new URL(url);
// 打開和URL之間的連接
URLConnection conn = realUrl.openConnection();
// 設置通用的請求屬性
conn.setRequestProperty("accept", "*/*");
conn.setRequestProperty("connection", "Keep-Alive");
conn.setRequestProperty("user-agent",
"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
// 發送POST請求必須設置如下兩行
conn.setDoOutput(true);
conn.setDoInput(true);
// 獲取URLConnection對象對應的輸出流
out = new PrintWriter(conn.getOutputStream());
// 發送請求參數
out.print(param);
// flush輸出流的緩沖
out.flush();
// 定義BufferedReader輸入流來讀取URL的響應
in = new BufferedReader(
new InputStreamReader(conn.getInputStream()));
String line;
while ((line = in.readLine()) != null) {
result += line;
}
} catch (Exception e) {
System.out.println("發送 POST 請求出現異常!"+e);
e.printStackTrace();
}
//使用finally塊來關閉輸出流、輸入流
finally{
try{
if(out!=null){
out.close();
}
if(in!=null){
in.close();
}
}
catch(IOException ex){
ex.printStackTrace();
}
}
return result;
}

Ⅲ Java發送get請求時設置ua

HttpClient方式發送請求或則以流的方式。
兩種實現方式不同,怎麼使用看個人喜好,不過在項目開發過程中,使用流的方式部署在預發機linux機器上會出現發送請求返回null的情況,但是本地windows卻正常訪問,而且,換另外一台預發機也能正常獲取數據,目前還沒有研究出個所以然,get是從伺服器上獲取數據,post是向伺服器傳送數據,get是把參數數據隊列加到提交表單的ACTION屬性所指的URL中,值和表單內各個欄位一一對應,在URL中可以看到,post是通過HTTPpost機制,將表單內各個欄位與其內容放置在HTML HEADER內一起傳送到ACTION屬性所指的URL地址。

Ⅳ 如何用java模擬ajax數據發送請求

importorg.apache.commons.httpclient.*;
importorg.apache.commons.httpclient.methods.*;
importorg.apache.commons.httpclient.params.HttpMethodParams;

importjava.io.*;

publicclassHttpClientTutorial{

PRivatestaticStringurl="http://10.129.39.149:8090/Ajax/loginMgt/login.action";

publicstaticvoidmethod(HttpClientclient,Stringurl,Stringbody){
PostMethodmethod=newPostMethod(url);
//"count":10,"ignoreCase":"false","paras":["a%"],"queryId":"getMenu"
NameValuePair[]postData=newNameValuePair[]{};
//postData[0]=newNameValuePair("count",10);
method.setRequestBody(body);//addParameters(postData);


//
/*method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
(3,false));*/

try{
//Executethemethod.
intstatusCode=client.executeMethod(method);

if(statusCode!=HttpStatus.SC_OK){
System.err.println("Methodfailed:"+method.getStatusLine());
}

//Readtheresponsebody.
byte[]responseBody=method.getResponseBody();

//Dealwiththeresponse.
//Usecaution:
System.out.println(newString(responseBody,"utf-8"));
}catch(HttpExceptione){
System.err.println("Fatalprotocolviolation:"+e.getMessage());
e.printStackTrace();
}catch(IOExceptione){
System.err.println("Fataltransporterror:"+e.getMessage());
e.printStackTrace();
}finally{
//Releasetheconnection.
method.releaseConnection();
}
}

publicstaticvoidmain(String[]args){
//CreateaninstanceofHttpClient.
HttpClientclient=newHttpClient();

Stringbody="[{"userId":1,"passWord":1}]";
//Createamethodinstance.
method(client,url,body);

url="http://10.129.39.149:8090/ajax/getInitValueArr.action";

body="[{"count":10,"ignoreCase":"false","paras":["a%"],"queryId":"getMenu"}]";
method(client,url,body);
}
}

Ⅳ JAVA代碼發送HTTP請求問題(我想實現和伺服器進行一次連接時發送兩次請求)

我覺得你這個問題的解決應該是你的程序做一次這個網站的登陸,而且這個登陸的動作應該是需要發生在你這段代碼以前,因為你這段代碼的動作其實只是訪問了一下那個網站,但是沒有任何的用戶或者是其他的信息。
一般來說你登陸以後,你會獲得一個token,用那個token就可以讓網站認為你已經登陸,然後改密碼什麼就好辦了。建議你先抓一下IE的包看看是人家的通信是怎麼樣的,然後用java做就好了。或者是那個網站有開發者文檔就最好了。

熱點內容
支持編譯成機器碼的語言 發布:2024-04-27 17:31:59 瀏覽:745
串口伺服器ip限制 發布:2024-04-27 17:30:20 瀏覽:501
sql編程入門經典 發布:2024-04-27 17:28:52 瀏覽:282
紅木卧室有哪些配置 發布:2024-04-27 17:09:47 瀏覽:855
中心機編程 發布:2024-04-27 17:00:11 瀏覽:116
lms濾波演算法 發布:2024-04-27 16:55:37 瀏覽:444
蘋果電腦遠程桌面連接伺服器 發布:2024-04-27 16:53:08 瀏覽:234
為什麼安卓手機沒有回響 發布:2024-04-27 16:53:08 瀏覽:375
搭建郵箱中繼伺服器 發布:2024-04-27 16:40:42 瀏覽:198
我的世界的神奇寶可夢伺服器 發布:2024-04-27 16:28:28 瀏覽:589