Java-Get服务器在尝试调用Web服务时返回了URL:***的HTTP响应代码:401
我正在尝试用Java调用Web服务。 当我第一次给他打电话时,我犯了这个错误: “Get服务器返回了URL的HTTP响应代码:401: " 显然,我很快就找到了约1000000个关于这个话题的答案。 我试了很多 在我使用Fiddler了解为什么我可以通过IE访问服务,但不能使用IE使用的Jar之后,我看到了IE使用的Jar “授权:协商**********”而不是 “授权:基本**********”,所以我试着在谷歌上搜索“urlconnection+协商”等等 没有找到显示如何为“协商”或“kerberos”编码的答案。 这是我的密码:Java-Get服务器在尝试调用Web服务时返回了URL:***的HTTP响应代码:401,java,authentication,wsdl,webservice-client,urlconnection,Java,Authentication,Wsdl,Webservice Client,Urlconnection,我正在尝试用Java调用Web服务。 当我第一次给他打电话时,我犯了这个错误: “Get服务器返回了URL的HTTP响应代码:401: " 显然,我很快就找到了约1000000个关于这个话题的答案。 我试了很多 在我使用Fiddler了解为什么我可以通过IE访问服务,但不能使用IE使用的Jar之后,我看到了IE使用的Jar “授权:协商**********”而不是 “授权:基本**********”,所以我试着在谷歌上搜索“urlconnection+协商”等等 没有找到显示如何为“协商”或“k
public class CallWebService {
public static void main(String[] args) {
String params[] = {"0","",""};
System.out.print(UpdateDocumentId(params, "http://my-intranet-server:7047/my/web/service"));
}
public static String UpdateDocumentId(String[] args, String url) {
String retVal = "";
try {
url = url + "?wsdl";
URL Url = new URL(url);
final String userName = "domain\\user";
final String password = "password";
final URLConnection connection = Url.openConnection();
BASE64Encoder enc = new sun.misc.BASE64Encoder();
String encoded = enc.encode( (userName + ":" + password).getBytes("UTF-8") );
connection.setRequestProperty("Authorization", "Negotiate " + token);
connection.connect();
Url = connection.getURL();
ArchivingWebService service = new ArchivingWebService(Url);
ArchivingWebServicePort port = service.getArchivingWebServicePort();
Boolean res = port.updateDocumentId(Integer.parseInt(args[0]), args[1], args[2]);
retVal = (res == true) ? "true" : "false" ;
} catch (IOException ex) {
retVal = "Fehler: " + ex.getMessage();
} catch (Exception ex) {
retVal = "Fehler: " + ex.getMessage();
}
return retVal;
}
下面是完整的错误消息:
无法在以下位置访问WSDL:
http//my intranet服务器:7047/my/web/service?wsdl。失败:Get服务器返回了URL的HTTP响应代码:401:
http//my intranet服务器:7047/my/web/service?wsdl
从http//my intranet服务器打开流时:7047/my/web/service?wsdl 我希望你能帮我找到解决办法 致以最良好的祝愿,
Ren Nagasaki我也有同样的问题,在我的例子中,创建和设置默认的Java
CustomAuthenticator
是解决方案。(java.net.Authenticator
)
在执行web请求之前,添加以下行:
Authenticator.setDefault(new CustomAuthenticator());
在CallWebService
类中,添加以下代码:
public static class CustomAuthenticator extends Authenticator {
// Called when password authorization is needed
protected PasswordAuthentication getPasswordAuthentication() {
// Get information about the request. Use the variables if needed
String prompt = getRequestingPrompt();
String hostname = getRequestingHost();
InetAddress ipaddr = getRequestingSite();
int port = getRequestingPort();
String username = "domain\\user";
String password = "password";
// Return the information (a data holder that is used by Authenticator)
return new PasswordAuthentication(username, password.toCharArray());
}
}
我得到了他的帮助 您是否使用代理连接到内部网?您可能需要在代码中添加代理身份验证。不,我在与服务器计算机位于同一域的本地计算机上。我通过IE访问Web服务没有问题,但似乎无法通过Jar访问。您的IE可能有代理配置??不,我的IE是普通IE,没有VPN,没有代理。