I am using this code to connect Servlet. Mobile application when try to access internet.
The following message appears in mobile.
"Allow network access?? yes or no ". If I click "no" for that message in Nokia 6300 "Application Error" Warning will appear and it will close the application automatically.
I tried other nokia mobiles like N70 and N72. Mobile will not show "Application Error".
Is it Mobile problem or coding problem?
Is there any efficient way to connect Servlet using http?
public static InputStream getDataInputStream(String url, String request)
{
HttpConnection httpConnectionObj = null;
OutputStream dataOutputStreamObj = null;
try {
httpConnectionObj = (HttpConnection) Connector.open(url, Connector.READ_WRITE);
httpConnectionObj.setRequestMethod(HttpConnection.POST);
dataOutputStreamObj = httpConnectionObj.openOutputStream();
dataOutputStreamObj.write(request.getBytes());
dataOutputStreamObj.close();
return httpConnectionObj.openInputStream();
} catch (javax.microedition.io.ConnectionNotFoundException cnfe) {
//Alert
} catch (Exception ex) {
//Alert
} finally {
try {
if (httpConnectionObj != null) {
httpConnectionObj.close();
httpConnectionObj = null;
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
return null;
}
There is no good way to extract java.lang.Throwable.printStackTrace() on a Nokia 6300 since it is a Series40 phone.
The issue with the permission dialog has nothing to do with your code. You have to be aware of the MIDP security model in order to fix this.
A given phone has several security domain encoded in its firmware by the phone manufacturer.
In each domain, there can be several options to restrict access to a sensitive API.
When you install a MIDlet, the phone decides which domain it belongs to based on who trusts the certificate you signed it with. (could be unsigned, trusted third party, operator, manufacturer... )
When you run the MIDlet, every time it attempts to use a restricted API, the corresponding option is applied. (Could be always deny, ask the user every time, ask the user only once, always allow).
Different restricted APIs can have different options in the same domain.
There are therefore several possible explanations to your problem:
You signed the MIDlet differently for 6300 and N70.
The security domains are different on 6300 and n70.
The option to restrict HTTP connection is different on 6300 and N70.
The Mobile Network Operator is different on 6300 and N70.
I am not sure if it would help, but try closing output stream before HttpConnection in finally block:
} finally {
try {
if (dataOutputStreamObj != null)
dataOutputStreamObj.close();
dataOutputStreamObj = null;
if (httpConnectionObj != null)
httpConnectionObj.close();
httpConnectionObj = null;
} catch (IOException ex) {
ex.printStackTrace();
}
}
Related
I'm using IMobileServiceSyncTable from Azure Mobile App. In InsertAsync operation, on the backend server side, I had some validations for the data and, if that validations failure, I want throw Exception from the server side. I tried return InternalServerError(), throw HttpResponseException, but never worked on the client side. I debbuged the Post method in server side, the server throws the exception or return InternalServerError, but in the mobile client, don't occurs error.
Can anyone help me?
Here is my code on the client side:
public async Task<bool> AddPaciente(Paciente novoPaciente)
{
//other things
try
{
await _pacienteTable.InsertAsync(novoPaciente);
}
catch (Exception e)
{
Debug.WriteLine(e.Message);
Debug.WriteLine(e.StackTrace);
throw new WebException(AppResources.MensagemFalhaConexaoServidorAzure);
}
await SyncPaciente();
return true;
}
Here is my post method on the backend server side
// POST tables/Paciente
public async Task<IHttpActionResult> PostPaciente(Paciente novoPaciente)
{
//other things
if (paciente != null)
{
var responseMessage = new HttpResponseMessage(HttpStatusCode.BadRequest)
{
Content = new StringContent("Já existe um paciente com esse token cadastrado.")
};
//throw new HttpResponseException(responseMessage);
return InternalServerError(new Exception("Já existe um paciente com esse token cadastrado."));
}
}
You should listen to the response status code.
var response = await _pacienteTable.InsertAsync(novoPaciente);
if (response.IsSuccessStatusCode) {
var content = await response.Content.ReadAsStringAsync ();
}
else
{
//Here handle the error code
throw new WebException(AppResources.MensagemFalhaConexaoServidorAzure);
}
I'm using IMobileServiceSyncTable from Azure Mobile App. In InsertAsync operation, on the backend server side, I had some validations for the data and, if that validations failure, I want throw Exception from the server side.
For IMobileServiceSyncTable, you are dealing with An Offline Client which means that IMobileServiceSyncTable<T>.InsertAsync would directly insert data into the local SQLite store on your mobile client-side. Until you manually call MobileServiceClient.SyncContext.PushAsync(), then your local data store would be pushed to your mobile backend. For this approach, I would recommend you make sure that you need to validate the inputs before you saving them into the local data store, otherwise your push operations would fail, then you need to force your client user to adjust the existing inputs even after it has been successfully added before.
If you use An Online Client as follows, then both your approaches for throwing the exception would be immediately returned to your client.
var mobileClient= new MobileServiceClient("https://{your-mobile-app-name}.azurewebsites.net");
var _pacienteTable= mobileClient.GetTable<Paciente>();
await _pacienteTable.InsertAsync(novoPaciente);
Moreover, I used the following code line for catching the exception:
try
{
await table.InsertAsync(item);
}
catch (MobileServiceInvalidOperationException ex)
{
//TODO:
//await ex.Response.Content.ReadAsStringAsync(); //get detailed response content
}
catch (Exception e)
{
//TODO: other uncaught exception
}
Also, for the similar issue, you could leverage Fiddler to capture the network traces to narrow down this issue, make sure your client-side could correctly receive the relevant response.
I am trying to get back a profile for myself but I only get back a successful http response...no data. This is the code I am using.
try {
RestClient restClient = new RestClient("https://apps.na.collabserv.com","my.email","my.password");
Response<AtomFeed> responseFeed = restClient.doGet("/profiles/atom/profileService.do").asAtomFeed();
System.out.println(responseFeed.getData().toXmlString());
} catch(Exception e) {
e.printStackTrace();
}
If I try to add a email= to the url I get a 403 forbidden return.
Not really sure what the issue is,
Thanks!
I have an https web application in which we display external content in iframes (totally customizable by the user)
Since mixed content is blocked by many browsers I do the following for HTTP content:
An iframe links to my own JSP and sends the requested url as a parameter. The JSP then creates an input stream with the url and returns the response.
BufferedReader reader = null;
URL url;
String strUrl = (String) request.getParameter("url");
try {
url = new URL(strUrl);
reader = new BufferedReader(new InputStreamReader(url.openStream(), "UTF-8"));
for (String line; (line = reader.readLine()) != null;) {
out.println(line);
}
} catch (Exception e) {
log.warn("Error on URL " + strUrl);
} finally {
if (reader != null)
try {
reader.close();
} catch (IOException ignore) {
}
}
This works very well.
The question is: Can someone explain what are the security concerns here, is this something I would want to do? (I can technically say that only HTTPS urls are supported...).
Thanks!
Yes, this is certainly a security concern. What you've created is called an 'open redirect' and it's used in phishing attacks.
An attackers can abuse the trust your users have in your website (communication signed and encrypted with your SSL certificate) to redirect them to a malicious site.
Even though, they may not be able to control the usage of this JSP on your website, they can use it in an email or website comment. See this question for more information.
You can solve this problem by maintaining the list of sites you want to convert from HTTP to HTTPS at the server side and refer to them by index or keyword, like:
https://myserver/proxy.jsp?url=site1
I've created and registered custom http module to show maintenance message to user after administrator turns on maintenance mode via configuration change.
When I pass request for html it should return custom html loaded from file, but it returns message: "The service is unavailable." I can't find that string in my entire solution. Custom log message from custom maintenance module is written to log4net logs.
... INFO DdiPlusWeb.Common.MaintenanceResponder - Maintenance mode is on. Request rejected. RequestUrl=...
Seems something is miss configured in IIS on Azure. Something intercepts my 503 response. How to fix it?
Module code
void context_BeginRequest(object sender, EventArgs e)
{
HttpApplication application = (HttpApplication)sender;
HttpContext context = application.Context;
if (AppConfig.Azure.IsMaintenance)
{
MaintenanceResponder responder = new MaintenanceResponder(context, MaintenaceHtmlFileName);
responder.Respond();
}
}
Interesting part of responder code.
private void SetMaintenanceResponse(string message = null)
{
_context.Response.Clear();
_context.Response.StatusCode = 503;
_context.Response.StatusDescription = "Maintenance";
if (string.IsNullOrEmpty(message))
{
_context.Response.Write("503, Site is under maintenance. Please try again a bit later.");
}
else
{
_context.Response.Write(message);
}
_context.Response.Flush();
_context.Response.End();
}
EDIT: I lied. Sorry. Maintenance module returns the same message for requests that expect json or html.
This answer led me to the solution.
I've added another line to SetMaintenanceResponse method.
_context.Response.TrySkipIisCustomErrors = true;
It works now. Here is more about what it exactly means.
I have a midlet which sends an sms to a desired number. The midlet works fine on Nokia N70 and Nokia 6300. But while using on Samsung Champ, I am able to send an SMS only once to a certain number i.e. it works fine when sending SMS to a number but it does not work when the same or a different SMS is sent to the SAME number. It does not give any exception(s) or error(s). Here is the code I am using:
public boolean sendSMS(String contactNum, String payloadText) {
try {
String addr = "sms://" + contactNum;
MessageConnection conn = (MessageConnection) Connector.open(addr);
TextMessage msg = (TextMessage) conn.newMessage(MessageConnection.TEXT_MESSAGE);
msg.setPayloadText(payloadText);
if (conn.numberOfSegments(msg) == 0) {
return false;
}
conn.send(msg);
} catch (Exception e) {
new AlertDialog("Exception", "Exception in sendSMS() occurred", "OK").show();
}
return true;
}
Please somebody guide me in this regard.
Thanks.
I suppose problem related to SMS port. It's not recommended to use port=0 (aka phone SMS INBOX port number). Some models even restricts usage of port #0. So try to use another port, e.g. 5000 or so. But in this case SMS won't be directed to SMS INBOX, so you have to write another midlet which will listen incoming SMS on port:5000