JAVA如何调用对方http接口得到返回数据

JAVA如何调用对方http接口得到返回数据https://tcc.taobao.com/cc/json/mobile_tel_segment.htm?tel=”手机号码”jsp代码:<formaction=”https://tcc.taobao.com/cc/json/mobile_tel_segment.htm”method=”post”>请输入手机号:<inputtype=”tex…

大家好,又见面了,我是你们的朋友全栈君。

https://tcc.taobao.com/cc/json/mobile_tel_segment.htm?tel=”手机号码”

jsp代码:

<form action="https://tcc.taobao.com/cc/json/mobile_tel_segment.htm" method="post">
         请输入手机号:<input type="text" name="tel" value="">
	            <input type="submit" value="查询 ">
</form>

访问jsp页面,输入测试手机号码:13535382112,点击查询按钮,得到以下返回结果

__GetZoneResult_ = {
    mts:'1353538',
    province:'广东',
    catName:'中国移动',
    telString:'13535382112',
    areaVid:'30517',
    ispVid:'3236139',
    carrier:'广东移动'
}

通过JAVA代码访问:

package com.interfaces.demo1;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

public class InterfaceTest {
	public static void main(String[] args) throws Exception {
		// 方法一
		System.out.println(InterfaceTest.getURLContent());
		// 方法二
		String urlStr = "https://tcc.taobao.com/cc/json/mobile_tel_segment.htm?tel=13535382112";
		System.out.println(InterfaceTest.getURLContent(urlStr));
	}

	public static String getURLContent() throws Exception {
		String strURL = "https://tcc.taobao.com/cc/json/mobile_tel_segment.htm?tel=13535382112";
		URL url = new URL(strURL);
		HttpURLConnection httpConn = (HttpURLConnection) url.openConnection();
		httpConn.setRequestMethod("GET");
		httpConn.connect();

		BufferedReader reader = new BufferedReader(new InputStreamReader(httpConn.getInputStream()));
		String line;
		StringBuffer buffer = new StringBuffer();
		while ((line = reader.readLine()) != null) {
			buffer.append(line);
		}
		reader.close();
		httpConn.disconnect();
		System.out.println(buffer.toString());
		System.out.println(buffer);
		System.out.println(buffer.toString());
		return buffer.toString();
	}

	/**
	 * 程序中访问http数据接口
	 */
	public static String getURLContent(String urlStr) {
		/** 网络的url地址 */
		URL url = null;
		/** http连接 */
		HttpURLConnection httpConn = null;
		/**//** 输入流 */
		BufferedReader in = null;
		StringBuffer sb = new StringBuffer();
		try {
			url = new URL(urlStr);
			in = new BufferedReader(new InputStreamReader(url.openStream(), "GBk"));
			String str = null;
			while ((str = in.readLine()) != null) {
				sb.append(str);
			}
		} catch (Exception ex) {

		} finally {
			try {
				if (in != null) {
					in.close();
				}
			} catch (IOException ex) {
			}
		}
		String result = sb.toString();
		return result;
	}
}

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/142502.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • jar中没有主清单属性啥意思啊_maven打jar包无主清单属性

    jar中没有主清单属性啥意思啊_maven打jar包无主清单属性在maven-assembly-plugin插件配置中添加,如下内容:maven-assembly-plugin

    2022年9月5日
    2
  • Django(13)django时区问题

    Django(13)django时区问题前言我们都知道时区,标准时区是UTC时区,django默认使用的就是UTC时区,所以我们存储在数据库中的时间是UTC的时间,但是当我们做的网站只面向国内用户,或者只是提供内部平台使用,我们希望存储在

    2022年7月29日
    7
  • shiro框架—shiro配置介绍(一)

    shiro框架—shiro配置介绍(一)接上一篇文章shiro框架—关于用户登录和权限验证功能的实现步骤(二)shiro在springboot项目中的配置步骤1、引入依赖  首先shiro的应用,引入的依赖仅仅只有一个,即下边这个。&amp;amp;amp;amp;lt;dependency&amp;amp;amp;amp;gt;&amp;amp;amp;amp;lt;groupId&amp;amp;amp;amp;gt;org.apache.shiro&amp;amp

    2022年9月7日
    0
  • 互联网研发部门组织架构_百度组织架构图2019

    互联网研发部门组织架构_百度组织架构图2019互联网业务研发架构体系指南(草稿V0.0.1)大纲业务技术 稳定性 【稳定性day0】稳定性治理的三种思想—亚马逊、Netflix与蚂蚁金服 【稳定性day1】从DBA到运维架构总监之路-专注的力量 【稳定性day2】当当网的高可用之道 【稳定性day3】蘑菇街的运维体系-如何撑住双十一 【稳定性day4】美团外卖高可用的演进之路-日活两千万的…

    2022年10月12日
    0
  • HTML转word_讯飞语记怎么变成word文档

    HTML转word_讯飞语记怎么变成word文档HTML转word背景介绍1.使用POI进行转化1.1思路1.2代码示例1.3思考2.使用jacob进行转化2.1思路2.2代码示例2.3思考3.总结背景介绍业务:将平台中尽调笔记(富文本)以word形式导出。1.使用POI进行转化依赖jarpoi-3.17.jarpoi-excelant-3.17.jarpoi-ooxml-3.17.jarpoi-ooxml-…

    2022年10月12日
    0
  • WPF中WrapPanel、StackPanel等添加滚动条ScrollViewer

    WPF中WrapPanel、StackPanel等添加滚动条ScrollViewerwpf中,在控件中直接设置ScrollViewer.HorizontalScrollBarVisibility和ScrollViewer.VerticalScrollBarVisibility属性,并不能显示滚动条。因为在wpf中,想要显示滚动条,需要把控件放在滚动条视图控件(ScrollViewer)中。ScrollViewer属性说明:VerticalScrollBarVisibili

    2022年7月23日
    7

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号