I currently have to post HTML form to a java server like ( www.example.com/file.do ).Java server accept only HTML request. So whenever i post request with html file its work fine. But whenever i post it from php its not working ( same html file just change the extension to php). I have also try with mod-rewrite by changing php to html with .htaccess but nothing happen still not works. So i am thinking is there any difference in html form post and php form post? Or is there any other solution so i can post html form through php to java?
Thanks to all.
I think you are posting from localhost. Try to post from a live server. Hopefully this will solve your problem.
technically there is no difference.
Check whether there are some special http headers set, which are not accepted by your server, i.e.
X-Powered-By etc.
To create a POST request from a PHP script, you need to use curl; but to submit a form from the browser, it doesn't matter what the extension of the file that is rendered is.
Related
I am developing a website with Java for the backend and Angular for frontend. There is a situation when some external websites may send data to my website using POST form. For instance,
▼ General
Request URL: https://myangularwebsite/
Request Method: POST
...
▼ Request Headers
Content-Type: application/x-www-form-urlencoded
Host: myangularwebsite
Origin: https://externalwebsite
Referer: https://externalwebsite/send.form?id=0
...
▼ Form data
ID: 0000000
TIME: 2017.06.04 11:53:58
SIGNATURE: ...geirgmGKFGJWR...
...
Now, I need to capture the form in Angular somehow, send/redirect it to the backend to validate the signature and return the answer back to Angular to proceed working with this website.
I tried posting to my website to test how it might work using Postman, but get Cannot POST /.
I know how to work with GET and URL query parameters in Angular but I think I need to process a POST request based on headers I see with Chrome DevTools 'Network' section when coming from externalwebsite to myangularwebsite.
Should I dedicate a route in the backend and expose it, for example, .../api/external in my backend and tell these websites to use this link instead of directly posting to my Angular website's homepage?
I have already read another question ( How to read form post data in Angular 2 typescript? ) which is somewhat similar but I do not think using PHP is the right way for me as the website I am developing already has an older version written in PHP.
The answer at the link you provided is correct: you cannot do it in just Javascript, you have to use some server-side code. They mention PHP as an example, but any server-side component will do, and as you have Java at your backend, let it be Java.
So, when an HTTP request comes from an external site, you have to use a server-side component to handle it. But there are some options.
If this request is made using your user browser (so it is something like a redirect, but using a POST method), then you can do the following: catch that request at your backend, output some javascript with some data to the user's browser and process that data in your Angular code. Or this could be a redirection to your main Angular entry point, it is up to you.
If this request is made by some other means (for example, this is a server-to-server request made with with curl like a notification from a Credit Card processing), with no browser involved, then you don't need to have any Javascript (Angular or whatever it could be) as they are needed for browser only. In this case you just handle the request at your server-side.
In both cases, it seems plausible to dedicate some special endpoint for handling (or landing) such externally-originated requests.
In my organization, all http apps undergo a security check. There are a few headers I need to add to each response via the servlet, that's easy.
But, I have a new servlet, which returns html code as a response. part of the html is:
<script type=\"text/javascript\" src= \""+_pathToScript +"myScript.js\"/>
now, the browser gets this response and then generates another GET request for the file. this request is not passing via my servlet and thus I can't add the needed headers to this response.
I tried to change the src value to something like /servlet/MyServlet?GetJS, but the browser doesn't seem to understand that and it doesn't trigger the download.
Any suggestions?
Im running on IBM WebSphere.
Thanks a lot!
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.
I am rewriting some code that used to use GET and replaced it with POST.
The download URL used to be a GET request to
https://myurl/getfile?fileid=1234&filetype=pdf
Now, I changed that to
https://myurl/getfile
and put the fileid=1234&filetype=pdf in the POST body.
I did this using jquery's post method as:
function postCall(url, param) {
$.post(url, param);
}
The server side is written using Java and I tried to reused the old code for GET, which write the file binary into the servlet's stream.
However, my browser does not prompt user for download, which used to do for GET.
Previous posts on stackoverflow did suggest that AJAX should not be used for file download. But what is the alternative way for me to use? The request is not generated by a form though.
Many thanks.
I would suggest creating a form on the page (or create one dynamically using jQuery), and then have that form do the post submission (using jQuery's "submit" function or "trigger('submit')" on the form). This way the request won't be done asynchronously in the background. If the "getfile" script responds with a file with Content-disposition: attachment, it should download.
That said, I'm not sure the browser will "prompt" the user in this scenario--this is dependent on the browser (whether or not a dialog appears to save the download, or if it automatically downloads the file without a prompt).
I'm wondering if there is any way to upload files from a java program to a website that has a simple html form.
An example could look like the one on tinypic.com or bayimg.com.
Are there any Java libraries capable of performing this? Please point me in the right direction :).
Thanks.
Mike.
HTML Form uses just HTTP POST/PUT methods to upload file to server, you don't have to work/think in this case with HTML FORM, just do HTTP Connection to script and send data in required format
Read more:
http://www.jguru.com/faq/view.jsp?EID=62798
http://www.theserverside.com/news/1365153/HttpClient-and-FileUpload
http://www.java-tips.org/other-api-tips/httpclient/how-to-use-multipart-post-method-for-uploading.html
http://www.prasannatech.net/2009/03/java-http-post-file-upload-server.html
http://www.google.cz/search?aq=f&sourceid=chrome&ie=UTF-8&q=java+http+post+upload