Hi guys,
I am new in this field and this is a problem when I want to construct
a http request and send it.
Learn form GWT that I can send a request as follows:
1. String url =
2. RequestBuilder builder = new RequestBuilder
(RequestBuilder.POST,URL.encode(url));
3. builder.setHeader(Content-Type, application/x-www-form-
urlencoded);
4. String form = name=ge=13;
5. builder.sendRequest(form,newRequestCallback() {
6. .
7. });
So,what if there is a file input field and the contetn type is
multipart/form-data ? so according to the standard HTTP header format
when sending this kind of form,my solution is as follows :
public void onModuleLoad() {
final TextBox text = new TextBox();
final Button btn = new Button(Http Request Test);
final Label lbl = new Label();
RootPanel.get().add(text);
RootPanel.get().add(btn);
RootPanel.get().add(lbl);
btn.addClickHandler(new ClickHandler()
{
public void onClick(ClickEvent event) {
btn.setEnabled(false);
String url =
http://export.writer.zoho.com/remotedoc.im?
apikey=e13ac14e4b7bd43f8bae584105d39318output=editor;
String BOUNDARY =
---7d4a6d158c9;
url = URL.encode(url);
RequestBuilder builder = new
RequestBuilder(RequestBuilder.POST,
url);
StringBuffer sb = new StringBuffer();
sb = sb.append(--);
sb = sb.append(BOUNDARY);
sb = sb.append(\r\n);
sb = sb.append(Content-Disposition: form-data;
name=\Edit\\r\n\r
\n);
sb = sb.append(URL.encode(divbutton));
sb = sb.append(\r\n);
sb = sb.append(--);
sb = sb.append(BOUNDARY);
sb = sb.append(\r\n);
sb = sb.append(Content-Disposition: form-data;
name=\content\;
filename=\1.txt\\r\n);
sb = sb.append(Content-Type:
application/octet-stream\r\n\r\n);
byte[] data = sb.toString().getBytes();
byte[] end_data = (\r\n-- + BOUNDARY +
--\r\n).getBytes();
byte[] fileContent=; // the file content
/*
//Logic to write the data to http header,it may
like this but not
support by request builder:
output = builder.openOutputStream();
output.write(data);
output.write(fileContent);
output.write(end_data);
*/
builder.setHeader(Content-Type,
multipart/form-data;
charset=utf-8;boundary=+BOUNDARY);
builder.setHeader(Content-Length,
String.valueOf(data.length +
fileContent.length + end_data.length));
try {
builder.send();
} catch (RequestException e) {
e.printStackTrace();
}
}
}
);
}
1. The code will fail in compilation ,ERROR:The method getBytes() is
undefined for the type String ? Why does this error happen?
2.How can I add the byte[] to the related field in the http header?
Thanks very much !!!
--
You received this message because you are subscribed to the Google Groups
Google Web Toolkit group.
To post to this group, send email to google-web-tool...@googlegroups.com.
To unsubscribe from this group, send email to
google-web-toolkit+unsubscr...@googlegroups.com.
For more options, visit this group at
http://groups.google.com/group/google-web-toolkit?hl=en.