欧美一区二区三区老妇人-欧美做爰猛烈大尺度电-99久久夜色精品国产亚洲a-亚洲福利视频一区二区

java訪問外部接口代碼 java做接口給外部系統(tǒng)調(diào)用

java怎么調(diào)用別人給的接口

1、調(diào)用WebService,對(duì)方給出WebService地址,可以用Axis生成對(duì)WebService的調(diào)用代碼進(jìn)行調(diào)用

蓋州ssl適用于網(wǎng)站、小程序/APP、API接口等需要進(jìn)行數(shù)據(jù)傳輸應(yīng)用場(chǎng)景,ssl證書未來市場(chǎng)廣闊!成為創(chuàng)新互聯(lián)的ssl證書銷售渠道,可以享受市場(chǎng)價(jià)格4-6折優(yōu)惠!如果有意向歡迎電話聯(lián)系或者加微信:13518219792(備注:SSL證書合作)期待與您的合作!

2、對(duì)方提供接口文檔和傳輸方式,根據(jù)接口文檔調(diào)用。

Java接口是一系列方法的聲明,是一些方法特征的集合,一個(gè)接口只有方法的特征沒有方法的實(shí)現(xiàn),因此這些方法可以在不同的地方被不同的類實(shí)現(xiàn),而這些實(shí)現(xiàn)可以具有不同的行為(功能)。

兩種含義:一,Java接口,Java語(yǔ)言中存在的結(jié)構(gòu),有特定的語(yǔ)法和結(jié)構(gòu);二,一個(gè)類所具有的方法的特征集合,是一種邏輯上的抽象。前者叫做“Java接口”,后者叫做“接口”。

JAVA怎么調(diào)用接口?

String sendPost(String jsonStr, String path)

throws IOException {

byte[] data = jsonStr.getBytes();

java.net.URL url = new java.net.URL(path);

java.net.HttpURLConnection conn =

(java.net.HttpURLConnection) url.openConnection();

conn.setRequestMethod("POST");

conn.setConnectTimeout(5 * 1000);// 設(shè)置連接超時(shí)時(shí)間為5秒

conn.setReadTimeout(20 * 1000);// 設(shè)置讀取超時(shí)時(shí)間為20秒

// 使用 URL 連接進(jìn)行輸出,則將 DoOutput標(biāo)志設(shè)置為 true

conn.setDoOutput(true);

conn.setRequestProperty("Content-Type", "text/xml;charset=UTF-8");

//conn.setRequestProperty("Content-Encoding","gzip");

conn.setRequestProperty("Content-Length", String.valueOf(data.length));

OutputStream outStream = conn.getOutputStream();// 返回寫入到此連接的輸出流

outStream.write(data);

outStream.close();//關(guān)閉流

String msg = "";// 保存調(diào)用http服務(wù)后的響應(yīng)信息

// 如果請(qǐng)求響應(yīng)碼是200,則表示成功

if (conn.getResponseCode() == 200) {

// HTTP服務(wù)端返回的編碼是UTF-8,故必須設(shè)置為UTF-8,保持編碼統(tǒng)一,否則會(huì)出現(xiàn)中文亂碼

BufferedReader in = new BufferedReader(new InputStreamReader(

(InputStream) conn.getInputStream(), "UTF-8"));

msg = in.readLine();

in.close();

}

conn.disconnect();// 斷開連接

return msg;

}

Java如何向外提供接口

public?static?String?sendPostUrl(String?url,?String?param,?String?charset)?{

PrintWriter?out?=?null;

BufferedReader?in?=?null;

String?result?=?"";

try?{

URL?realUrl?=?new?URL(url);

//?打開和URL之間的連接

URLConnection?conn?=?realUrl.openConnection();

//?設(shè)置通用的請(qǐng)求屬性

conn.setRequestProperty("accept",?"*/*");

conn.setRequestProperty("connection",?"Keep-Alive");

conn.setRequestProperty("user-agent",?"Mozilla/4.0?(compatible;?MSIE?6.0;?Windows?NT?5.1;SV1)");

//?發(fā)送POST請(qǐng)求必須設(shè)置如下兩行

conn.setDoOutput(true);

conn.setDoInput(true);

//?獲取URLConnection對(duì)象對(duì)應(yīng)的輸出流

out?=?new?PrintWriter(conn.getOutputStream());

//?發(fā)送請(qǐng)求參數(shù)

out.print(param);

//?flush輸出流的緩沖

out.flush();

//?定義BufferedReader輸入流來讀取URL的響應(yīng)

in?=?new?BufferedReader(new?InputStreamReader(conn.getInputStream(),?charset));

String?line;

while?((line?=?in.readLine())?!=?null)?{

result?+=?line;

}

}?catch?(Exception?e)?{

System.out.println("發(fā)送?POST?請(qǐng)求出現(xiàn)異常!"?+?e);

e.printStackTrace();

}

//?使用finally塊來關(guān)閉輸出流、輸入流

finally?{

try?{

if?(out?!=?null)?{

out.close();

}

if?(in?!=?null)?{

in.close();

}

}?catch?(IOException?ex)?{

ex.printStackTrace();

}

}

return?result;

}

java如何調(diào)用接口

public interface PetInterface {

public abstract void pet();

}

比如說你的Fruit類實(shí)現(xiàn)PetInterface接口寫法為:

class Fruit implemented PetInterface{

public void pet(){

}

public void hitChild(){

System.out.println("水果:");

}

java如何使用http方式調(diào)用第三方接口?最好有代碼~謝謝

星號(hào)是IP地址和端口號(hào)

public class HttpUtil {

private final static Log log = LogFactory.getLog(HttpUtil.class);

public static String doHttpOutput(String outputStr,String method) throws Exception {

Map map = new HashMap();

String URL = "http://****/interface/http.php" ;

String result = "";

InputStream is = null;

int len = 0;

int tmp = 0;

OutputStream output = null;

BufferedOutputStream objOutput = null;

String charSet = "gbk";

System.out.println("URL of fpcy request");

System.out.println("=============================");

System.out.println(URL);

System.out.println("=============================");

HttpURLConnection con = getConnection(URL);

try {

output = con.getOutputStream();

objOutput = new BufferedOutputStream(output);

objOutput.write(outputStr.getBytes(charSet));

objOutput.flush();

output.close();

objOutput.close();

int responseCode = con.getResponseCode();

if (responseCode == 200) {

is = con.getInputStream();

int dataLen = is.available();

int retry = 5;

while (dataLen == 0 retry 0) {

try {

Thread.sleep(100);

} catch (InterruptedException e) {

}

dataLen = is.available();

retry--;

log.info("未獲取到任何數(shù)據(jù),嘗試重試,當(dāng)前剩余次數(shù)" + retry);

}

log.info("獲取到報(bào)文單位數(shù)據(jù)長(zhǎng)度:" + dataLen);

byte[] bytes = new byte[dataLen];

while ((tmp = is.read()) != -1) {

bytes[len++] = (byte) tmp;

if (len == dataLen) {

dataLen = bytes.length + dataLen;

byte[] newbytes = new byte[dataLen];

for (int i = 0; i bytes.length; i++) {

newbytes[i] = bytes[i];

}

bytes = newbytes;

}

}

result = new String(bytes, 0, len, charSet);

} else {

String responseMsg = "調(diào)用接口失敗,返回錯(cuò)誤信息:" + con.getResponseMessage() + "(" + responseCode + ")";

System.out.println(responseMsg);

throw new Exception(responseMsg);

}

} catch (IOException e2) {

log.error(e2.getMessage(), e2);

throw new Exception("接口連接超時(shí)!,請(qǐng)檢查網(wǎng)絡(luò)");

}

con.disconnect();

System.out.println("=============================");

System.out.println("Contents of fpcy response");

System.out.println("=============================");

System.out.println(result);

Thread.sleep(1000);

return result;

}

private static HttpURLConnection getConnection(String URL) throws Exception {

Map map = new HashMap();

int rTimeout = 15000;

int cTimeout = 15000;

String method = "";

method = "POST";

boolean useCache = false;

useCache = false;

HttpURLConnection con = null;

try {

con = (HttpURLConnection) new URL(URL).openConnection();

} catch (IOException e) {

log.error(e.getMessage(), e);

throw new Exception("URL不合法!");

}

try {

con.setRequestMethod(method);

} catch (ProtocolException e) {

log.error(e.getMessage(), e);

throw new Exception("通信協(xié)議不合法!");

}

con.setConnectTimeout(cTimeout);

con.setReadTimeout(rTimeout);

con.setUseCaches(useCache);

con.setDoInput(true);

con.setDoOutput(true);

log.info("當(dāng)前連接信息: URL:" + URL + "," + "Method:" + method

+ ",ReadTimeout:" + rTimeout + ",ConnectTimeOut:" + cTimeout

+ ",UseCaches:" + useCache);

return con;

}

public static void main(String[] args) throws Exception {

String xml="?xml version=\"1.0\" encoding=\"GBK\" ?documenttxcode101/txcodenetnumber100001/netnumber........./document";

response=HttpUtil.doHttpOutput(xml, "post");

JSONObject json= JSONObject.parseObject(response);

String retcode=json.getString("retcode");

調(diào)用這個(gè)類就能獲得返回的參數(shù)。。over.

}

}

}

javaweb、jsp中調(diào)用外部的http接口

import java.net.*;

import java.io.*;

public class URLReader {

public static void main(String[] args) throws Exception {

URL yahoo = new URL(";param2=value2");

BufferedReader in = new BufferedReader(

new InputStreamReader(

yahoo.openStream()));

String inputLine;

while ((inputLine = in.readLine()) != null)

System.out.println(inputLine);

in.close();

}

}

網(wǎng)頁(yè)題目:java訪問外部接口代碼 java做接口給外部系統(tǒng)調(diào)用
URL分享:http://chinadenli.net/article46/doohchg.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供軟件開發(fā)靜態(tài)網(wǎng)站電子商務(wù)網(wǎng)站設(shè)計(jì)公司動(dòng)態(tài)網(wǎng)站定制開發(fā)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

綿陽(yáng)服務(wù)器托管