问题描述
- java 访问带身份认证的web页面(或者webservice)
- 各路大神 请指导一下: iis里面开启了windows身份认证,java程序访问该web应该如何加身份认证? 谢谢!
代码如下,该如何改?
try {
URL realUrl = new URL(url);
URLConnection connection=realUrl.openConnection();
HttpURLConnection httpURLConnection=(HttpURLConnection)connection;
httpURLConnection.setDoOutput(true);
httpURLConnection.setRequestMethod(""POST"");
httpURLConnection.setRequestProperty(""content-type"" contentType);
outputStream = httpURLConnection.getOutputStream();
outputStreamWriter = new OutputStreamWriter(outputStreamutf-8"");
outputStreamWriter.write(str);
outputStreamWriter.flush();if (httpURLConnection.getResponseCode()>=300) { throw new Exception(Http Request is not successResponse code is ""+httpURLConnection.getResponseCode()); } inputStream=httpURLConnection.getInputStream(); inputStreamReader=new InputStreamReader(inputStreamutf-8""); bufferedReader=new BufferedReader(inputStreamReader); while((resultLine=bufferedReader.readLine())!=null){ if(!resultLine.trim().equals("""")){ stringBuffer.append(resultLine).append(""n""); //读完每行后换行 } } } catch (Exception e) { e.printStackTrace(); } ------------------------------------------- 在csdn搜到个同类问题 但貌似没有解答 http://ask.csdn.net/questions/162055
解决方案
正在写web services 一般是需要参数的,你把用户名和密码做成参数传递过去试试。
解决方案二:
@dcxy0 这个方法,我是想过的,是可行的,但是我现在是跟其它项目做一个对接,不太可能要求对方修改模式,所以还是自己能研究出来解决办法最好了,在网上搜索到一个解决办法,貌似用到CFX,可惜自己不了解,不知如何下手!