I need to send XML data to a URL. The XML data is contained in a stringbuffer.
I need to post it and get the response code.
Can anyone tell me how to do it?
I have a piece of code, but eclipse says its deprecated
PostMethod postMethod = new PostMethod(URL);
InputStream stream = new StringBufferInputStream(command);
postMethod.setRequestBody(stream);
Javadoc says to use setRequestEntity(RequestEntity) instead.
Related
I am new to Java and trying to make native android application which includes making HTTP Calls to API Server. Now My issue is that for making HTTP POST (apache httpPost and httpClient) call with some JSON data. So to make StringEntity out of JSONObject I am writing this line of code:
StringEntity userDataStringEntity = new StringEntity(userDataString);
Where StringEntity is imported from import org.apache.http.entity.StringEntity;.
I have tried searching for this issue and I am finding same method with same "string" parameter.
Here are some links, but it didn't help me:
How to send a JSON object over HttpClient Request with Android?
How to send a JSON object over Request with Android?
That's definitely weird, by default the StringEntity goes for the charset "ISO-8859-1" which tells me that the userDataString is in another charset.
Either way, try:
StringEntity userDataStringEntity = new StringEntity(userDataString, "UTF-8");
This will work for utf-8 encoded strings.
Perhaps unrelated, but I was getting an error at compile time, as the new StringEntity(str) wasn't wrapped in a try catch.
Might be of use to someone tho :)
I'm trying to use Yahoo Content Analysis using a file containing text as input. So every character and length is possible.
This code works with a simple text String (no special characters, short text) however when I use longer texts or special characters I get a Bad Request error (HTTP 400) sometimes with an error message like "no viable alternative at character '['" or without an error message.
I encode every request and HTTP Post shouldn't have any limit as to the length.
Does the Yahoo service place a limit on the length of the request and/or are there any characters that it can't handle?
Any help to help this work is appreciated!
Here's my code (using commons-httpclient):
String fileInput = FileUtils.readFileToString(f);
StringBuilder builder = new StringBuilder();
builder.append("http://query.yahooapis.com/v1/public/yql?");
System.out.println(fileInput);
builder.append("q=")
.append(URLEncoder.encode("select * from contentanalysis.analyze where text='"+ fileInput +"'" , "UTF-8"))
.append("&format=json");
final String postUrl = builder.toString();
HttpClient client = new HttpClient();
PostMethod method = new PostMethod(postUrl);
// Send POST request
int statusCode = client.executeMethod(method);
I think the problem is that while you are sending the request as an HTTP POST, the YQL query and text are all included in the URL. YQL does not really have a way for you to make HTTP POST requests directly, so I can think of a couple options:
Directly use the Content Analysis web service with an HTTP POST (docs)
Create a custom YQL data table which uses the <execute> tag to run custom JavaScript which could do the POST (example)
Of these options I think the former would be easier.
This may be standard stuff but unable to get it wokring.
I'm using org.apache.commons.httpclient.methods for making Http request from my Java code. In one instance I've to make a PUT request and pass some parameters. I'm doing it the following way:
PutMethod putMethod = new PutMethod(url);
putMethod.getParams().setParameter("param1", "param1Value");
putMethod.getParams().setParameter("param2", "param2Value");
httpClient.executeMethod(putMethod);
But at the server, when it tries to read these parameters - it can only get null.
However, When I modify my url as url?param1=param1Value¶m2=param2Value it works.
How do I get it working using setParameter method?
To add Query Params to PutMethod, follow this method.
NameValuePair[] putParameters = new NameValuePair[2];
putParameters[0] = new NameValuePair(Param1, value1);
putParameters[1] = new NameValuePair(Param2, value2);
HttpClient client = new HttpClient();
PutMethod putMethod = new PutMethod(url);
putMethod.setQueryString(putParameters);
Then Call,
int response = client.executeMethod(putMethod);
Instead of putMethod.setQueryString(putParameters); you could also use
putMethod.setRequestBody(EncodingUtil.formUrlEncode(putParameters, "UTF-8"));
(This is deprecated)
GetMethod, PostMethod have slight differences when adding Query Params compared to the above code.
For More Code Examples : http://www.massapi.com/class/pu/PutMethod.html
Hope this helps.
your server side code has to support the PUT method
for example if its a Servlet you can include the method
doPUT(); // your put request will be delivered to this method
if you use REST based frameworks such as jersey
you can use
#PUT
Response yourPutMethod(){..}
I know that to send a POST request to the web I can use this syntax:
HttpPost post = new HttpPost(api_address);
String response = null;
int status_code = -1;
StringEntity se = new StringEntity(json_data, HTTP.UTF_8);
se.setContentType("application/json");
// Set entity
post.setEntity(se);
However, the setEntity methos does not exist for DELETE. So what are the alternatives to send a DELETE with data?
I gave a look to this: HttpDelete with body
but I didnt understand it really... I'm just a beginner!
You can use the solution provided in HttpDelete with body like this:
HttpDeleteWithBody delete = new HttpDeleteWithBody(api_address);
StringEntity se = new StringEntity(json_data, HTTP.UTF_8);
se.setContentType("application/json");
delete.setEntity(se);
This works for me.
But the code listed in
HttpDelete with body
is using annotation library so removed below portion if you do not wanted to include annotation jars else it is ok.
Import: import org.apache.http.annotation.NotThreadSafe;
Annotation above the class:#NotThreadSafe
and place the class in the application and use it according to "fiddler's" comment.
I am sure you can have result.As i am getting success.
I am using Apache Commons HttpClient PostMethod 3.1.
In the PostMethod class there are also three methods for setting POST method's request body:
setRequestBody(InputStream body)
setRequestBody(String body)
setRequestBody(NameValuePair[] parametersBody);
NameValuePair API
First two methods are deprecated. Does anybody knows why? Because if I want to put an XML to request body, NameValuePair does not help me.
Does anybody knows an workaround or a solution?
The javadoc says:
Deprecated. use setRequestEntity(RequestEntity)
RequestEntity has a lot of implementors, namely:
ByteArrayRequestEntity, FileRequestEntity, InputStreamRequestEntity, MultipartRequestEntity, StringRequestEntity
Use the one that suits you:
if your xml is in a String, use the StringRequestEntity
if it is in a file, use the FileRequestEntity
and so on.
Yes, so for example,
post.setRequestEntity( new StringRequestEntity( xml ) );
instead of
post.setRequestBody( xml );