Replace + with its unicode equivalent in a url - java

I need to test a login process with the beta server and the security policy was updated so that all users have a unique email address.
So I thought for testing the app I would just use this type of email address
"myemailaddr+a#gmail.com"
where the +a allows for a unique address. Then on each subsequent test I would use "+B" etc to ensure I have a unique email address.
All good with the server but when I try and place the URL into my android webView it wont allow me to log in. Another genuine email address works just fine.
SO, how do I replace a "+" in a url string with its Unicode value of "%2B" and is it possible to do so in a URL.
Here is the URL template:
https://team.mycompany.com/teambeta/Login.aspx?username=myemailaddr+a#gmail.com&password=xxxxxxxx&mobile=1&offSetHours=11&appDevice=AndroidAndroid
Is what I am hoping to acheive possible or do I need to go and create multiple unique email addresses for testing?

Use URLEncoder.encode(). See the documentation at https://developer.android.com/reference/java/net/URLEncoder.html

After struggling with the URL ideas and other suggestions on the net I simply replaced all instances of "+" with "%2B" as follows.
webURL = webURLBefore.replace("+", "%2B");
Works well. And I did not have to encode it as when I did I was strunggling to encode just the "+" and the whole URL was encoded and would not work.
Hope this helps someone.

Related

Camel SFTP with username with special characters

In Camel (2.15.0) call to the SFTP (docs) as such:
String uri = "sftp://foo.co.uk?username=Me+Admin&privateKeyFile=/my/id_rsa&knownHostsFile=/my/known_hosts&preferredAuthentications=publickey"
producerTemplate.sendBodyAndHeader(uri, fileContents, "CamelFileName", fullFilePath);
Results in SFTP attempt for user Me Admin. Clearly the + is replaced by a space.
I tried to url-encode this (Me%2DAdmin), still replaced by space (Me Admin).
Tried to encode it twice, now the SFTP attempt is for username Me%2DAdmin.
Anyone has an idea how do I get Camel to SFTP for a user with a + in the username? Thanks.
It’s always good to surround the username/password with RAW() tag. For example sftp:user#hostname?password=RAW(password#123)
Ok to answer my own question, changing the request url from sftp://host?username=user to sftp://user#host fixes the problem. The username still has to be encoded once:
String uri = "sftp://Me%2DAdmin#foo.co.uk?privateKeyFile=/my/id_rsa&knownHostsFile=/my/known_hosts&preferredAuthentications=publickey"
producerTemplate.sendBodyAndHeader(uri, fileContents, "CamelFileName", fullFilePath);

HttpServletRequest#getHeader("User-Agent") returns null browser name

I'm using Java 6. I have very less knowledge of JSP and Servlets.
I'm using the following code to get browser name in which my application is running:
String browserName = requestProvider.get().getHeader("User-Agent");
Also I'm using the following code to get IP address of the machine on which my application is running:
String ipAdd = requestProvider.get().getRemoteAddr();
In both the cases requestProvider is a reference variable of type Provider<HttpServletRequest>. And I'm assured that it is never NULL.
Now the problem is some times I get both values (browserName and ipAdd) NULL.
I've written sometimes because I don't have a test case.
So my question is, what are the cases in Java, when these values can be NULL?
What care should I take in coding to avoid this issue?
Is there any alternate way to get IP address & browser name every time?
String browserName = requestProvider.get().getHeader("User-Agent");
null means whoever sent the request didn't include a "User-Agent" header.
String ipAdd = requestProvider.get().getRemoteAddr();
is unlikely to return null under normal circumstances, but there are reports the it may do so in edge cases, like after the response has already been sent. Regardless, "get IP address of the machine on which my application is running" doesn't sound like what getRemoteAddr() is for. It's for getting the address of the most recent client or proxy that sent the request.
Is there any alternate way to get IP address & browser name every time?
No. You're entirely dependent on the behavior of the HTTP client and/or any intervening proxies to get information like this.
Try using user-agent as lowercase, because it works if we directly access from header.
String browserName = requestProvider.get().getHeader("user-agent");
alternate way to get IP address is
String ip = requestProvider.get().getHeader("True-Client-IP"); this works if we have akamai integeration.

javax.mail.URLName cannot parse username and password when username contains an '#' character

I am using org.springframework.integration.mail.ImapMailReceiver from spring integration to read some emails from IMAP server.
Like many other IMAP servers, the IMAP server I am connecting to uses emails addresses as usernames.
So I am creating new instance of ImapMailReceiver this way
new ImapMailReceiver(“imap://user#mail.XXXX.com.au:password#mail.XXXX.com.au:143/INBOX”);
I believe ImapMailReceiver uses URLName class to parse the given string into protocol, user and etc.
However since the url string contains 2 '#' characters, URLName class is getting confused and failing to parse the username and password.
Have anybody else had similar problems before? How did you get around this problem?
Any comments will be appretiated!!
Thanks.
The "#" in the user name needs to be URL encoded. If you use the URLName constructor that takes separate username, password, etc. parameters, it will do that for you. If you're writing it by hand, you need to write it correctly, with the proper encoding for any special characters.
I've try to get mails from GMAIL that's why I use imaps
`imapMailReceiver = new ImapMailReceiver("imaps://" + URLEncoder.encode(USERNAME, "UTF-8") + ":" + PASSWORD + "#imap.gmail.com:993/INBOX");`
No, it works well

UrlEncodedFormEntity Replacement

We have web server which only accepts decoded value from android phone
As example
"http://www.url.com/data/?name=hello World"
returns expected rssult
but when we are trying to use
"http://www.url.com/data/?name=URLEncoder.encode("hello World")"
gives nothing.
We can not change the web service.
But as we all know java only accept encoded url
How can we achieve the goal so that we can send the decoded url as it is to the server
Could you please restructure the question, to make it more clear? also the below statement appear to be incorrect (Assuming you are using Java)
http://www.url.com/data/?name=URLEncoder.encode("hello World")

Modify the url of the web application with jboss 4.2.3

i'm need to changed or hide the url of my web application deploy in jboss to the user because it's to long here's an example
my current url is:
www.example.com:8180/blazedsExample/Example/Example.html
i just want that the url was
www.example.com
thansk for the help
You can use many free url shorten services.Goole is also offering such kind of service.
Use http://goo.gl/ and enter your URL there , that will give you a shortened url.
To use this service in your application,call /action/shorten action and pass input text field (id) replaced with your value(www.example.com:8180/blazedsExample/Example/Example.html).That will return shortened URL.You can make above call using java.net.URLConnection class.
Hope this will help.

Categories