How to send text by HttpPost method? - java

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&param2=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)

Related

How to redirect to a site from proxy java

Am using Apache Httpclient to open a URL through proxy and getting response instead I want to redirect the site from a proxy passing post parameters.
This is my code , It is a servlet
String parameter= request.getParameter("parameter");
HttpClient httpClient = new HttpClient();
httpClient.getHostConfiguration().setProxy(proxyhost, proxyport);
log.info("message:::"+message);
PostMethod postMethod = new PostMethod(url);
NameValuePair[] data = new NameValuePair[1];
data[0] = new NameValuePair("parameter", parameter);
postMethod.setRequestBody(data);
int code = httpClient.executeMethod(postMethod);
response.setContentType("text/html");
PrintWriter out=response.getWriter();
out.print(postMethod.getResponseBodyAsString());
Actual problem is am getting response from other site and html is being rendered and clicking the link in browser the URL is being opened from my server because the response is coming this this format
var url "../../someparams"
It should indeed open http://url/someparams(url here is mentioned above one in the code) or The as soon as the URL is hit using proxy can we redirect to that page in the browser too I mean opening the URL through that proxy and removing the URl of the servlet being called.
#Law Anthony:
response.sendRedirect("http://www.google.com"); is not helping
Need your help to resolve this .
Is this a servlet?
If it is,
repsonse.sendRedirect("http://www.google.com");
This will send a code 302 redirect to the request.
EDIT: What exactly you want?

How to use the Android Apache HttpDelete class with parameter

I need to send an ID to the server and have the server to delete one record in a DB.
I want to use the HttpDelete Apache Android SDK integrated class but I cannot figure out how to use it and how to pass parameters to the server.
With the POST request I use .setEntity method on the HttpPost class.
But in HttpDelete there's no .setEntity method.
What I have so far achieved is:
HttpClient httpclient = new DefaultHttpClient();
HttpDelete httpdelete = new HttpDelete(url);
httpdelete.setHeader(HTTP.CONTENT_TYPE, "text/xml");
response = httpclient.execute(httpdelete);
HTTP DELETE requests do not have a body. You pass parameters right on the URL:
String url = "http://foo.com/bar?bing=bang"
HttpDelete httpdelete = new HttpDelete(url);

How to send an XML request to a web service

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.

how to sending multipart/form-data Post Request in with use of Apache HttpComponents in java

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

Configuring Apache HttpClient to access service through proxy/load-balancer (overriding Host header)

I am having a problem getting the Apache HttpClient to connect to a service external to my virtualised development environment.
To access the internet (e.g. api.twitter.com) I need to call a local URL (e.g. api.twitter.com.dev.mycompany.net), which then forwards the request to real host.
The problem is, that to whatever request I send, I get a 404 Not Found response.
I have tried debugging it using wget, and it appears the problem is, that the destination server identifies the desired resource by using both the request URL and the hostname in the Host header. Since the hostname does not match, it is unable to locate the resource.
I have (unsuccessfully) tried to override the Host header by setting the http.virtual-host parameter on the client like this:
HttpClient client = new DefaultHttpClient();
if (envType.isWithProxy()) {
client.getParams().setParameter(ClientPNames.VIRTUAL_HOST, "api.twitter.com");
}
Technical details:
Client is used as an executor in RESTeasy to call the REST API. So "manually" setting the virtual host (as described here) is not an option.
Everything is done via HTTPS/SSL - not that I think it makes a difference.
Edit 1: Using a HttpHost instead of a String does not have the desired effect either:
HttpClient client = new DefaultHttpClient();
if (envType.isWithProxy()) {
HttpHost realHost = new HttpHost("api.twitter.com", port, scheme);
client.getParams().setParameter(ClientPNames.VIRTUAL_HOST, realHost);
}
Edit 2: Further investigation has revealed, that the parameter needs to be set on the request object. The following is the code v. 4.2-aplha1 of HttpClient setting the virtual host:
HttpRequest orig = request;
RequestWrapper origWrapper = wrapRequest(orig);
origWrapper.setParams(params);
HttpRoute origRoute = determineRoute(target, origWrapper, context);
virtualHost = (HttpHost) orig.getParams().getParameter(
ClientPNames.VIRTUAL_HOST);
paramsare the parameters passed from the client. But the value for 'virtualHost' is read from the request parameters.
So this changes the nature of the question to: How do I set the VIRTUAL_HOST property on the requests?
ClientPNames.VIRTUAL_HOST is the right parameter for overriding physical host name in HTTP requests. I would just recommend setting this parameter on the request object instead of the client object. If that does not produce the desired effect please post the complete wire / context log of the session (see logging guide for instructions) either here or to the HttpClient user list.
Follow-up
OK. Let's take a larger sledge hammer. One can override content of the Host header using an interceptor.
DefaultHttpClient client = new DefaultHttpClient();
client.addRequestInterceptor(new HttpRequestInterceptor() {
public void process(
final HttpRequest request,
final HttpContext context) throws HttpException, IOException {
request.setHeader(HTTP.TARGET_HOST, "www.whatever.com");
}
});
One can make the interceptor clever enough to override the header selectively, only for specific hosts.

Categories