java访问外部接口代码 java做接口给外部系统调用

java怎么调用别人给的接口

1、调用WebService,对方给出WebService地址,可以用Axis生成对WebService的调用代码进行调用

盖州ssl适用于网站、小程序/APP、API接口等需要进行数据传输应用场景,ssl证书未来市场广阔!成为创新互联的ssl证书销售渠道,可以享受市场价格4-6折优惠!如果有意向欢迎电话联系或者加微信:13518219792(备注:SSL证书合作)期待与您的合作!

2、对方提供接口文档和传输方式,根据接口文档调用。

Java接口是一系列方法的声明,是一些方法特征的集合,一个接口只有方法的特征没有方法的实现,因此这些方法可以在不同的地方被不同的类实现,而这些实现可以具有不同的行为(功能)。

两种含义:一,Java接口,Java语言中存在的结构,有特定的语法和结构;二,一个类所具有的方法的特征集合,是一种逻辑上的抽象。前者叫做“Java接口”,后者叫做“接口”。

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);// 设置连接超时时间为5秒

conn.setReadTimeout(20 * 1000);// 设置读取超时时间为20秒

// 使用 URL 连接进行输出,则将 DoOutput标志设置为 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();//关闭流

String msg = "";// 保存调用http服务后的响应信息

// 如果请求响应码是200,则表示成功

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

// HTTP服务端返回的编码是UTF-8,故必须设置为UTF-8,保持编码统一,否则会出现中文乱码

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();

// 设置通用的请求属性

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(), charset));

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如何调用接口

public interface PetInterface {

public abstract void pet();

}

比如说你的Fruit类实现PetInterface接口写法为:

class Fruit implemented PetInterface{

public void pet(){

}

public void hitChild(){

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

}

java如何使用http方式调用第三方接口?最好有代码~谢谢

星号是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("未获取到任何数据,尝试重试,当前剩余次数" + retry);

}

log.info("获取到报文单位数据长度:" + 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 = "调用接口失败,返回错误信息:" + con.getResponseMessage() + "(" + responseCode + ")";

System.out.println(responseMsg);

throw new Exception(responseMsg);

}

} catch (IOException e2) {

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

throw new Exception("接口连接超时!,请检查网络");

}

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("通信协议不合法!");

}

con.setConnectTimeout(cTimeout);

con.setReadTimeout(rTimeout);

con.setUseCaches(useCache);

con.setDoInput(true);

con.setDoOutput(true);

log.info("当前连接信息: 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");

调用这个类就能获得返回的参数。。over.

}

}

}

javaweb、jsp中调用外部的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();

}

}


网页名称:java访问外部接口代码 java做接口给外部系统调用
网站链接:http://ybzwz.com/article/doohchg.html