I used a xml parser many times but the only thing now is the server where the xml file is stored asks for a username and password (i have them) but how do i login with a httpGet or httpPost.
Thanks in advance.
(PS: The username and password are always the same)
I think you need to know how to do HTTP Authentication in android.
Read this question. There you will learn how to do HTTP authentication in android.
EDIT: Check here to see how to determine authetication type needed.
Using java.net.URLConnection is described very well in the following link by BalusC
Using java.net.URLConnection to fire and handle HTTP requests
Following that thread you can easily send httpGet or httpPost and login to server.
Related
https://www.cbz.com Link when pasted in IE or chrome Doesn't ask for authentication but when I write java code hitting the same URL I get Error 401.
I am in client's network and i don't know his credentials but I still want to hit the URL and generate the token.
Note :I do not provide Username Password of the client in httpurlconnection and want the functionality like the browsers(i.e without asking for uid/pwd).
Anything shared by anyone will be much appreciated!!!
You need to pass the crediantials, either use single signon using providers like SiteMinder or an LDAP authentication.
I am designing a third party application that requires a POST request to be sent to a php file on a website and hopefully I should get a response. The site requires me to be logged in in order to make this request normally through the site by pressing a button on it. If I do
Url obj = new URL("http://www.dota2lounge.com/ajax/bumpTrade.php";
HttpUrlConnection con = (HttpUrlConnection) obj.openConnection();
con.setRequestProperty("User-Agent", "Chrome/36.0.1916.144");
And then continue to carry out the POST request, will the site recognize that I am sending this from my Chrome browser in which I am already logged in? Thanks
will the site recognize that I am sending this from my Chrome browser in which I am already logged in?
No, it will not. Imagine how easy it would be to spoof the authentication system of a web application if it worked that way.
Logins typically work by sending Cookies or other headers. You need to send those to authenticate your request. For this to work as if you were logged in with your Chrome application, you'll need to find the corresponding cookies that Chrome stored and send those.
You can find from the link i shared how you can make the authentication.
https://stackoverflow.com/a/3283496/1257445
After you have made an authentication you can make a post request using the session
Cookie
am new to android and cookie concept, plz guys help me how insert a some value device number,device version and phone model name in cookies and tat cookie send into an url.i saw lot of examples but i dont know exact part.. am in still in confusion.plz guys solve my problem.phone model,device name,os version into cookie,send cookie into an url.
Cookies are not send in the url, they are sent in the HTTP header.
What library are you using ? It should have a method for sending cookies in the request.
Probably what you really need is to make cookie visible to server so it will look like normal HTTP request from browser. Is that right?
Then you don't need to insert cookie in URL.
Cookie is a special content that is sent with request headers. It is normally not exposed to user.
So what you probably need is some Java library that will handle the HTTP protocol for you. Just google some, there are plenty of them for both Java EE and for Android. They will much simplify how you can deal with HTTP requests.
Also I am not really familiar with Android development but I reckon that framework itself should have ability to manage HTTP requests with no problem. Maybe some documentation lookup will help?
I am working on small Java project to programmatically connect to a website with username/password, after login, browse to different links on the site to download some data. First, I need to connect to the website with username/password,
second, while I keep the session open, go to other links to download data.
How do I do this in Java?
Any help will be highly appreciated!
Check out the Apache HTTPClient, it can do all this for you.
Edit: Apache HTTPClient has authentication and cookie handling features included, which will save you a lot of work doing this yourself.
If you want to extract some data HtmlUnit can help you a lot it can manage the authentication and also help you with data extraction.
Investigate with your browser how the web page submits the username/pass data? HTTP Form POST, Ajax, etc..? Use a plugin like Firebug to see network traffic.
You can use URLConnection to create HTTP requests. You will neet to simulate a username/pass login and remember the cookie for use in consequent HTTP requests to simulate a session. Here are some examples: send HTTP POST request, get a cookie, send a cookie.
I want to create a small java application to copy some wiki content from one server to another. The API is based on the XML-RPC.
Basically I have three methods, login, getPage and putPage. I use Apache HttpClient 3.x and managed to use login to login successfully and getPage to get a page from the old wiki correctly.
Authentication is handled with cookies: I log into the new wiki and some cookies are set on the corresponding httpclient. The doku tells me that one of those cookies is used for authentification.
Then I execute putPage with another POST method on the same httpclient and the server responds with a authentication failure message.
The code sequence goes like this (very reduced):
HttpClient client = new HttpClient();
PostMethod postLogin = createNewPostMethod("login", "user", "pw");
client.executeMethod(postLogin);
// Now I'm logged in and the client definitly has stored the cookies
PostMethod postPutPage = createNewPostMethod("putPage", getPage());
client.executeMethod(postPutPage); // the server won't let me put the page
Should it work like that or do I have to add the cookies manually to the second post method and, if yes, how?
Edit / Solution
With the help of the answers to this question I was able to identify and solve the problem, which was outside of the usage of httpclient. At the end it was a configuration issue on the target wiki side. The answers here helped me to ask the right questions in another forum.
Cookies are handled by HTTPClient by default. You shouldn't have to do anything to have cookies work properly.
Source:
http://www.innovation.ch/java/HTTPClient/getting_started.html#cookies
Edit for Apache HTTP Client:
Apache HTTP Client behaves the same :-)
Here is the source:
http://hc.apache.org/httpclient-3.x/cookies.html
You can set manually cookies with HTTP Client but it will handle correctly cookies created during your connection.
HttpClient supports automatic management of cookies, including allowing the server to set cookies and automatically return them to the server when required. It is also possible to manually set cookies to be sent to the server.
Resources :
Apache HttpClient - cookies
I have historically used this when I wanted to accept cookies with HttpClient
postPutPage.getParams().setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY);