Imitating a "browserLike" POST request - java

I'm trying to get search results from this site, using Java's org.apache.http.impl.client.CloseableHttpClient. I'm able to login without any problems, but when I POST the search request, the response I get is the same page refreshed without the search results. So I checked what and where exactly Chrome sends via it's POST request and configured my request the same way. Well now the response I get is one of those capture images with text that one must type to proceed(that dose not happen when searching via Chrome).
Is there any way the server recognizes that the request was not POST'ed by a browser?

Related

Jmeter calling rest url with query param

I'm trying to do a rest call which has the query params like:
https://www.tutorialspoint.com/?hasName=true
I have to set hasName=true in Jmeter. I'm getting a 500 and I do have JSON data that I want to post at the same time. If I set the parameters using the parameter tab then I can't update JSON data in the body data. How can I do both things at same time? I tried adding user defined variables and it did not work.
You can set in HTTP Request the JSON in Body Data tab and put full URL in Path field
https://www.tutorialspoint.com/?hasName=true
As a special case, if the path starts with "http://" or "https://" then this is used as the full URL.
Do a successful request in the browser or any other tool and compare the request you're sending through jmeter. Check everything including request urls, cookies, headers etc. If you think its all exactly the same. Reach out to the server admins/app devs to check the web server/application logs to find out what is wrong either in server or your request.
The 500 Internal Server Error is a very general HTTP status code that
means something has gone wrong on the website's server, but the server
could not be more specific on what the exact problem is.

How to intercept web url from chrome and add headers to the request?

given: my chrome browser hits the following url that renders a single page application web view (index.jsp).
url: http://server/dostuff?stuff=123
however, what I want to do is somehow intercept that GET request and tack on a header to it (i.e. X-HIDE-MAIN-FOOTER). if this header is present the code simply hides the footer.
doing so would allow me to see the output per chrome (and verify the footer is hidden.
I have tried sending a raw GET request through postman and other api-rest test tools with that header included in the GET request but the payload comes back with a lot of javascript tags (the same ones as a regular request so I can't tell if the header is hidden or not) and won't render the page in preview mode per postman nor can I save the file locally as .html to see what gets rendered.
any ideas here how to achieve something like this?
This worked very well
https://chrome.google.com/webstore/detail/modheader/idgpnmonknjnojddfkpgkljpfnnfcklj?hl=en
credit to #Allahbaksh Asadullah

Redirecting html page

Here is my problem :
I have to perform post from java code to some page , get the data and parse it.
The problem is that only my country ip can post to this page. Requests from another ip's are rejected.
I want to find workaround.
I have added my html page on server in my country (this server accessible from all ips) . Now I am sending a get request (in open to all server) to this page from Java code.
What I want to do is to redirect my html page to post to the original page.
I tried to use redirection , but it doesn't work - from Java code I get my html page and not redirected one.
Is there any solution or my problem ?
Thanks
I tried to use redirection , but it doesn't work - from Java code I
get my html page and not redirected one.
Yes it wont work because redirection works on client side. You perform a request to your HTML page which sends back a redirect header and your Java implementation doesnt know what to do with it. Even if it did, it had to make a new request to redirected page, which means that the request to the redirected page would still be from a denied IP.
Another option is that your redirection HTML uses JavaScript window.location.assign or something like that. The point remains the same, beacause this also is a client side solution.
You have to use some kind of server side language on the host where you placed your HTML and in that server side script you have to perform a (post or get as you wish) request to only-your-country URL. This way this only-your-country URL will see that the request came from the host where the script was, not the client itself.
For example if you can use java as your server side language on the place where currently your redirection html is, then you can check out this thread: How to send simple http post request with post parameters in java
You need a reverse proxy installed on a server located in your country. If you make a request to this reverse proxy, it will make a request to the only-your-country server and when it gets a response it will forward it to you.
So the only-your-country server will receive the same request as you make to the reverse proxy, but with a source IP address changed to the IP of the reverse proxy server.

Java servlet - Forwarding/Redirecting from subpage

I am working on a project where I have running servlets packaged in a war that listen for requests and populate a foreign div on a separate page. An initial request is made to servlet (a) and if there is data to display, it renders that HTML. If there is NO data, it passes a query string to another servlet to handle the request and then renders a page with options to choose.
I am running Tomcat 6 with Windows Server 2008.
But I run into two problems:
When I use redirect, I get no response from the servlet being
redirected to. I have some javascript alerts up that are never called by the (b)
servlet. I AM using relative paths and confirm the link is
correct in logs.
When I copy that link in step 1 in a new window, I see
the results. Just not when it's embedded in another page that makes
the request. Why would that be? Is it possibly a limitation from the host page and not being able to render the response?
When I use a forward, I see the servlet response,
but then a new window opens. Thus taking the person away from the
original page. This would be great if the results render in the same page.
What's the best practice to assure that I can "redirect" from an initial servlet call to another servlet using the response object from that first servlet?
A redirect returns a HTTP 302 response with the new URL in Location header which the client needs to deal with. Basically, your JS code must check the response status code if it's 302 and then extract the Location header and then re-send a new request on it. Repeat this until the response status code is 200.
That it works when pasting the URL in browser's address bar is because the browser already knows how to deal with 3nn responses properly. If you open up the network traffic tracker in browser's webdeveloper toolset, then you'll see that a second GET request is been fired on the new URL.
Another way, if the servlets run in the same container, is to just use RequestDispatcher#forward() instead of HttpServletResponse#sendRedirect().

Spring java code form post and redirection to a different server

I am building a web application using Spring framework that requires users to make a payment. When the user posts a form, it redirects to Hp's payment website and processes the payments there before returning to my application. This method however leaves my applications vulnerable to security threats and form manipulations.
I now want to post the form to my server, validate users inputs and if necessary post data to hp's web server. I have already written a java code for posting a form from my code and getting the response back into a file from hp's site but am unable to figure out how to redirect the user to the hp website using the java form post. Can someone please help? I am new to Spring so am open to suggestions that would help me accomplish this task either using this method or another way to do so.
Thanks
Obviously you need to perform the redirect on the server side, not on the client side. Redirection is basically returning HTTP 302 with Location header pointing to new location. When browser receives such response it opens the URL in question rather than rendering the response like it is with 200.
If you can receive and validate your form all you have to do is send the redirect back to the browser. I don't know which web framework do you use. In servlets you simply say:
response.sendRedirect("http://www.example.com/payment/...");
In spring-mvc return the following string from your controller as opposed to a view name:
return "redirect:http://www.example.com/payment/...";

Categories