今天写一个小功能需要通过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. FREE 开源 API 管理工具等

    最近学习API 管理工具,发现几个不错的东西,记录如下: 1.IBM 收购NODE 厂家  STRONGLOOP 有一产品LOOPBACK,开源,好! 2.apigee  api管理平台 也不错. 3 ...

  2. VC++ LoadLibrary失败,错误126(找不到指定的模块)

    在VS中调用一个资源模块dll,LoadLibrary返回值为NULL,没有加载成功.GetLastError后原因为"找不到指定的模块"!代码如下: HINSTANCE hIns ...

  3. JS里设定延时:js中SetInterval与setTimeout用法

     js中SetInterval与setTimeout用法 JS里设定延时: 使用SetInterval和设定延时函数setTimeout 很类似.setTimeout 运用在延迟一段时间,再进行某项操 ...

  4. 【leetcode】Reverse Nodes in k-Group (hard)☆

    Given a linked list, reverse the nodes of a linked list k at a time and return its modified list. If ...

  5. 【gsl】生成随机数

    来自:http://hsxqwanting.blog.163.com/blog/static/16945437201301042830815/ 使用GSL生成随机数时的三个步骤:    (1)gsl_ ...

  6. Linux 命令执行结果输出到屏幕的同时写入到文件中

    tee命令可以做到这一点: 例:ls -al /home | tee log 就可以把命令输出的内容显示在屏幕上的同时也输出至文件log.

  7. 本地Yum

    1. mkdir xxx #新建文件夹 (新建一个挂载需要的文件夹) 2.配置本地yum源(挂载光盘) 3.进入 yum.repos.d 4.ls (查看当前文件夹全部的文件) 并 mv 修改   除 ...

  8. 6. ZigZag Conversion

    题目: The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows l ...

  9. 60. Permutation Sequence

    题目: The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and labeling all of t ...

  10. 两个viewport的故事(第一部分)

    原文:http://www.quirksmode.org/mobile/viewports.html 在这个迷你系列的文章里边我将会解释viewport,以及许多重要元素的宽度是如何工作的,比如< ...