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

java發送http請求

發布時間: 2022-09-06 08:48:25

❶ 為什麼使用java發送http請求

因為現在使用的是HTTP協議所以,SUN公司在servlet介面實現類的時候考慮到如果以後還有別的協議的話那麼就會直接添加一個類來繼承GenericServlet;
servlet是介面,GenericServlet是這個介面的實現類,
HttpServlet是GenericServlet的子類.

❷ java怎麼主動發送http請求

實現思路就是先定義請求頭內容,之後進行請求頭設置。

  • 定義請求頭

  • LinkedHashMap<String,String> headers = new LinkedHashMap<String,String>();

    headers.put("Content-type","text/xml");

    headers.put("Cache-Control", "no-cache");

    headers.put("Connection", "close");

  • 給HttpPost 設置請求頭

  • HttpPost httpPost = new HttpPost("地址");

    if (headers != null) {

    for (String key : headers.keySet()) {

    httpPost.setHeader(key, headers.get(key));

    }

    }

    備註:只需要在map中設置相應的請求頭內容即可。根據實際需要修改即可

❸ 怎樣java向伺服器發送http請求

你好,java有一個組件,httpclient,這個jar包,可以模擬http客戶端,向服務端發送http請求,而且現在大部分都用的是這個。

❹ JAVA在雲伺服器上發送http請求報錯,跪求大神,小弟熬夜加班不容易啊

看你的url地址沒寫默認埠號,那麼默認埠就是80埠,確認一下你雲伺服器web項目是否用的80埠,如果不是請在url里加入響應埠號

❺ java使用Http協議發請求報文

1.java.net.Socket 中有一個介面叫 PackageReceiver 你創建一個類繼承自PackageReceiver
2.自己寫一個類ConnectToHost 定義一些常量如
public ConnectToHost()
{
port = 0;
keepAlive = false;
lastCommTime = 0;
connectionCtrl = false;
maxConnection = 10;
connections = new ArrayList();
soTimeOut = 0;
inUsedConnection = 0;
alive = true;
al = true;
reconnectImmediately = false;
pollingWeight = 1;
}
3.寫一個方法用來獲取本地要發送的數據,如
private static String getRequestData(String name) throws IOException {
// TODO Auto-generated method stub
File testReq = new File("E:/virturlSend/", name);

// 輸入流
FileInputStream infile = null;
InputStreamReader isr = null;
BufferedReader in = null;
String strLine = "";
StringBuffer strTotalLine = new StringBuffer();
try {
infile = new FileInputStream(testReq); // 以流的方式讀配置文件
isr = new InputStreamReader(infile);
in = new BufferedReader(isr);

for (int iLine = 1; (strLine = in.readLine()) != null; iLine++) { // 從上傳文件中每次讀一行
// System.out.println(strLine.getBytes());
strTotalLine.append(strLine);
}
} catch (Exception ee) {

} finally {
in.close();
isr.close();
infile.close();
}
return strTotalLine.toString();
// return "baoyy";
}
4.設置主機信息 如
ConnectToHost connectToHost = new ConnectToHost();
//connectToHost.setHostAddr("20.*2.*.35");
//connectToHost.setPort(27098);
connectToHost.setHostAddr("127.*.*.1");
connectToHost.setPort(6789);
connectToHost.setMaxConnection(20);
5.發送數據
service.addConnectToHost(connectToHost);

以上代碼不是全部 僅提供大體思路和構架 不知道能不能幫到您

❻ 怎麼用java 發送http報文

URL url = null;
HttpURLConnection httpurlconnection = null;
try {
url = new URL("http://211.147.222.30/se/sseS");
// 以post方式請求
httpurlconnection = (HttpURLConnection) url.openConnection();
httpurlconnection.setConnectTimeout(6000);
httpurlconnection.setReadTimeout(6000);
httpurlconnection.setDoOutput(true);
httpurlconnection.setRequestMethod("POST");
msg=java.net.URLEncoder.encode(msg,"utf-8");
String username = "UserName=user&Password=pwd&SrcNumber=1065&DestTermID="
+ dest+ "&MsgContent=" + msg;

httpurlconnection.getOutputStream().write(
username.getBytes("utf-8"));
httpurlconnection.getOutputStream().flush();
httpurlconnection.getOutputStream().close();
// 獲取響應代碼
code = httpurlconnection.getResponseCode();
// 獲取頁面內容
java.io.InputStream in = httpurlconnection.getInputStream();
java.io.BufferedReader breader = new BufferedReader(
new InputStreamReader(in, "gb2312"));
String str = breader.readLine();
while (str != null) {
resp+=str;
str= breader.readLine();
}
} catch (Exception e) {
resp="err";

} finally {
if (httpurlconnection != null)
httpurlconnection.disconnect();
}

❼ 如何使用HttpClient包實現JAVA發起HTTP請求

publicclassHttpClientUtil{

publicstaticStringdoGet(Stringurl,Map<String,String>param){

//創建Httpclient對象
CloseableHttpClienthttpclient=HttpClients.createDefault();


StringresultString="";
CloseableHttpResponseresponse=null;
try{
//創建uri
URIBuilderbuilder=newURIBuilder(url);
if(param!=null){
for(Stringkey:param.keySet()){
builder.addParameter(key,param.get(key));
}
}
URIuri=builder.build();

//創建httpGET請求
HttpGethttpGet=newHttpGet(uri);

//執行請求
response=httpclient.execute(httpGet);
//判斷返回狀態是否為200
if(response.getStatusLine().getStatusCode()==200){
resultString=EntityUtils.toString(response.getEntity(),"UTF-8");
}
}catch(Exceptione){
e.printStackTrace();
}finally{
try{
if(response!=null){
response.close();
}
httpclient.close();
}catch(IOExceptione){
e.printStackTrace();
}
}
returnresultString;
}

publicstaticStringdoGet(Stringurl){
returndoGet(url,null);
}

publicstaticStringdoPost(Stringurl,Map<String,String>param){
//創建Httpclient對象
CloseableHttpClienthttpClient=HttpClients.createDefault();
CloseableHttpResponseresponse=null;
StringresultString="";
try{
//創建HttpPost請求
HttpPosthttpPost=newHttpPost(url);
//創建參數列表
if(param!=null){
List<NameValuePair>paramList=newArrayList<>();
for(Stringkey:param.keySet()){
paramList.add(newBasicNameValuePair(key,param.get(key)));
}
//模擬表單
UrlEncodedFormEntityentity=newUrlEncodedFormEntity(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(Exceptione){
e.printStackTrace();
}finally{
try{
response.close();
}catch(IOExceptione){
//TODOAuto-generatedcatchblock
e.printStackTrace();
}
}

returnresultString;
}

publicstaticStringdoPost(Stringurl){
returndoPost(url,null);
}

publicstaticStringdoPostJson(Stringurl,Stringjson){
//創建Httpclient對象
CloseableHttpClienthttpClient=HttpClients.createDefault();
CloseableHttpResponseresponse=null;
StringresultString="";
try{
//創建HttpPost請求
HttpPosthttpPost=newHttpPost(url);
//創建請求內容
StringEntityentity=newStringEntity(json,ContentType.APPLICATION_JSON);
httpPost.setEntity(entity);
//執行http請求
response=httpClient.execute(httpPost);
resultString=EntityUtils.toString(response.getEntity(),"utf-8");
}catch(Exceptione){
e.printStackTrace();
}finally{
try{
response.close();
}catch(IOExceptione){
//TODOAuto-generatedcatchblock
e.printStackTrace();
}
}

returnresultString;
}
}

❽ java發送http請求500異常

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

❾ Java開發中怎麼向URL地址發送Http請求,根據返回的狀態碼判斷成功失敗

資料庫伺服器上不一定開了80埠呀,個人感覺判斷網路是否通,還是使用Ping命令的好,如下:
String ip = "127.0.0.1"; // 使用正則,從Url中解析出資料庫IP地址
System.out.println("ip = " + ip);
Process process = Runtime.getRuntime().exec("ping " + ip); // 執行Ping命令
DataInputStream dis = new DataInputStream(process.getInputStream());

byte[] buffer = new byte[1024 * 1000];
int len = dis.read(buffer);
StringBuffer sb = new StringBuffer();
while (len > 0) {
sb.append(new String(buffer, 0, len));
len = dis.read(buffer);
}
System.out.println("ping result = " + sb.toString());
// 從Ping的結果中,解析出丟失率
Pattern p = Pattern.compile("(?<=\\().*%");
Matcher m = p.matcher(sb.toString());
boolean flag = true; // 網路是否通
if (m.find()) {
String str = m.group();
System.out.println("lost = " + str);
// 如果丟失率等於100%,則說明網路不通
flag = str.equals("100%") ? false : true;
}
System.out.println("the net is " + flag);

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

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

熱點內容
db2新建資料庫 發布:2024-09-08 08:10:19 瀏覽:170
頻率計源碼 發布:2024-09-08 07:40:26 瀏覽:778
奧迪a6哪個配置帶後排加熱 發布:2024-09-08 07:06:32 瀏覽:100
linux修改apache埠 發布:2024-09-08 07:05:49 瀏覽:208
有多少個不同的密碼子 發布:2024-09-08 07:00:46 瀏覽:566
linux搭建mysql伺服器配置 發布:2024-09-08 06:50:02 瀏覽:995
加上www不能訪問 發布:2024-09-08 06:39:52 瀏覽:811
銀行支付密碼器怎麼用 發布:2024-09-08 06:39:52 瀏覽:513
蘋果手機清理瀏覽器緩存怎麼清理緩存 發布:2024-09-08 06:31:32 瀏覽:554
雲伺服器的優點與缺點 發布:2024-09-08 06:30:34 瀏覽:734