I have generated web service classes by using wsimport and now I am supposed to send a XML request (particular format given) to this webservice which return a XML response and then I can use that XML response on my side. How do you create this custom XML request which I am supposed to send to webservice. Any documentation available there?
There is a lot of ways to do that..
one of them is using HttpClient from Apache and executing a POST like this
PostMethod post = new PostMethod("http://jakarata.apache.org/");
NameValuePair[] data = {
new NameValuePair("user", "joe"),
new NameValuePair("password", "bloggs")
};
post.setRequestBody(data);
post.setRequestHeader("Content-type", "application/xhtml+xml");
// execute method and handle any error responses.
...
InputStream in = post.getResponseBodyAsStream();
// handle response.
Related
have to use akka library with java and i need to do a post request with body in it, a body like this: {"subjectId":961,"other":null}
In this application, I have some examples already written with get requests and parameters to those requestes. but now i need to send a post request with body in it.
The following snippet is correct? if not, can you tell me what i do wrong? thanks.
..
Http http = new Http((ExtendedActorSystem) context().system());
HttpRequest postRequest = HttpRequest.POST(url)
.withEntity(MediaTypes.APPLICATION_JSON.toContentType(),
"{\"subjectId\": 961, \"other\": null,}");
CompletionStage<HttpResponse> response = http.singleRequest(postRequest);
..
I'm trying to send a file with resteasy client to an http server with some code like this:
File source = new File("test.pdf");
Client client = ClientBuilder.newClient();
MultipartFormDataOutput upload = new MultipartFormDataOutput();
upload.addFormData("source", source, MediaType.APPLICATION_OCTET_STREAM_TYPE);
Entity entity = Entity.entity(upload, MediaType.MULTIPART_FORM_DATA_TYPE)
Response response = client.target(url).request().post(entity);
What happens is that on the http server I'm not getting the usual "file" in request (with the content, the name etc..), but something like a regular POST parameter named "source" with the file content as its value.
I tried it with some different web servers, so the issue have to be in the request that RESTeasy builds.
Any help?
MultipartFormDataOutput behaves the same away as a HTML form would do. It sends key/value pairs to the server.
If you want to upload a MIME message consider using MultipartOutput.
i am creating a desktop application which send file to an tomcat server. the servlet receiver and saves file fine.
I need some help to do a java program that post in a https site. I dont know how to put the parameters because it a multpart form data contect type.. Please help! when I do a post with firefox its like this...
This will depend. I've used the following technique to upload a multi-part file to a server before, based on providing a series of form key/name pairs.
This will be depended on you own requirements and what the servlet is actually expecting...
MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
String name = file.getName();
entity.addPart(new FormBodyPart("someFormParameter", new StringBody("someFormName")));
/*...*/
entity.addPart("formFileNameParameter", new FileBody(file, mimeType));
HttpClient client = /*...*/
HttpPost post = new HttpPost(url.toURI());
post.setEntity(entity);
HttpResponse response = client.execute(post);
// Process response
I want to call a servlet's POST method from another application, in which I am passing request and response. Can anyone tell me how is that possible?
If your servlet is invoked in an HTTP POST then you can do an HTTP 307 redirect to another servlet and it will call it's doPost. If you want to POST to a different page from a servlet (or any Java method) you can compose a POST with something like HttpClient like this:
PostMethod post = new PostMethod("http://jakarata.apache.org/");
NameValuePair[] data = {
new NameValuePair("user", "joe"),
new NameValuePair("password", "bloggs")
};
post.setRequestBody(data);
// execute method and handle any error responses.
...
InputStream in = post.getResponseBodyAsStream();
I have no idea, how to send some text using HTTPCLIENT (java // apache) library. I need to send parameters by text to server.
Any idea?
Assume you have some-remote-server as your remote server address and some-servlet as your remote servlet which accepts param1, param2 etc.. with its respective values on request. If the remote servlet accept GET call you can use below to send the request;
HttpClient httpClient = new HttpClient();
GetMethod getMethod = new GetMethod(); //You could use PostMethod if servlet accept POST
String request ="http://some-remote-server/some-servlet?param1=value1¶m2=value2";
httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(5000);
getMethod.setURI(new URI(request, false, null));
...
And then recieve the response return from the remote servlet like this;
ObjectInputStream ois = new ObjectInputStream(getMethod.getResponseBodyAsStream());
ois.readObject();
If you can change the tool, try RestClient Tool for eclipse.
It has great support for testing restful web-services. It has option to specify,
Header Parameter,
Query Parameter,
Body Text
Request type (GET,POST,PUT,DELETE,HEAD,OPTIONS,TRACE)