0

I'm trying to send a video url in a http POST request but it's not working for me, I think I've (almost?) the necessary code to make it work, or else I'm missing something very simple?

public void postVideoURL() throws IOException {

    String encodedUrl = URLEncoder.encode("http://video.ted.com/talks/podcast/DavidBrooks_2011.mp4", "UTF-8");
    URL obj = new URL("http://10.50.0.105:8060/launch/dev?url="+encodedUrl);

    HttpURLConnection con = (HttpURLConnection) obj.openConnection();

    //add request header
    con.setRequestMethod("POST");

    // Send post request
    con.setDoOutput(true);
    OutputStreamWriter wr = new OutputStreamWriter(con.getOutputStream());
    System.out.println(con.getResponseCode());
    System.out.println(con.getResponseMessage());
    wr.flush();
    wr.close();

    wr.write("");
    }

Any tips to lead me to the right direction?

Here is what I'm trying to do but in C#

using System.Net;
using System.Web;

class Program
{
    static void Main()
    {
        string rokuIp     = "192.168.0.6";
        string channelId  = "dev";
        string videoUrl   = "http://video.ted.com/talks/podcast/DavidBrooks_2011.mp4";

        // Note that the query string parameters should be url-encoded
        string requestUrl = $"http://{rokuIp}:8060/launch/{channelId}?url={HttpUtility.UrlEncode(videoUrl)}";

        using (var wc = new WebClient())
        {
            // POST the query string with no data
            wc.UploadString(requestUrl, "");
        }
    }
}

The following Post command to use in terminal works, this is essentially what I want to do, but in Java: curl -d "" "http://10.50.0.46:8060/launch/12?url=http%3A%2F%2Fvideo.ted.com%2Ftalks%2Fpodcast%2FDavidBrooks_2011.mp4"

  • define *not working* – njzk2 May 17 '16 at 14:17
  • The app starts but the video is not sent to the app, no errors are showing up in the code, i'm sending the video to an app on a tv. (i'm using connectsdk as the android app) – UnknownDistance May 17 '16 at 14:26
  • You are writing a video url in a get request as parameter. That is ok if the server wants it that way. – greenapps May 17 '16 at 14:43
  • @greenapps the video url can be sent through the terminal with a http post using - [curl -d "" "http://10.50.0.46:8060/launch/dev?url=http%3A%2F%2Fvideo.ted.com%2Ftalks%2Fpodcast%2FDavidBrooks_2011.mp4"'] So I assume I can do it this way through java? – UnknownDistance May 17 '16 at 14:48
  • This way? Sorry but i see no curl post. And you are not explaining why you put that video url as GET parameter in the url and also want to POST it yet another time. – greenapps May 17 '16 at 15:25
  • Not sure why the command isn't showing, but if you hover over it, it'll show the full command.. sorry where am I putting it as GET? i'm just trying to understand it to work, I'm trying to POST it but guess failing badly? – UnknownDistance May 17 '16 at 15:35
  • `new URL("http://10.50.0.105:8060/launch/dev?url=URLEncoder.encode(http://video.ted.com/talks/podcast/DavidBrooks_2011.mp4)");`. There are some typos. You mean `new URL("http://10.50.0.105:8060/launch/dev?url=" + URLEncoder.encode("http://video.ted.com/talks/podcast/DavidBrooks_2011.mp4"));` – greenapps May 17 '16 at 15:38
  • `System.out.println(con.getResponseCode()); System.out.println(con.getResponseMessage());`. There are two of the three responses. But you should put those lines after wr.close(). What do they print? – greenapps May 17 '16 at 16:42

1 Answers1

0

You are never writing anything to the output stream. You have to call wr.write() to write your data to the stream.

Also, you can't encode the URL like that inside the String. You need to concatenate the two Strings together after you've encoded the url separately. Like this:

String encodedUrl = URLEncoder.encode("http://video.ted.com/talks/podcast/DavidBrooks_2011.mp4");
URL obj = new URL("http://10.50.0.105:8060/launch/dev?url="+encodedUrl);
NoChinDeluxe
  • 3,446
  • 1
  • 16
  • 29
  • Cannot resolve method write? Do I need something else to make this work? – UnknownDistance May 17 '16 at 14:43
  • You have to supply the data to write. So you should be calling `wr.write("some data you want to POST to the server");` – NoChinDeluxe May 17 '16 at 14:48
  • But what I want to POST is the url of obj, do I shove the whole url into wr.write()? Putting the url into write(urlhere) isn't working – UnknownDistance May 17 '16 at 15:05
  • So according to that C# code you posted, you should be POSTing an empty query string to the url. So in that case just do `wr.write("");` Also, you can't encode the URL inside a String like that in Java. You would need to concatenate the encoded URL onto the other part of the URL. See my edit above on how to do this. – NoChinDeluxe May 17 '16 at 15:19
  • Updated original posts with your recommendations, video still won't send not sure whats going on... – UnknownDistance May 17 '16 at 15:44
  • `video still won't send`. What do you mean? You are not sending a video to begin with. And the video url is send as parameter in that GET request. So now we would like to know what the server is receiving. What kind of server? – greenapps May 17 '16 at 15:48
  • I'm triyng to send the video url to a video player on a Roku app i've made, when the video player receives the video url it should play it. Sorry if i'm being unclear – UnknownDistance May 17 '16 at 15:52
  • Well what does that video player receive instead? What kind of errors does it produce? You should add android java code to read the response code and response message and responge page that the video player sends. – greenapps May 17 '16 at 15:53
  • There is no errors with the video player as it can work fine POSTing a url from the terminal to it – UnknownDistance May 17 '16 at 15:55
  • If it goes fine there are no errors. But now it does not work so there will be errors. You are not reading those responses so you will never know what happens. – greenapps May 17 '16 at 15:56
  • Post the curl terminal code in your post. Not in a comment. So we can read it. – greenapps May 17 '16 at 15:59
  • I'm not sure how to trace the responses, I know you can do it with wireshark, no idea how though – UnknownDistance May 17 '16 at 16:01
  • If you just need to POST a url to your roku app, then you should be sending that String in through the write() method like this: `wr.write("the url to the video as a string");` – NoChinDeluxe May 17 '16 at 16:55