第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

為HttpURLConnection添加標(biāo)頭

為HttpURLConnection添加標(biāo)頭

元芳怎么了 2019-07-29 16:48:07
為HttpURLConnection添加標(biāo)頭我正在嘗試為我的請(qǐng)求添加標(biāo)頭,HttpUrlConnection但該方法setRequestProperty()似乎不起作用。服務(wù)器端沒(méi)有收到帶有標(biāo)頭的任何請(qǐng)求。HttpURLConnection hc;     try {         String authorization = "";         URL address = new URL(url);         hc = (HttpURLConnection) address.openConnection();         hc.setDoOutput(true);         hc.setDoInput(true);         hc.setUseCaches(false);         if (username != null && password != null) {             authorization = username + ":" + password;         }         if (authorization != null) {             byte[] encodedBytes;             encodedBytes = Base64.encode(authorization.getBytes(), 0);             authorization = "Basic " + encodedBytes;             hc.setRequestProperty("Authorization", authorization);         }
查看完整描述

3 回答

?
不負(fù)相思意

TA貢獻(xiàn)1777條經(jīng)驗(yàn) 獲得超10個(gè)贊

我以前使用過(guò)以下代碼,它在TomCat中啟用了基本身份驗(yàn)證:


URL myURL = new URL(serviceURL);

HttpURLConnection myURLConnection = (HttpURLConnection)myURL.openConnection();


String userCredentials = "username:password";

String basicAuth = "Basic " + new String(Base64.getEncoder().encode(userCredentials.getBytes()));


myURLConnection.setRequestProperty ("Authorization", basicAuth);

myURLConnection.setRequestMethod("POST");

myURLConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");

myURLConnection.setRequestProperty("Content-Length", "" + postData.getBytes().length);

myURLConnection.setRequestProperty("Content-Language", "en-US");

myURLConnection.setUseCaches(false);

myURLConnection.setDoInput(true);

myURLConnection.setDoOutput(true);

您可以嘗試上面的代碼。上面的代碼用于POST,您可以為GET修改它


查看完整回答
反對(duì) 回復(fù) 2019-07-29
?
一只甜甜圈

TA貢獻(xiàn)1836條經(jīng)驗(yàn) 獲得超5個(gè)贊

只是因?yàn)槲以谏厦娴拇鸢钢袥](méi)有看到這一點(diǎn)信息,最初發(fā)布的代碼片段無(wú)法正常工作的原因是因?yàn)?code>encodedBytes變量是a byte[]而不是String值。如果您將傳遞byte[]給a new String(),如下所示,代碼段完美無(wú)缺。

encodedBytes = Base64.encode(authorization.getBytes(), 0);authorization = "Basic " + new String(encodedBytes);


查看完整回答
反對(duì) 回復(fù) 2019-07-29
  • 3 回答
  • 0 關(guān)注
  • 862 瀏覽
慕課專(zhuān)欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢(xún)優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)