HttpClient client = new DefaultHttpClient();
String URL = ("http://www.alfa");
HttpGet request = new HttpGet(URL);
i want to load the card with "LOAD" method and use "CONFIRM" method
can someone suggest me how to write those methods or should i use only httpget and post methods
I'm not sure a LOAD method exists, according to the RFC: http://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html
Does the API you're requesting access to specifically state "LOAD" instead of "GET" must be used?
Related
Sorry that it's potentially an easy answer but I can't find anything.
I currently have the method:
public MediaSource getConvertedMediaServletCall(String format) throws HttpException, IOException{
HttpClient httpclient = new HttpClient();
GetMethod httpGet = new GetMethod(MEDIA_SERVER_URL);
httpGet.getParams().setParameter("format", format);
httpGet.getParams().setParameter("handler", "handle");
try{
int statusCode = httpclient.executeMethod(httpGet);
byte[] responseBody = httpGet.getResponseBody();
Now I know this doing really do anything, this has to be worked on once I get the problem working. The problem is, I am create what I believe a http client, which executes the get method. Now the problem is when the code runs the httpClient.executeMethod(httpGet); the servlet doGet method is being executed, and I know this for a fact, but the parameters are never passed through from this executing method.
Anyhelpwould be appreciated..
For anyone still wondering, although HttpClient gives you the methods for adding parameters, it won't work. Get parameters have to passed in the URL, and these parameters are being added to the body of the message, like a post request.
The workaround to this is discussed here: How do I add query parameters to a GetMethod (using Java commons-httpclient)?
I have a protected resource which requires me to login. Im using the commons client with the following code block.
HttpClient httpClient = new HttpClient();
httpClient.getParams().setParameter("http.protocol.cookie-policy", CookiePolicy.BROWSER_COMPATIBILITY);
httpClient.getParams().setParameter("http.protocol.single-cookie-header", Boolean.TRUE);
PostMethod postMethod = new PostMethod("/admin/adminlogon.do");
postMethod.setRequestEntity(new StringRequestEntity("action=logon&adminUser=admin&adminPassword=password",
"application/x-www-form-urlencoded",
"UTF-8"));
postMethod.addParameter("action","logon");
postMethod.addParameter("adminUser","admin");
postMethod.addParameter("adminPassword","password");
httpClient.executeMethod(postMethod);
String response2 = postMethod.getResponseBodyAsString();
Above is where I basically login. This works fine im getting a nice little JSESSIONID cookie back.
GetMethod get = new GetMethod("/admin/api.do?action=getSomeJson");
httpClient.executeMethod(get);
When I check the logic on the sever the for the 2nd request I notice that we are using a different JSESSIONID. Therefore the get seems to fail to log in. I was under the impression the httpClient managed the cookies and sent the same cookie back. When I log into my app normally through the UI I see the same cookie in each request just not in the this test code.
String s = get.getResponseBodyAsString();
get.releaseConnection();
Do I need to do something with the httpClient to ensure it uses the same cookies from the first post request when it does its get request??
Thanks in advance.
Your assumption regarding HTTP client cookie behavior is correct.
In your case your not use the same httpClient instance. To fix it you need to allocate the httpClient only once (in PostConstructor):
httpClient = new DefaultHttpClient(); // or new HttpClient();
Then, you perform your calls using the same instance of the client. The client will take a cookie from a response, will store it in the cookieStore and will send it with the next request.
[Added after the comment]
The following code works for me:
httpClient = new DefaultHttpClient();
// Create a local instance of cookie store
cookieStore = new BasicCookieStore();
// Set the store
httpClient.setCookieStore(cookieStore);
As HttpClient document suggests - Generally it is recommended to have a single instance of HttpClient per communication component or even per application.
I got different behaviours between HttpClient is singleton or not.
1) With singleton, I first created a global static HttpClient instance, and send every request with this instance with below segment,
PostMethod post = new PostMethod(url);
int status = httpClient.executeMethod(post);
2) Without singleton, I send every request by creating a new HttpClient instance
PostMethod post = new PostMethod(url);
HttpClient httpClient = new HttpClient();
int status = httpClient.executeMethod(post);
The differences are, without singleton, everything is OK, I can get the correct result separately in consecutive requests. But with singleton, it seems there is some request context, the second request doesn't return the response string as expected because of the first request parameter (weird!!).
I don't have the service codes and server configuration. Can you help me figure out the possible reason?
Thanks in advance.
I am new to android and I'm making a simple app that sends a JSON object to the server. The many examples that I found on the internet had the following three lines of code:
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(prepai.host22.com/LoadStory.php);
HttpResponse httpResponse = httpClient.execute(httpPost);
I tried to run them but the app running in the eclipse emulator crashed when the execute() function got executed. I got this log:
Caused by: java.lang.IllegalStateException: Target host must not be null, or set in parameters. scheme=null, host=null, path=prepai.host22.com/LoadStory.php
How do I give the host a value? Also do I have to do all the communication in a separate thread? Or is there anything else that I need to do?
You should pass the full url (including the http or https) as a string, for example:
HttpPost httpPost = new HttpPost("http://prepai.host22.com/LoadStory.php");
The librarian method returns the apache HttpResponse object. I need to get the redirection Uri. How can I do that?
I think you can use Location header:
response.getLastHeader("Location").getValue();
To get redirect Uri you have to do 2 things:
Set redirecting to your HttpClient instance:
HttpClient httpclient = getNewHttpClient();
HttpClientParams.setRedirecting(httpclient.getParams(), false); // necessary to detect redirection
Get redirection Uri from HttpResponse instance after request:
response.getLastHeader("Location").getValue();