1、調(diào)用WebService,對方給出WebService地址,可以用Axis生成對WebService的調(diào)用代碼進行調(diào)用
蓋州ssl適用于網(wǎng)站、小程序/APP、API接口等需要進行數(shù)據(jù)傳輸應(yīng)用場景,ssl證書未來市場廣闊!成為創(chuàng)新互聯(lián)的ssl證書銷售渠道,可以享受市場價格4-6折優(yōu)惠!如果有意向歡迎電話聯(lián)系或者加微信:13518219792(備注:SSL證書合作)期待與您的合作!
2、對方提供接口文檔和傳輸方式,根據(jù)接口文檔調(diào)用。
Java接口是一系列方法的聲明,是一些方法特征的集合,一個接口只有方法的特征沒有方法的實現(xiàn),因此這些方法可以在不同的地方被不同的類實現(xiàn),而這些實現(xiàn)可以具有不同的行為(功能)。
兩種含義:一,Java接口,Java語言中存在的結(jié)構(gòu),有特定的語法和結(jié)構(gòu);二,一個類所具有的方法的特征集合,是一種邏輯上的抽象。前者叫做“Java接口”,后者叫做“接口”。
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è)置連接超時時間為5秒
conn.setReadTimeout(20 * 1000);// 設(shè)置讀取超時時間為20秒
// 使用 URL 連接進行輸出,則將 DoOutput標志設(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)信息
// 如果請求響應(yīng)碼是200,則表示成功
if (conn.getResponseCode() == 200) {
// HTTP服務(wù)端返回的編碼是UTF-8,故必須設(shè)置為UTF-8,保持編碼統(tǒng)一,否則會出現(xiàn)中文亂碼
BufferedReader in = new BufferedReader(new InputStreamReader(
(InputStream) conn.getInputStream(), "UTF-8"));
msg = in.readLine();
in.close();
}
conn.disconnect();// 斷開連接
return msg;
}
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è)置通用的請求屬性
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請求必須設(shè)置如下兩行
conn.setDoOutput(true);
conn.setDoInput(true);
//?獲取URLConnection對象對應(yīng)的輸出流
out?=?new?PrintWriter(conn.getOutputStream());
//?發(fā)送請求參數(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?請求出現(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;
}
public interface PetInterface {
public abstract void pet();
}
比如說你的Fruit類實現(xiàn)PetInterface接口寫法為:
class Fruit implemented PetInterface{
public void pet(){
}
public void hitChild(){
System.out.println("水果:");
}
星號是IP地址和端口號
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("獲取到報文單位數(shù)據(jù)長度:" + 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)用接口失敗,返回錯誤信息:" + con.getResponseMessage() + "(" + responseCode + ")";
System.out.println(responseMsg);
throw new Exception(responseMsg);
}
} catch (IOException e2) {
log.error(e2.getMessage(), e2);
throw new Exception("接口連接超時!,請檢查網(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)用這個類就能獲得返回的參數(shù)。。over.
}
}
}
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)頁題目:java訪問外部接口代碼 java做接口給外部系統(tǒng)調(diào)用
URL分享:http://www.rwnh.cn/article46/doohchg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供軟件開發(fā)、靜態(tài)網(wǎng)站、電子商務(wù)、網(wǎng)站設(shè)計公司、動態(tài)網(wǎng)站、定制開發(fā)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)