今天写一个小功能需要通过http请求获取一些返回数据,但是在登陆时是需要进行用户名和密码的校验的。写好之后请求,返回异常Java Server returned HTTP response code: 401

下面是修改之后的代码:

 private static void httpRequest(){
String username = "XX";
String password = "XX";
String requestURL = "https://XXXX"; URL url;
URLConnection connection = null;
try {
url = new URL(requestURL);
connection = (HttpURLConnection) url.openConnection();
String encoding = new String(Base64.encode(new String(username+":"+password).getBytes()));
((HttpURLConnection) connection).setRequestMethod("GET");
connection.setRequestProperty("Content-Type", "text/plain");
connection.setRequestProperty("charset", "UTF-8");
connection.setRequestProperty( "Authorization","Basic "+encoding);
connection.connect(); BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream(),"utf-8"));
File fp = new File("/tmp/xml.txt");// 获取整个返回的结果入/tmp/xml.txt中
PrintWriter pfp= new PrintWriter(fp);
pfp.print(in.readLine());// 返回结果只有一行……
        pfp.close();         in.close();       } catch (MalformedURLException e) {         // TODO Auto-generated catch block
        e.printStackTrace();
      } catch (IOException e) {
        // TODO Auto-generated catch block
         e.printStackTrace();
      }finally{
      }
     }

注意事项:

1. 第15行,Basic 后面有一个空格

参考:http://stackoverflow.com/questions/22677930/java-server-returned-http-response-code-401

Java Server returned HTTP response code: 401的更多相关文章

  1. java.io.IOException: Server returned HTTP response code: 411 for URL

    今日调用一post方式提交的http接口,此接口在测试环境ip调用时无问题,但在生产环境通过域名调用时一直报如下错误: java.io.IOException: Server returned HTT ...

  2. 通过设置代理,解决服务器禁止抓取,报“java.io.IOException: Server returned HTTP response code: 403 for URL”错误的方法

    java.io.IOException: Server returned HTTP response code: 403 for URL: http:// 这个是什么异常呢? 当你使用java程序检索 ...

  3. 记录解决java.io.IOException: Server returned HTTP response code: 500 for URL:xxxxxxxx

    踩坑经历 因为项目需要去对接别的接口,使用URLConnection POST请求https接口,发送json数组时遇到java.io.IOException: Server returned HTT ...

  4. 元素 'beans' 必须不含字符 [子级], 因为该类型的内容类型为“仅元素”;Syntax error on token "Invalid Character";Server returned HTTP response code: 503 for URL;

    元素 'beans' 必须不含字符 [子级], 因为该类型的内容类型为“仅元素”:复制的代码有中文空格 Syntax error on token "Invalid Character&qu ...

  5. java get请求带参数报错 java.io.IOException: Server returned HTTP response code: 400 for URL

    解决方案 在使用JAVA发起http请求的时候,经常会遇到这个错误,我们copy请求地址在浏览器中运行的时候又是正常运行的,造成这个错误的原因主要是因为请求的URL中包含空格,这个时候我们要使用URL ...

  6. bug日记之-------java.io.IOException: Server returned HTTP response code: 400 for URL

    报的错误 出事代码 出事原因 解决方案 总结 多看源码, 我上面的实现方式并不好, 如果返回的响应编码为400以下却又不是200的情况下getErrorStream会返回null, 所以具体完美的解决 ...

  7. (转)Genymotion安装virtual device的“unable to create virtual device, Server returned Http status code 0”的解决方法

    网络原因无法下载virtual device,status 为0表示服务器没有响应.FQ下载吧,有VPN的小伙伴推荐这种. 或者直接手动下载ova虚拟机文件,然后将虚拟机文件导入到virtualbox ...

  8. genymotion下载出现Unable to create virtual device,Server returned HTTP status code 0.

    解决方法:

  9. Genymotion加入模拟器时报“Unable to create virtual device,Server returned HTTP status code 0”

    今天也遇到这个问题,算是对这个文章的一点补充 打开图中这个文件 C:\Users\xxx\AppData\Local\Genymobile 搜索 [downloadFile] 找到这个一串URL ht ...

随机推荐

  1. IPC-----POSIX消息队列

    消息队列可以认为是一个链表.进程(线程)可以往里写消息,也可以从里面取出消息.一个进程可以往某个消息队列里写消息,然后终止,另一个进程随时可以从消息队列里取走这些消息.这里也说明了,消息队列具有随内核 ...

  2. We will be discontinuing the Nitrous Development Platform and Cloud IDE on November 14th, 2016.

    我表示我很难过 Nitrous We will be discontinuing the Nitrous Development Platform and Cloud IDE on November ...

  3. 【leetcode】Reverse Linked List II

    Reverse Linked List II Reverse a linked list from position m to n. Do it in-place and in one-pass. F ...

  4. Selenium WebDriver 处理cookie

    在使用webdriver测试中,很多地方都使用登陆,cookie能够实现不必再次输入用户名密码进行登陆. 首先了解一下Java Cookie类的一些方法. 在jsp中处理cookie数据的常用方法: ...

  5. Metro各种流转换

    Ibuffer转byte[] ,(int)buffer.Length); Byte[]转Ibuffer WindowsRuntimeBufferExtensions.AsBuffer(bytes,,b ...

  6. K3中添加的一条新数据,其在数据库中的位置

    最近研究将K3系统与生产管理系统结合起来,减少工作量,但如何确定其各自后台数据库的构成,其对应数据各自位于那张表内,总结了一下: 1.从百度搜索,查看表结构,然后找到目标表    另:K3数据库中单独 ...

  7. 【STL】next_permutation的原理和使用

    1.碰到next_permutation(permutation:序列的意思) 今天在TC上碰到一道简单题(SRM531 - Division Two - Level One),是求给定数组不按升序排 ...

  8. JavaScript高级程序设计学习笔记--基本概念

    1.语句 ECMAScript中的语句以一个分号结尾:如果省略分号,则由解析器确定语句的结尾,如下例所示: var sum=a+b //即使没有分号也是有效的语句--推荐 var diff=a-b; ...

  9. MySQL 获得当前日期时间\时间戳 函数 ( 转自传智播客)

    MySQL 获得当前日期时间 函数 1.1 获得当前日期+时间(date + time)函数:now() mysql> select now(); +-------+ | now() | +-- ...

  10. EF查询视图只得到一条记录

    1.出错结果:数据库表视图有多条数据,在使用EF框架进行查询时却只得到一条数据(注:拦截EF得到的sql语句在数据库进行查询并没有任务问题). 2.出错原因:该视图中没有ID或者主键,EF查询时进行反 ...