Java Server returned HTTP response code: 401
今天写一个小功能需要通过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的更多相关文章
- java.io.IOException: Server returned HTTP response code: 411 for URL
今日调用一post方式提交的http接口,此接口在测试环境ip调用时无问题,但在生产环境通过域名调用时一直报如下错误: java.io.IOException: Server returned HTT ...
- 通过设置代理,解决服务器禁止抓取,报“java.io.IOException: Server returned HTTP response code: 403 for URL”错误的方法
java.io.IOException: Server returned HTTP response code: 403 for URL: http:// 这个是什么异常呢? 当你使用java程序检索 ...
- 记录解决java.io.IOException: Server returned HTTP response code: 500 for URL:xxxxxxxx
踩坑经历 因为项目需要去对接别的接口,使用URLConnection POST请求https接口,发送json数组时遇到java.io.IOException: Server returned HTT ...
- 元素 'beans' 必须不含字符 [子级], 因为该类型的内容类型为“仅元素”;Syntax error on token "Invalid Character";Server returned HTTP response code: 503 for URL;
元素 'beans' 必须不含字符 [子级], 因为该类型的内容类型为“仅元素”:复制的代码有中文空格 Syntax error on token "Invalid Character&qu ...
- java get请求带参数报错 java.io.IOException: Server returned HTTP response code: 400 for URL
解决方案 在使用JAVA发起http请求的时候,经常会遇到这个错误,我们copy请求地址在浏览器中运行的时候又是正常运行的,造成这个错误的原因主要是因为请求的URL中包含空格,这个时候我们要使用URL ...
- bug日记之-------java.io.IOException: Server returned HTTP response code: 400 for URL
报的错误 出事代码 出事原因 解决方案 总结 多看源码, 我上面的实现方式并不好, 如果返回的响应编码为400以下却又不是200的情况下getErrorStream会返回null, 所以具体完美的解决 ...
- (转)Genymotion安装virtual device的“unable to create virtual device, Server returned Http status code 0”的解决方法
网络原因无法下载virtual device,status 为0表示服务器没有响应.FQ下载吧,有VPN的小伙伴推荐这种. 或者直接手动下载ova虚拟机文件,然后将虚拟机文件导入到virtualbox ...
- genymotion下载出现Unable to create virtual device,Server returned HTTP status code 0.
解决方法:
- Genymotion加入模拟器时报“Unable to create virtual device,Server returned HTTP status code 0”
今天也遇到这个问题,算是对这个文章的一点补充 打开图中这个文件 C:\Users\xxx\AppData\Local\Genymobile 搜索 [downloadFile] 找到这个一串URL ht ...
随机推荐
- Verify Preorder/Inorder/Postorder Sequence in Binary Search Tree
Verify Preorder Sequence in Binary Search Tree \Given an array of numbers, verify whether it is the ...
- jq获取元素
<tr><td><div id="add"></div></td></tr>$("#add&quo ...
- C#之串口
1.字符发送 string strSend = "00 01 02 03"; serialPort1.Write(strSend); 2.字符接收 ReadDataFromSeri ...
- php empty()和isset()
2015年12月11日 10:59:08 echo phpversion(); //5.6.13 $a = array( 'aaa' => 1, 'bbb' => 0, 'ccc' =&g ...
- java压缩
/* @description:压缩文件操作 * @param filePath 要压缩的文件路径 * @param descDir 压缩文件保存的路径 d:\\aaa.zip */ public s ...
- jquery若干问题
1.获取服务器端的控件 $("#<%=photopath.ClientID%>").uploadPreview({ Img: "ImgPr", Wi ...
- log4j:WARN No appenders could be found for logger
直接写我的解决办法: 在src下面新建file名为log4j.properties内容如下:# Configure logging for testing: optionally with log f ...
- Mysql 基础3
1. 逗号是个好东西2.对于多条件查询 和范围查询 的灵活运用(and 和or 的灵活运用)in 用的时候注意 补充select * from car where name like '%奥迪%' a ...
- 【leetcode】Balanced Binary Tree(middle)
Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary ...
- CodeForces 407B Long Path (DP)
题目链接 题意:一共n+1个房间,一个人从1走到n+1,如果第奇数次走到房间i,会退回到房间Pi,如果偶数次走到房间i,则走到房间i+1,问走到n+1需要多少步,结果对1e9+7取模. 题解:设dp[ ...