`
JadeLuo
  • 浏览: 409598 次
  • 性别: Icon_minigender_1
  • 来自: 珠海
社区版块
存档分类
最新评论

java http请求头设置强制服务端返回不同格式结果

    博客分类:
  • java
 
阅读更多
/*
 * ====================================================================
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 * ====================================================================
 *
 * This software consists of voluntary contributions made by many
 * individuals on behalf of the Apache Software Foundation.  For more
 * information on the Apache Software Foundation, please see
 * <http://www.apache.org/>.
 *
 */
package org.apache.http.examples.client.win;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.WinHttpClients;

/**
 * This example demonstrates how to create HttpClient pre-configured with
 * support for integrated Windows authentication.
 */
public class ClientWinAuth {

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

        if (!WinHttpClients.isWinAuthAvailable()) {
            System.out.println("Integrated Win auth is not supported!!!");
        }

        CloseableHttpClient httpclient = WinHttpClients.createDefault();
        // There is no need to provide user credentials
        // HttpClient will attempt to access current user security context through
        // Windows platform specific methods via JNI.
        try {
//            HttpGet httpget = new HttpGet("http://10.0.1.209:8084/WebServletTest/ServletTest");
            HttpGet httpget = new HttpGet("http://changba.com/s/rXjwse6PBuNQd4fROXSh_Q");
            httpget.addHeader("User-Agent", "Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.23 Mobile Safari/537.36");
            System.out.println("Executing request " + httpget.getRequestLine());
            CloseableHttpResponse response = httpclient.execute(httpget);
            try {
//                System.out.println("----------------------------------------");
//                System.out.println(response.getStatusLine());
//                System.out.println(response.getProtocolVersion());
                HttpEntity httpEntity = response.getEntity();
                InputStream is = null;
                if (response.getEntity() != null) {
                    is = httpEntity.getContent();
                }
                String message = ConvertStreamToString(is);
                System.out.println("message=" + message);
//                EntityUtils.consume(response.getEntity());
            } finally {
                response.close();
            }
        } finally {
            httpclient.close();
        }
    }

    public static String ConvertStreamToString(InputStream is) {
        BufferedReader reader = new BufferedReader(new InputStreamReader(is));
        StringBuilder sb = new StringBuilder();
        String line = null;
        try {
            while ((line = reader.readLine()) != null) {
                sb.append(line + "\n");
            }
        } catch (IOException e) {
            System.out.println("Error=" + e.toString());
        } finally {
            try {
                is.close();
            } catch (IOException e) {
                System.out.println("Error=" + e.toString());
            }
        }
        return sb.toString();
    }

}

 

分享到:
评论

相关推荐

    DataTables自行封装请求参数和返回数据的零耦合服务端分页示例(PHP&JAVA)

    本例子主要展示了自行封装请求参数和返回数据的用法,对后台的交互没有任何格式和命名限制。 基于Bootstrap 2.3.2,相关的其他插件包括图标控件FontAwesome、等待提示控件Spinjs(修改版)、弹窗控件lhgdialog(修改版) ...

    JAVA上百实例源码以及开源项目

     util实现Java图片水印添加功能,有添加图片水印和文字水印,可以设置水印位置,透明度、设置对线段锯齿状边缘处理、水印图片的路径,水印一般格式是gif,png,这种图片可以设置透明度、水印旋转等,可以参考代码...

    java源码包---java 源码 大量 实例

     util实现Java图片水印添加功能,有添加图片水印和文字水印,可以设置水印位置,透明度、设置对线段锯齿状边缘处理、水印图片的路径,水印一般格式是gif,png,这种图片可以设置透明度、水印旋转等,可以参考代码...

    JAVA上百实例源码以及开源项目源代码

     util实现Java图片水印添加功能,有添加图片水印和文字水印,可以设置水印位置,透明度、设置对线段锯齿状边缘处理、水印图片的路径,水印一般格式是gif,png,这种图片可以设置透明度、水印旋转等,可以参考代码...

    java源码包4

     util实现Java图片水印添加功能,有添加图片水印和文字水印,可以设置水印位置,透明度、设置对线段锯齿状边缘处理、水印图片的路径,水印一般格式是gif,png,这种图片可以设置透明度、水印旋转等,可以参考代码...

    java源码包3

     util实现Java图片水印添加功能,有添加图片水印和文字水印,可以设置水印位置,透明度、设置对线段锯齿状边缘处理、水印图片的路径,水印一般格式是gif,png,这种图片可以设置透明度、水印旋转等,可以参考代码...

    java源码包2

     util实现Java图片水印添加功能,有添加图片水印和文字水印,可以设置水印位置,透明度、设置对线段锯齿状边缘处理、水印图片的路径,水印一般格式是gif,png,这种图片可以设置透明度、水印旋转等,可以参考代码...

    成百上千个Java 源码DEMO 4(1-4是独立压缩包)

    Java编写的山寨QQ,多人聊天+用户在线 21个目标文件 摘要:JAVA源码,媒体网络,山寨QQ,Java聊天程序 Java编写的山寨QQ,多人聊天+用户在线,程序分服务端和客户端,典型C/S结构, 当用户发送第一次请求的时候,验证...

    成百上千个Java 源码DEMO 3(1-4是独立压缩包)

    Java编写的山寨QQ,多人聊天+用户在线 21个目标文件 摘要:JAVA源码,媒体网络,山寨QQ,Java聊天程序 Java编写的山寨QQ,多人聊天+用户在线,程序分服务端和客户端,典型C/S结构, 当用户发送第一次请求的时候,验证...

    java 面试题 总结

    (s1+1运算结果是int型,需要强制转换类型) short s1 = 1; s1 += 1;(可以正确编译) 26、Math.round(11.5)等於多少? Math.round(-11.5)等於多少? Math.round(11.5)==12 Math.round(-11.5)==-11 round方法返回与...

    超级有影响力霸气的Java面试题大全文档

    (s1+1运算结果是int型,需要强制转换类型) short s1 = 1; s1 += 1;(可以正确编译) 29、Math.round(11.5)等於多少? Math.round(-11.5)等於多少? Math.round(11.5)==12 Math.round(-11.5)==-11 round方法返回...

    RxHttp:对RxJava2+Retrofit2+OkHttp3的封装

    RxHttp对RxJava2+Retrofit2+OkHttp3的封装,优雅实现接口请求和文件下载功能简介网络请求(RxRequest)支持监听请求声明周期,如开始结束和网络错误支持多BaseUrl,可针对不同请求重定向支持针对不同请求设置不同...

    积分管理系统java源码-api-gateway:API网关,准备造的一个轮子

    积分管理系统java源码 API-GATEWAY API网关,准备造的一个轮子,使用Vert.x实现 大面积重构中 ...调用方发送服务端请求是加上版本号的请求头,如: x-api-verson : 20171108 不同版本的API使用VersionPlugi

    Java高并发高性能分布式框架从无到有微服务架构设计.doc

    高并发- HTTP缓存浏览器缓存是指当我们使用浏览器访问一些网站页面或者http服务时,根据服 务端返回的缓存设置响应头将响应内容缓存到浏览器,下次可以直接使用缓存内容或者 仅需要去服务端验证内容是否过期即可。...

    整理后java开发全套达内学习笔记(含练习)

    Overload [java] 方法的重载(不同参数列表的同名方法) [,әuvә'lәud] Override [java] 方法的覆盖(覆盖父类的方法) [,әuvә'raid] polymiorphism[java] 多态 (polymorphism 多形性[,pɒli'mɒ:fizm]) ...

    blockChain_Server:区块链中心服务器的JAVA实现

    服务端:一个servlet负责用httpURLconnenct接收请求,请求参数为一个数字,是客户端计算出的hash值,和其余信息。然后服务端读取xml文件的最后一个值,并且计算hash是不是当前参数的值,如果是,则将当前值所包含的...

    XML轻松学习手册--XML肯定是未来的发展趋势,不论是网页设计师还是网络程序员,都应该及时学习和了解

    XML对这一原理进行了深化和扩展,用XML,你可以描述你的信息在哪里,你可以通过meta来验证信息,执行搜索,强制显示,或者处理其他的数据。 下面是一些XML metadata在实际应用中的用途: 1.可以验证数字签名,使...

    开涛高可用高并发-亿级流量核心技术

    16.7.11 网络抖动时,返回502错误 346 16.7.12 机器流量太大 346 16.8 其他 347 17 京东商品详情页服务闭环实践 348 17.1 为什么需要统一服务 348 17.2 整体架构 349 17.3 一些架构思路和总结 350 17.3.1 两种读服务...

Global site tag (gtag.js) - Google Analytics