http authorization basic请求代码示例

/**
 *
 */
package testJava.java;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Base64;

/**
 * @author xxx
 * @function
 * @date 2016年4月12日
 * @version
 */
public class Zuzuche {

    public static String appPost(Object jsonBean, String reqUrl,String method) throws Exception{
        //发送数据
        HttpURLConnection conn;
        try {
            URL url = new URL(reqUrl);
            conn = (HttpURLConnection) url.openConnection();
            conn.setDoOutput(true);
            conn.setDoInput(true);
            conn.setUseCaches(false);
            conn.setInstanceFollowRedirects(false);//是否自动处理重定向
            conn.setRequestMethod(method);
//                    conn.setRequestProperty("User-Agent", "xxxEs_console");
//                    conn.setRequestProperty("Content-Type","application/x-gzip");
            conn.setRequestProperty("Host", "xxx.yyy.com");
            conn.setRequestProperty("Connection", "keep-alive");
            //base64编码的"user:passwd"字符串。如果没有,或者用户密码不对,则返回http code 401页面给客户端
            //Authorization: "Basic 用户名和密码的base64加密字符串",注意用户名和密码中间的冒号.
            conn.setRequestProperty("Authorization", "Basic xfdfsfwUxMTc1LWhQQCZ9fjpjfdsfsfesOw==");
            conn.connect();

            //接收返回数据
            InputStream in = conn.getInputStream();
//                    GZIPInputStream gzin = new GZIPInputStream(in);
//                    BufferedReader reader = new BufferedReader(new InputStreamReader(gzin,"UTF-8"));
            BufferedReader reader = new BufferedReader(new InputStreamReader(in,"UTF-8"));

            String line;
            StringBuffer sb=new StringBuffer();
            while ((line = reader.readLine()) != null) {
                sb.append(line);
            }
            reader.close();
            conn.disconnect();
            return sb.toString();
        } catch (Exception e) {
            throw e;
        }
    }

    public static void main(String[] args) {
        //base64加密
        byte[] auth = Base64.getEncoder().encode("用户名:密码".getBytes());
        String sauth = new String(auth);
        System.out.println("sauth="+sauth);

        //或者: 用户名:密码@url.com方式请求,在火狐浏览器上面.

        String url = "http://xxx.yyy.com/2.0/standard/queryStatistic.php?beginDate=2016-04-01&endDate=2016-04-02";
        try {
            System.out.println("url="+ url);
            String result = appPost(null, url, "GET");

            System.err.println("接口调用返回结果:" + result);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

}
时间: 2024-08-25 22:34:53

http authorization basic请求代码示例的相关文章

检查HTTP 的 Basic认证代码示例-JSP

检查HTTP 的 Basic认证. since http1.0 代码如下所示: <%@ page pageEncoding="UTF-8" contentType="text/html;charset=UTF-8" %> <%@ page import="sun.misc.BASE64Decoder" %> <%@ page import="java.io.IOException" %> &

jquery使用jsonp跨域请求代码示例

HTML 部分 文件名:test.html 1 <meta charset="utf-8"> 2 <script src="http://cdn.bootcss.com/jquery/1.11.1/jquery.min.js"></script> 3 <script> 4 function jsonpCallback(){ 5 alert('jsonpCallback'); 6 } 7 $(document).read

JS分析URL字符串,取得参数名,AJAX传参请求代码示例

//当前页面URL中参数分析函数,正则校验 function getQueryString(name) { var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i"); var r = window.location.search.substr(1).match(reg); if (r != null) return unescape(r[2]); return

通过http请求传递xml流和接收xml流的代码示例

通过http请求传递xml流和接收xml流的代码示例 //1.在servlet中post一个xml流:import java.io.OutputStreamWriter;import org.jdom.Document;import org.jdom.Document; public void doPost(HttpServletRequest request, HttpServletResponse response)    throws ServletException, IOExcepti

php发送get、post请求的6种方法代码示例

本文主要展示了php发送get.post请求的6种方法的代码示例,分别为使用file_get_contents .fopen.fsockopen.curl来发送GET和POST请求,代码如下: 方法1: 用file_get_contents 以get方式获取内容: 1 <?php 2 $url='http://www.jb51.net/'; 3 $html = file_get_contents($url); 4 echo $html; 5 ?> 方法2: 用fopen打开url, 以get方

SFTP客户端代码示例

SFTP客户端代码示例 环境:libssh2 1.4.3.zlib-1.2.8.openssl-1.0.1g Author: Kagula 最后更新日期:2014-5-18 从http://www.libssh2.org/下载libssh2-1.4.3.tar.gz文件,解压后打开libssh2.dsw文件升级项目到VisualStudio 2013,里面有两个项目,只要编译libssh2项目就可以了.编译前需要添加zlib和openssl的头文件和库文件链接位置,如果编译libssh2提示找不

微信消息接收 验证URL有效性 C#代码示例

官方文档只给出了PHP的示例代码 开发者提交信息后,微信服务器将发送GET请求到填写的URL上,GET请求携带四个参数: 参数 描述 signature 微信加密签名,signature结合了开发者填写的token参数和请求中的timestamp参数.nonce参数. timestamp 时间戳 nonce 随机数 echostr 随机字符串 开发者通过检验signature对请求进行校验(下面有校验方式).若确认此次GET请求来自微信服务器,请原样返回echostr参数内容,则接入生效,成为开

My.Ioc 代码示例——使用默认构造参数和覆盖参数

在 Ioc 世界中,有些框架(例如 Autofac/NInject/Unity)支持传递默认参数,有些框架(例如 SimpleInjector/LightInjector 等)则不支持.作为 My.Ioc 来说,我们支持默认参数. 当我们在 My.Ioc 中注册对象时,有些对象类型 (System.Type) 要求我们必须提供默认参数,而有些则不是必要的.在 My.Ioc 中,默认参数有两个作用:1. 容器根据默认参数来选择用于构建对象的构造函数.而一旦选定构造函数之后,今后容器便会一直使用该构

C#控制台线程计时器代码示例

在C#中提供了三种类型的计时器:1.基于 Windows 的标准计时器(System.Windows.Forms.Timer)2.基于服务器的计时器(System.Timers.Timer)3.线程计时器(System.Threading.Timer)一.基于 Windows 的标准计时器(System.Windows.Forms.Timer)首先注意一点就是:Windows 计时器是为单线程环境设计的此计时器从Visual Basic 1.0 版起就存在于该产品中,并且基本上未做改动这个计时器