我正在用java编写网络代码,它看起来如下:
//Declaring the reference variable
HttpsURLConnection httpsURLConnection = null;
AuthHttpURLConnection authHttpURLConnection = null;
httpsURLConnection = (HttpsURLConnection) ProxyUrlConnection.openConnection(url);
httpsURLConnection.setSSLSocketFactory(getSocketFactory());
authHttpURLConnection = new AuthHttpURLConnection(httpsURLConnection);
我不是ProxyUrlConnection和AuthHttpURLConnection的作者,但这里是每个的描述:
ProxyURLConnection
public class ProxyURLConnection{
public static synchronized URLConnection openConnection(URL url) throws IOException {
URLConnection urlConnection = null;
urlConnection = url.openConnection();
return urlConnection;
}
... more stuff
}
AuthHttpURLConnection
public class AuthHttpUrlConnection() extends HttpURLConnection{
public AuthHttpURLConnection(HttpURLConnection connection) {
super(connection.getURL());
// sets up the auth headers
}
}
最后HttpURLConnection和HttpsURLConnection
我的具体问题:
我怎么能做这个演员? openConnection返回URLConnection的对象,我该如何进行这种向下转换而不会出现任何运行时错误?
(HttpsURLConnection) ProxyUrlConnection.openConnection(url);
以下隐式上传是如何工作的?
AuthHttpURLConnection(httpsURLConnection);
我知道upcasting总是合法的,但如果我这样做,那么实例不会成为httpURLConnection的对象而不是httpsURLConnection,一旦发生这种情况我怎么能在httpURLConnection的对象上调用方法setSSLSocketFactory,因为这个方法仅出现在httpsURLConnection?
答案 0 :(得分:1)
一般来说,以下代码有几种可能的运行时流程:
httpsURLConnection = (HttpsURLConnection) ProxyUrlConnection.openConnection(url);
httpsURLConnection.setSSLSocketFactory(getSocketFactory());
url
参数具有“https://”方案,那么它应该没有任何问题。url
以任何其他方案开头(例如'http://','ftp://'),那么ProxyUrlConnection.openConnection(url);
可能会返回URLConnection
的不同实现}(例如HttpURLConnection
,JarURLConnection
)然后您的代码仍将编译,但是,在应用程序运行时期间,您将获得ClassCastException
,这将阻止进一步执行流程(因此将进入您的问题:setSSLSocketFactory
URLConnection
醇>
只要您对此处理流程之前的https://
方案进行明确验证,就可以使用该代码段,否则您必须明确处理URLConnection
的不同实现时的情况返回以阻止ClassCastException
。
长话短说:
我怎么能做这个演员? openConnection返回URLConnection的对象,我该如何进行这种向下转换而不会出现任何运行时错误?
只要你有明确的转换,那么这段代码就会被编译。但是,如果ClassCastException
除URLConnection
HttpsURLConnection
的任何其他实现,则在使用ClassCastException
的应用程序运行时期间将失败
我如何在httpURLConnection的对象上调用方法setSSLSocketFactory,因为此方法仅出现在httpsURLConnection中?
如果提供了非“https”URI,则在setSSLSocketFactory
方法调用之前,AuthHttpURLConnection(httpsURLConnection);
的应用程序执行流程将失败并显示connection
以下隐式上传是如何工作的?
HttpURLConnection
只要应用程序流程达到这一点,这个隐式转换将始终有效,本来可以确保ClassCastException
是var UserCampusName = "0";
var EmpID = null;
var FirstName = null;
var LastName = null;
var mobileNo = null;
var EmpEmail = null;
var userRoles = "0";
$('#SubmitUserRole').attr('disabled', 'disabled');
$(function () {
$("#userRoleForm").hover(function(){
UserCampusName = $('#UserCampus').val()
EmpID = $('#UserEmpID').val()
FirstName = $('#UserEmpFirstName').val()
LastName = $('#UserEmpLastName').val()
mobileNo = $('#UserEmpMobileNo').val()
EmpEmail = $('#UserEmpEmail').val()
userRoles = $('#Roles').val()
if ((FirstName.length > 0) && (LastName.length > 0) && (mobileNo.length > 0) && (EmpEmail.length > 0) && (EmpID.length > 0) && (userRoles != 0) && (UserCampusName != 0)) {
$('#SubmitUserRole').removeAttr('disabled');
}
else {
$('#SubmitUserRole').attr('disabled', 'disabled');
}
})
})
的任何子类的实例。在此之前会抛出<script src="http://code.jquery.com/jquery-1.8.2.min.js"></script>
<form role="form" class="form-horizontal form-bordered" id="userRoleForm">
<center>
<h2>User Roles & Privileges Set-up</h2></center>
<div class="form-group">
<label class="label1 col-md-4">Select department <span class="required"> * </span> </label>
<div class="col-md-7">
<select class="form-control" id="UserCampus" data-placeholder="Select" tabindex="1">
<option value="Category 0" default disabled selected>--Select--</option>
</select>
</div>
</div>
<div class="form-group">
<label class="label1 col-md-4">Enter the employee id <span class="required"> * </span> </label>
<div class="col-md-7">
<input id="UserEmpID" type="text" class="form-control" />
</div>
</div>
<div class="form-group">
<label class="label1 col-md-4">Enter First Name <span class="required"> * </span> </label>
<div class="col-md-7">
<input id="UserEmpFirstName" type="text" class="form-control"/>
</div>
</div>
<div class="form-group">
<label class="label1 col-md-4">Enter Middle Name </label>
<div class="col-md-7">
<input id="UserEmpMiddleName" type="text" class="form-control"/>
</div>
</div>
<div class="form-group">
<label class="label1 col-md-4">Enter Last Name <span class="required"> * </span> </label>
<div class="col-md-7">
<input id="UserEmpLastName" type="text" class="form-control"/>
</div>
</div>
<div class="form-group">
<label class="label1 col-md-4">Enter Mobile Number <span class="required"> * </span> </label>
<div class="col-md-7">
<input id="UserEmpMobileNo" type="text" class="form-control allownumericwithoutdecimal"/>
</div>
</div>
<div class="form-group">
<label class="label1 col-md-4">Enter Email Id <span class="required"> * </span> </label>
<div class="col-md-7">
<input id="UserEmpEmail" type="Email" class="form-control"/>
</div>
</div>
<div class="form-group">
<label class="label1 col-md-4">Select the applicable user role & privileges <span class="required"> * </span> </label>
<div class="col-md-7">
<select id="Roles" style="width:100%;" data-placeholder="Select an option" multiple> </select>
</div>
</div>
<div class="form-group">
<button class="btn btn-primary" id="SubmitUserRole" type="button">submit User</button>
</div>
</form>
(参见前两点)
希望这有帮助。