Java代码需要在objectiveC中编写一个等价物。
你能帮我解决一下:
JavaCode 网址url;
HttpURLConnection urlConn;
DataOutputStream printout;
BufferedReader input;
url = new URL(IDP_URL);
urlConn = (HttpURLConnection) url.openConnection();
urlConn.setDoOutput(true);
urlConn.setDoInput(true);
urlConn.setRequestProperty("accept", "application/json");
urlConn.setRequestMethod("POST");
// Specify the content type.
urlConn.setRequestProperty("Content-Type", "text/plain");
// Send POST output.
printout = new DataOutputStream(urlConn.getOutputStream());
String content = "username="
+ URLEncoder.encode(username, UTF_8_TYPE)
+ "&password="
+ URLEncoder.encode(password, UTF_8_TYPE)
+ "&client_assertion_type="
+ URLEncoder.encode(tokenType, UTF_8_TYPE)
+ "&client_assertion="
+ URLEncoder.encode(appToken, UTF_8_TYPE)
+ "&spEntityID="
+ URLEncoder.encode("http://localhost:8080/opensso",
UTF_8_TYPE);
printout.writeBytes(content);
printout.flush();
printout.close();
// Get response data.
input = new BufferedReader(new InputStreamReader(urlConn
.getInputStream()));
do {
str = input.readLine();
} while (str == null);
input.close();
} catch (IOException e) {
LOGGER.error("Exception: ", e);
}
等效的ios代码:
- (NSString *)urlEncodeValue:(NSString *)str
{
// NSString *result = (NSString *) CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault, (CFStringRef)str, NULL, CFSTR(":/?#[]@!$&()*+,;="), kCFStringEncodingUTF8);
// return [result autorelease];
return [str stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
}
-(void) IdpServiceCall {
NSString* strRequest = [NSString stringWithFormat:@"username=%@&password=%@&client_assertion_type=%@&client_assertion=%@&spEntityID=%@",
[self urlEncodeValue:strUsrName],[self urlEncodeValue:strPasswrd],[self urlEncodeValue:@"JWT"],[self urlEncodeValue:strAppTknContent],[self urlEncodeValue:@"http://localhost:8080/opensso"]];
NSHTTPURLResponse *response = nil;
NSError *error = nil;
NSURL *nsurl = [[NSURL alloc] initWithString:@"http://10.230.148.253:8080/IDP/services/login/authuser"];
NSMutableURLRequest* urlRequest = [[NSMutableURLRequest alloc] initWithURL:nsurl];
NSData* dataRequest = [NSData dataWithBytes:[strRequest UTF8String] length:[strRequest length]];//this line is for making the nsstring to utf8string and not encoding
[urlRequest setHTTPMethod: @"POST"];
[urlRequest setHTTPBody: dataRequest];
NSString *contentType = [NSString stringWithFormat:@"text/plain"];
[urlRequest setValue:contentType forHTTPHeaderField:@"Content-Type"];
[urlRequest setTimeoutInterval:3.0];
[urlRequest setCachePolicy:NSUrl];
NSData *responseData =[NSURLConnection sendSynchronousRequest:urlRequest returningResponse:&response error:&error];
NSString *result =[[NSString alloc]initWithData:responseData encoding:NSUTF8StringEncoding];
NSLog(@"downloaded idp output %@",result);
}
我没有得到所需的输出0字节。
请发现我的问题。真是太棒了。
答案 0 :(得分:2)
您的代码看起来很好,只有HTTP Body的NSData看起来很奇怪
尝试建立更像这样的数据:
appToken = [prefs objectForKey:@"Applicasa appToken"];
[data appendData:[[NSString stringWithFormat:@"%@=%@",@"Key",@"Value"] dataUsingEncoding:NSUTF8StringEncoding]];
[_request setHTTPBody:data];
如果您为它工作,请告诉我