How to inclide tspecials(<," etc.) in URL for HttpServer? - java

I have implemented simple com.sun.net.httpserver.HttpServer application using one of examples in the internet.
Server get request as expected except for one exception:
if url contains tspecials: " or '<' "/" request doesn't reach the server at all.
Using simple java.net.ServerSocket all works great and request received where all tspesials are encoded, which is great for me, but i prefer use HttpServer.
for example request: http://127.0.0.1/MyApp/Test?var=<xml id=“1“>value</xml>
works for ServerSocket and not working for HttpServer.
Any help will be appreciated.

You need to encode your urls on the client side, using the % notation:
http://127.0.0.1/MyApp/Test?var=%3Cxml+id%3D%271%27%3Evalue%3C%2Fxml%3E
On the server side, you should use URI.getRawQuery() in case the query contains & characters, and décode parameter values with URLDecoder.decode.

Related

Using dropwizard to make RESTful service, the URL can contain a space

I have some code making a #Path for an endpoint:
#Path("/productLine:[a-zA-Z]{1,25}}/cat")
I want to allow two word product lines in the URL. I tried this
#Path("productLine:[a-zA-Z ]{1,25}}/cat")
But the client returns a
HTTP 404 Not Found
when I use a request that has two words, like this:
/services/New Host/cat
can u try with:
#Path("productLine:[a-zA-Z\x0B]{1,25}}/cat")
This worked:
#Path("productLine:[a-zA-Z%20]{1,25}}/cat")
The %20 is the HTTP encoding for space.

Open an authenticated image served by django from java using Apache http client

I Am serving an authenticated image using django. The image is behind a view which require login, and in the end I have to check more things than just the authentication.
Because of a reason to complicated to explain here, I cannot use the real url to the image, but I Am serving it with a custom url leading to the authenticated view.
From java the image must be reachable, to save or display. For this part I use Apache httpclient.
In Apacahe I tried a lot of things (every example and combination of examples...) but can't seem to get it working.
For other parts of the webapp I use django-rest-framwork, which I succesfully connected to from java (and c and curl).
I use the login_reuired decorator in django, which makes the attempt to get to the url redirect to a login page first.
Trying the link and the login in a webviewer, I see the 200 code (OK) in the server console.
Trying the link with the httpclient, I get a 302 Found in the console.... (looking up 302, it means a redirect..)
this is what I do in django:
in urls.py:
url(r'^photolink/(?P<filename>.*)$', 'myapp.views.photolink',name='photolink'),
in views.py:
import mimetypes
import os
#login_required
def photolink(request, filename):
# from the filename I get the image object, for this question not interesting
# there is a good reason for this complicated way to reach a photo, but not the point here
filename_photo = some_image_object.url
base_filename=os.path.basename(filename_photo)
# than this is the real path and filename to the photo:
path_filename=os.path.join(settings.MEDIA_ROOT,'photos',mac,base_filename)
mime = mimetypes.guess_type(filename_photot)[0]
logger.debug("mimetype response = %s" % mime)
image_data = open(path_filename, 'rb').read()
return HttpResponse(image_data, mimetype=mime)
by the way, if i get this working i need another decorator to pass some other tests....
but i first need to get this thing working....
for now it's not a secured url.... plain http.
in java i tried a lot of things... using apache's httpclient 4.2.1
proxy, cookies, authentication negociation, with follow redirects... and so on...
Am I overlooking some basic thing here?...
it seems the login of the website client is not suitable for automated login...
so the problem can be in my code in django....or in the java code....
In the end the problem was, using HTTP authorization.
Which is not by default used in the login_required decorator.
adding a custom decorator that checks for HTTP authorization did the trick:
see this example: http://djangosnippets.org/snippets/243/

Printing the contents of a Restlet web service request

I am implementing a Restful web service using Restlet - I have not found a way to print the content of the HTTP request. I need to check the content of the http request, to get something like this:
POST http://localhost:8080/students
<Student>
<name>Tony<name/>
<age>19<age/>
<Student/>
I am send a custom object the server resource using the following code
ClientResource c = new CLientResource(url);
c.post(student, Student.Class);
I tried to get the HTTP request also with wireshark , I did not find any http requests, I only found TCP connections.
Anybody knows how to print the content of the http request either on client or server side?
You can use the following on the client side :
clientResource.getResponseEntity().getText();
From the javadoc :
Converts the representation to a string value. Be careful when using
this method as the conversion of large content to a string fully
stored in memory can result in OutOfMemoryErrors being thrown.
By the way, HTTP requests are TCP connections.

Access URL with double slash with HttpClient

I'm doing a request on another server like this:
HttpGet req = new HttpGet("http://example.com//foo");
new DefaultHttpClient().execute(req);
However, HttpClient changes example.com//foo to example.com/foo, so the other server (which is not mine) doesn't understand the request.
How can I fix this?
A double-slash is not a legal in the path section of a URI (see RFC2396, sections 3.2, 3.4). The '//' sequence has a defined meaning in the URI, it denotes the authority component (server).
I realize this does not answer your question but the HttpClient is, in fact, behaving in accordance with the HTTP and URL standards. The server your are reading from is not. This appears to be previously reported (https://issues.apache.org/jira/browse/HTTPCLIENT-727) and discarded by the HttpClient team.
It is an illegal URL in fact.
Did you try passing an URI instead of a String?
Did you try / \ \ / ? Or the URL might be equivalent to /default.asp/, /index.html/, /./, /?/, example.com/foo/ or the like.
Otherwise you will need to hack the sources.
I also wanted to do same thing and Apache Http client don't support that.
I managed to get it done using a Netty. I wrote http client using Netty and with that I was able send request with double slash(//) in the path. I used SnoopClient as sample.

UrlEncodedFormEntity Replacement

We have web server which only accepts decoded value from android phone
As example
"http://www.url.com/data/?name=hello World"
returns expected rssult
but when we are trying to use
"http://www.url.com/data/?name=URLEncoder.encode("hello World")"
gives nothing.
We can not change the web service.
But as we all know java only accept encoded url
How can we achieve the goal so that we can send the decoded url as it is to the server
Could you please restructure the question, to make it more clear? also the below statement appear to be incorrect (Assuming you are using Java)
http://www.url.com/data/?name=URLEncoder.encode("hello World")

Categories