Tôi đang cố gắng chuyển một ứng dụng Android sang nền tảng iOS. Tôi nhận dữ liệu từ một dịch vụ nằm trong máy chủ Apache Tomcat và gần đây họ đã thêm bảo mật trên dịch vụ. Tổng quan về bảo mật làYêu cầu đăng Android trên IOS
- https đơn giản yêu cầu
- if (valid_user) - Dữ liệu> lợi nhuận dịch vụ
- else-> lợi nhuận dịch vụ vào ô đăng nhập -> bài credentials-> lợi nhuận dịch vụ dữ liệu
Phiên bản Android hoạt động mà không có vấn đề gì. Trước khi bảo mật được thêm vào, tôi đã thực hiện yêu cầu http đơn giản nhưng bây giờ tôi cần thực hiện yêu cầu POST với thông tin xác thực của mình và tôi allways nhận được error.html như là kết quả. Tôi không phải là chuyên gia về lập trình mạng.
Đây là Android mã thiết lập http client:
if(httpClient==null)
{
DefaultHttpClient client = new DefaultHttpClient();
KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
trustStore.load(null, null);
SSLSocketFactory sf = new MySSLSocketFactory(trustStore);
sf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
SchemeRegistry registry = new SchemeRegistry();
registry.register(new Scheme("https", sf, 8443));
mgr = new ThreadSafeClientConnManager(client.getParams(), registry);
int timeoutConnection = 5000;
HttpConnectionParams.setConnectionTimeout(client.getParams(), timeoutConnection);
// Set the default socket timeout (SO_TIMEOUT)
// in milliseconds which is the timeout for waiting for data.
int timeoutSocket = 7000;
HttpConnectionParams.setSoTimeout(client.getParams(), timeoutSocket);
httpClient = new DefaultHttpClient(mgr, client.getParams());
}
Và đây là MySSLSocketFactory lớp:
public class MySSLSocketFactory extends SSLSocketFactory {
SSLContext sslContext = SSLContext.getInstance("TLS");
public MySSLSocketFactory(KeyStore truststore) throws NoSuchAlgorithmException, KeyManagementException, KeyStoreException, UnrecoverableKeyException {
super(truststore);
TrustManager tm = new X509TrustManager() {
public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {
}
public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {
}
public X509Certificate[] getAcceptedIssuers() {
return null;
}
};
sslContext.init(null, new TrustManager[] { tm }, null);
}
@Override
public Socket createSocket(Socket socket, String host, int port, boolean autoClose) throws IOException, UnknownHostException {
return sslContext.getSocketFactory().createSocket(socket, host, port, autoClose);
}
@Override
public Socket createSocket() throws IOException {
return sslContext.getSocketFactory().createSocket();
}
}
Đây là mã yêu cầu POST cho Android (nó đang làm việc OK):
...
HttpPost httpost = new HttpPost(host+"/serviceName/j_security_check");
List<BasicNameValuePair> nvps = new ArrayList<BasicNameValuePair>();
nvps.add(new BasicNameValuePair("j_username", userName));
nvps.add(new BasicNameValuePair("j_password", userPassword));
try {
httpost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));
}
catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
httpResponse = httpClient.execute(httpost);
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
entity = httpResponse.getEntity();
...
Tôi đã thử một số cách nhưng tất cả đều có cùng kết quả. Tôi chỉ thêm mã mà tôi nghĩ là đáng chú ý:
-(void)connection:(NSURLConnection *)connection willSendRequestForAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge
{
NSURLCredential *credential=[NSURLCredential credentialWithUser:self.userName password:self.userpassword persistence:NSURLCredentialPersistenceForSession];
[[challenge sender] useCredential:credential forAuthenticationChallenge:challenge];
}
Trong phương pháp này mà yêu cầu POST được định nghĩa:
{
...
NSString *urlstr=[NSString stringWithFormat:@"%@%@",host,@"serviceName/j_security_check"];
NSURL *url=[[NSURL alloc]initWithString:urlstr];
NSMutableURLRequest *request=[NSMutableURLRequest requestWithURL:url];
NSString *params=[[NSString alloc]initWithFormat:@"j_username=my_userName&j_userpassword=my_userPassword"];
[request setHTTPMethod:@"POST"];
[request setHTTPBody:[params dataUsingEncoding:NSUTF8StringEncoding]];
[[NSURLConnection alloc]initWithRequest:request delegate:self];
}
Tôi đã được tìm kiếm một yêu cầu POST hợp lệ trên web nhưng không ai đã hữu ích ...
Nếu bạn cần thêm thông tin xin vui lòng yêu cầu nó
sự giúp đỡ của bạn được apreciated.
CHỈNH SỬA: Tôi đã thay đổi willSendRequestForAuthenticationThực hiện ủy quyền vì trước đó tôi không nhận được biểu mẫu login.html.
EDIT: Tôi đã thêm mã thiết lập khách hàng http từ Android. Bây giờ các phương pháp đăng dữ liệu trông giống như câu trả lời @MTahir nhưng tôi vẫn nhận được error.html, có nghĩa là (tôi nghĩ) rằng phương thức POST đang hoạt động ok nhưng dữ liệu không được viết đúng cách.
tôi có một số nghi ngờ ...cho dù bạn cần gửi yêu cầu đăng bài hay phương thức nhận trực tiếp ... hãy chỉ định ... và bạn cần bất kỳ giá trị được mã hóa nào ... – maheswaran
@maheswaran Tôi sử dụng phương thức GET để truy cập dữ liệu chung (bất cứ khi nào người dùng của tôi có phiên hợp lệ trên máy chủ) nhưng tôi nghĩ rằng phương thức POST là một yêu cầu trong xác thực người dùng, vì trong phiên bản Android dường như sử dụng POST. Tất cả comunication là https, nếu bạn muốn tôi có thể thêm mã cấu hình httpclient từ phiên bản Android. –
Tôi đã thêm thông tin trong câu hỏi. –