下面是我的 java 代码,它是示例 web 服务的一部分:
@Override
public void filter(ContainerRequestContext requestContext)
{
Method method = resourceInfo.getResourceMethod();
//Access allowed for all
if( ! method.isAnnotationPresent(PermitAll.class))
{
//Access denied for all
if(method.isAnnotationPresent(DenyAll.class))
{
requestContext.abortWith(ACCESS_FORBIDDEN);
return;
}
//Get request headers
final MultivaluedMap<String, String> headers = requestContext.getHeaders();
//Fetch authorization header
final List<String> authorization = headers.get(AUTHORIZATION_PROPERTY);
//If no authorization information present; block access
if(authorization == null || authorization.isEmpty())
{
requestContext.abortWith(ACCESS_DENIED);
return;
}
//Get encoded username and password
final String encodedUserPassword = authorization.get(0).replaceFirst(AUTHENTICATION_SCHEME + " ", "");
//Decode username and password
String usernameAndPassword = new String(Base64.decode(encodedUserPassword.getBytes()));;
//Split username and password tokens
final StringTokenizer tokenizer = new StringTokenizer(usernameAndPassword, ":");
final String username = tokenizer.nextToken();
final String password = tokenizer.nextToken();
//Verifying Username and password
System.out.println(username);
System.out.println(password);
//Is user valid?
if( ! isUserAllowed(username, password))
{
requestContext.abortWith(ACCESS_DENIED);
return;
}
}
}
我得到响应的快速代码是:
let headers = [
"authorization": "Basic YWRtaW46YWRtaW4=",
"cache-control": "no-cache"
]
let parameters = ["username" : "admin","password" : "admin"]
let request = NSMutableURLRequest(URL: NSURL(string: "http://192.168.10.229:8080/JerseyDemos/rest/employees")!,
cachePolicy: .UseProtocolCachePolicy,
timeoutInterval: 60.0)
request.HTTPMethod = "GET"
//request.allHTTPHeaderFields = headers
do {
request.HTTPBody = try! NSJSONSerialization.dataWithJSONObject(parameters, options: [])
}
request.addValue("application/json", forHTTPHeaderField: "Content-Type")
request.addValue("application/json", forHTTPHeaderField: "Accept")
let session = NSURLSession.sharedSession()
let dataTask = session.dataTaskWithRequest(request, completionHandler: { (data, response, error) -> Void in
if (error != nil) {
print(error)
} else {
let httpResponse = response as? NSHTTPURLResponse
print(httpResponse)
}
})
dataTask.resume()
但这不是很好。我收到以下错误:
Optional(Error Domain=NSURLErrorDomain Code=-1017 "cannot parse response" UserInfo={NSErrorFailingURLStringKey=http://192.168.10.229:8080/JerseyDemos/rest/employees, _kCFStreamErrorCodeKey=-1, NSErrorFailingURLKey=http://192.168.10.229:8080/JerseyDemos/rest/employees, NSLocalizedDescription=cannot parse response, _kCFStreamErrorDomainKey=4, NSUnderlyingError=0x145319e0 {Error Domain=kCFErrorDomainCFNetwork Code=-1017 "(null)" UserInfo={_kCFStreamErrorDomainKey=4, _kCFStreamErrorCodeKey=-1}}})
我怎样才能使它工作? java服务工作正常。我用 Postman 查了一下。
Best Answer-推荐答案 strong>
从 ios 10 开始,您应该只使用 https 网址。
您需要将 ssl 证书添加到您的 java 服务器。
关于java - swift中的Web服务请求对应java代码,我们在Stack Overflow上找到一个类似的问题:
https://stackoverflow.com/questions/40765060/
|