The modified classes attached appear to work correctly.
The problem is that form data in the entity is expected to be "url encoded"
-- that is, transformed to sequences like %2F instead of / -- the example
you provided skips this step. By using the Form abstraction on both sides
(or just ensuring the client properly url-encodes the data) you will get the
correct results. All browsers I know of will url-encode a POSTed form
submission.
So the client pattern looks like this:
final Form form = new Form();
form.add
("json","{\"zip\":1,\"tel\":\"1\",\"street\":\"1\",\"city\":\"北京\"}");
Representation jsonRepr = form.getWebRepresentation(
CharacterSet.UTF_8);
request.setEntity(jsonRepr);
The data enroute looks like this:
json=%7B%22zip%22%3A1%2C%22tel%22%3A%221 ... etc.
... and it comes back correctly from the (unmodified) server.
I think the difference between the example I constructed earlier and your
JSON use case is that the browser automatically does this step, but your
JSON client skipped it.
- R
On 1/27/08, cleverpig <[EMAIL PROTECTED]> wrote:
>
> Great!
> But now,Rob,do you have time to take a look?
>
> On Jan 25, 2008 12:56 PM, Rob Heittman <[EMAIL PROTECTED]> wrote:
> > Thanks for this continued diligence ... I am travelling right now, but
> will
> > have a look at it Friday morning (US Central time) - that is, about 8
> hrs
> > from now.
> >
> >
> > On 1/24/08, cleverpig <[EMAIL PROTECTED]> wrote:
> > > Hi,Rob.I made a pair of test program that can prove my
> consideration,pls
> > try on:
> > >
> > >
> >
>
>
>
> --
> cleverpig
> Location: Beijing
> Address: Room 4018,No.A2 South Avenue Fuxingmen Beijing,P.R.China
> Zipcode: 100031
> Phone: 010-66415588-1113
> MSN: [EMAIL PROTECTED]
> QQ: 149291732
> Skepe: cleverpigatmatrix
> My Facebook ID:cleverpig
> My Blog: hihere.sohu.com
> My Tags: del.icio.us/cleverpig
> My Twitter: twitter.com/cleverpig
> My Organization: www.beijing-open-party.org
> 一些值得关注的唧歪:
> http://jiwai.de/t/jmatrix/
> http://jiwai.de/t/db4o/
> http://jiwai.de/t/matrix-community/
>
package com.solertium.planningdatabase.container;
import java.io.IOException;
import org.restlet.Client;
import org.restlet.data.CharacterSet;
import org.restlet.data.Encoding;
import org.restlet.data.Form;
import org.restlet.data.Language;
import org.restlet.data.MediaType;
import org.restlet.data.Method;
import org.restlet.data.Preference;
import org.restlet.data.Protocol;
import org.restlet.data.Request;
import org.restlet.data.Response;
import org.restlet.resource.Representation;
public class ZHProblemClient {
public static void main(final String[] argv) throws IOException {
// Prepare the request
final Request request = new Request(Method.POST,
"http://localhost:8182/params");
request.setReferrerRef("http://www.bjinfotech.com");
request.getClientInfo().getAcceptedCharacterSets().add(
new Preference<CharacterSet>(CharacterSet.ALL));
request.getClientInfo().getAcceptedEncodings().add(
new Preference<Encoding>(Encoding.GZIP));
request.getClientInfo().getAcceptedLanguages().add(
new Preference<Language>(Language.ALL));
request.getClientInfo().getAcceptedMediaTypes().add(
new
Preference<MediaType>(MediaType.APPLICATION_JAVASCRIPT));
request.getClientInfo().getAcceptedMediaTypes().add(
new
Preference<MediaType>(MediaType.APPLICATION_XML));
request.getClientInfo().getAcceptedMediaTypes().add(
new Preference<MediaType>(MediaType.TEXT_HTML));
request.getClientInfo().getAcceptedMediaTypes().add(
new Preference<MediaType>(MediaType.TEXT_XML));
request.getClientInfo().getAcceptedMediaTypes().add(
new Preference<MediaType>(MediaType.ALL));
final Form form = new Form();
form.add("json","{\"zip\":1,\"tel\":\"1\",\"street\":\"1\",\"city\":\"å京\"}");
System.out.println("I should be sending
json={\"zip\":1,\"tel\":\"1\",\"street\":\"1\",\"city\":\"å京\"}");
Representation jsonRepr =
form.getWebRepresentation(CharacterSet.UTF_8);
System.out.println("I am sending: "+jsonRepr.getText());
request.setEntity(jsonRepr);
// Handle it using an HTTP client connector
final Client client = new Client(Protocol.HTTP);
final Response response = client.handle(request);
System.out.println(response.getEntity().getCharacterSet().getName());
// Write the response entity on the console
final Representation output = response.getEntity();
output.setCharacterSet(CharacterSet.UTF_8);
System.out.println(output.getText());
}
}package com.solertium.planningdatabase.container;
import org.restlet.Application;
import org.restlet.Component;
import org.restlet.Restlet;
import org.restlet.Router;
import org.restlet.data.CharacterSet;
import org.restlet.data.Form;
import org.restlet.data.MediaType;
import org.restlet.data.Protocol;
import org.restlet.data.Request;
import org.restlet.data.Response;
public class ZHProblemServer {
public static void main(final String[] argv) throws Exception {
final Component component = new Component();
component.getServers().add(Protocol.HTTP, 8182);
component.getClients().add(Protocol.FILE);
final Application application = new
Application(component.getContext()) {
@Override
public Restlet createRoot() {
Router router = new Router(getContext());
router.attach("/params", new
Restlet(getContext()) {
@Override
public void handle(Request request,
Response response) {
Form f = new
Form(request.getEntity());
response.setEntity("<html><body>" + "json: "
+
f.getValues("json") + "<br/>"
+
"</body></html>", MediaType.TEXT_HTML);
response.getEntity()
.setCharacterSet(CharacterSet.UTF_8);
}
});
return router;
}
};
component.getDefaultHost().attach(application);
component.start();
}
}