jaunos Posted May 6, 2012 Report Share Posted May 6, 2012 HiI am trying to write an application in java with apache http client, which uses utorrent webui. I have tired the GET and the POST method. It seems the get method wont work with that site because when I put the torrent url eg.: http://site.com/torrents.php?action=download&id=1000382 to the s parameter the application doesnt add the torrent and chrome either.The app gives back the {"build":26773}, which means there is no problem, but the torrent isnt added.HttpGet get_torr = new HttpGet("http://localhost:8080/gui/?token=" + this.mytoken + "&action=add-url" + "&s=" + torrenturl);I don't know why but didn't work with get. I added the cookies properly etc.Then I tried it with the POST method. My POST is :// LOGIN List <NameValuePair> formparams = new ArrayList <NameValuePair>(); formparams.add(new BasicNameValuePair("user", "myusername")); formparams.add(new BasicNameValuePair("pass", "mypassword")); HttpPost post_nc = new HttpPost("http://site.com/login.php"); post_nc.setEntity(new UrlEncodedFormEntity(formparams, HTTP.UTF_8)); HttpResponse resp_post_nc = client.execute(post_nc, localContext); resp_post_nc.getEntity().writeTo(System.out);// LOGIN END// DOWNLOAD AND SAVE A TORRENT HttpPost gettorrent = new HttpPost("http://site.com/torrents.php?action=download&id=1000382"); HttpResponse responsedtorrent = client.execute(gettorrent,localContext); ReadableByteChannel rbc = Channels.newChannel(responsedtorrent.getEntity().getContent()); FileOutputStream fos = new FileOutputStream("testtorrent.torrent"); fos.getChannel().transferFrom(rbc, 0, 1 << 24);// DOWNLOAD AND SAVE A TORRENT END// TRY TO ADD HttpPost post_torr = new HttpPost("http://localhost:8080/gui/?token=" + this.mytoken + "&action=add-file"); FileBody bin = new FileBody(new File("testtorrent.torrent")); StringBody comment = new StringBody("testtorrent.torrent"); MultipartEntity reqEntity = new MultipartEntity(); reqEntity.addPart("file", bin); reqEntity.addPart("torrent_file", comment); post_torr.setEntity(reqEntity); HttpResponse resp_post_torr = client.execute(post_torr, localContext);// TRY TO ADD END System.out.println(resp_post_torr.toString()); resp_post_torr.getEntity().writeTo(System.out);I think the multipart POST is wrong, but I dont know where. I got this error message:{"build":26773,"error": "Can't add torrent: torrent is not valid bencoding"}I downloaded a BEncode Editor I opened the two files, what I downloaded and what the app and it is the same. When I add manually(double-click on it) the downloaded torrent file by app, it works and the download starting. So the downloaded torrent file isn't corrupt I think.How can I add properly .torrent with the POST method?Thanks,jaunos Link to comment Share on other sites More sharing options...
Ultima Posted May 8, 2012 Report Share Posted May 8, 2012 ... Are you trying to add a .torrent file, or are you trying to add a URL? It's not clear what you're trying to do here, and you seem to be mixing the two requests. Link to comment Share on other sites More sharing options...
jaunos Posted May 8, 2012 Author Report Share Posted May 8, 2012 I tried both but none of them worked yet with that particular site. The site needs user to be authenticated. Now I am trying to figure out how to add a .torrent file with the multipart POST method. The GET url adding method didnt work with the browser too, that's why I am trying the POST method, because I think it could be work just I am doing something wrong.The app logs in to that site, then downloads the torrent, - I think this is working because when I click the downloaded torrent file the utorrent opens it and starts download - then it tries to send to webui - this gives me the beencode error.Sorry for bad English. Link to comment Share on other sites More sharing options...
jaunos Posted May 8, 2012 Author Report Share Posted May 8, 2012 figured out.problem was here...... **reqEntity.addPart("file", bin); // it is wrong reqEntity.addPart("torrent_file", comment); // StringBody is not necessary.....**and it needs to be reqEntity.addPart("torrent_file", bin);silly me.. Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.