For a couple of days I work to chance information with web services using javascript at Indesign.
The first try was using HttpConnection from bridge. In this ocasion the post method dont work fine and I discard this idea.
the second try was use Javascript and this post talk about exacly this code.
Primarilly, I start using a well know code you can find at google. A function called getUrl(). this implementation was a little slow because the code get just one byte at once when it read the answer from socket. You can solve this at this code changing a couple lines but it’s just a function. without any object orientation facility.
The code above use OO and represent my initial test. Feel free for use, reproduce and changing this code. If you can give me your opinio… I’m glady with it.
function httpconn(method,ssl){
// todo: implements test for set of methods
this._coveredMethods = [“GET”,”POST”];
this.method = (method == undefined)?”GET”:method;
this.port = (ssl == undefined)?”80″:ssl;
this.init();
}
httpconn.prototype.setUrl=function(url){
this.url = url;
this._configureSocket();
return this;
}
httpconn.prototype.init=function(){
this.socket = new Socket;
this.socket.timeout = 15;
this._parsedparam = new Object();
this.params = new Object();
this.contentType = “Content-Type: application/x-www-form-urlencoded; charset=UTF-8”;
this.extraHeader = “”;
}
httpconn.prototype.clean=httpconn.prototype.init;
httpconn.prototype._configureSocket=function(){
var _urlsplit = this.url.replace(/[http|https]*[://]*/,””);
_urlsplit = _urlsplit.split(“/”);
this.host = _urlsplit[0];
if (_urlsplit[1] != undefined ){
_urlsplit.shift();
this.path = “/”+ _urlsplit.join(“/”);
}else{
this.path = “/”;
}
return this;
}
httpconn.prototype._parseParam=function(){
var _parsedparam = “”;
for (i in this.params){
_parsedparam = _parsedparam + ( ( (_parsedparam.length!=0)?”&”:””)+i+”=”+String( this.params[i]) );
}
this._parsedparam.str = _parsedparam;
this._parsedparam.length = _parsedparam.length;
return this;
}
httpconn.prototype.addParam=function(name, value){
this.params[name] =value;
return this;
}
httpconn.prototype.setContentType=function(ct){
this.contentType = ct+”n”;
}
httpconn.prototype.addHeader=function(headerLine){
this.extraHeader+=headerLine+”n”;
}
httpconn.prototype.request=function(){
var _request = this.method;if (this.method==”POST”)
_request +=” “+this.path+” HTTP/1.1n”;
else
_request +=” “+this.path+”?”+this._parsedparam.str+” HTTP/1.1n”;_request +=”Host: “+this.host+”:”+this.port+” n” +
“User-Agent: Arizona Systemsn” +
“Accept: */*n”+
this.contentType;
_request+=this.extraHeader;
if (this.method==”POST”)
_request+=”Content-Length: “+this._parsedparam.length+”n”;_request+=”Connection: keep-alivernrn”;
alert(“host:”+this.host+”n method:”+this.method+”n port:”+this.port+”n dados:”+this._parsedparam.str);
alert(“request:”+_request);
this.socket.listen(this.port);
this.socket.open(this.host+”:”+this.port);
this.socket.write(_request);
this.socket.write(this._parsedparam.str);
// this.clean();
return this.socket.read();
}
To use this object you can make like this
//http = new httpconn(“POST”);
//var retorno = http.setUrl(“http://192.168.0.21/ws/teste.php”).addParam(‘teste’,’1′).addParam(‘nome’,’ivo’)._parseParam().request();
//alert(retorno);
Hope you enjoy.
Tiago - lemons
16/12/2009 — 12:52
Animal bixo !!! gratz !!!
Danilo
16/12/2009 — 13:03
Great man, I hope that your ideas are always fertile for ever… XD
Brendon
11/01/2010 — 04:52
Mate, what a great help! Thanks a ton for publishing this.
kaiser shahid
18/01/2010 — 18:09
great work! i just wrote a set of request/response classes based on some of the code here.
admin
19/01/2010 — 04:51
Thanks man. Can u share the code you produce?
Did you observe we(adobe developer) don’t have much information.
we need share more code/informations…
[]’s
bruce.zhao
08/12/2010 — 18:52
well done. thanks so much.
autumnator
22/04/2015 — 20:31
Curious, what (or how) didn’t work for Bridge’s HttpConnection POST method? It worked for us, though by the time I worked for company we used CS5 and now CS6. Might have been CS4 or older at some point, not sure.
All we had to do (compared to a GET) was add the following before executing the request:
var http = new HttpConnection(“some URL”) ;
http.method = ‘POST’;
http.request = thePostBodyContentHere;
http.mime = ‘application/json’; //or appropriate MIME type
By the way, thanks for sharing this alternative approach.
autumnator
22/04/2015 — 20:44
By the way, where did you find this “well known” code via Google? You still have the actual link or search query?
autumnator
22/04/2015 — 20:55
Also, are those really “n” at the end of the line in the code sample or should it be “\n”?
autumnator
23/04/2015 — 19:41
FYI, one can find another HTTP wrapper library around sockets in this project: https://github.com/debrouwere/Extendables/tree/master/core-packages/http/doc