0

我正在尝试在 http POST 请求中发送视频 url,但它对我不起作用,我想我(几乎?)有必要的代码让它工作,或者我错过了一些非常简单的东西?

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("");
    }

有什么提示可以引导我走向正确的方向吗?

这是我想要做的,但在 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, "");
        }
    }
}

以下在终端中使用的 Post 命令有效,这基本上是我想要做的,但在 Java 中: curl -d "" " http://10.50.0.46:8060/launch/12?url=http%3A%2F %2Fvideo.ted.com%2Ftalks%2Fpodcast%2FDavidBrooks_2011.mp4 "

4

1 回答 1

0

您永远不会向输出流写入任何内容。您必须调用wr.write()才能将数据写入流。

此外,您不能像在字符串中那样对 URL 进行编码。分别对 url 进行编码后,您需要将两个字符串连接在一起。像这样:

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);
于 2016-05-17T14:30:59.913 回答