0

在 c# 中,我能够做到这一点:

        HttpWebRequest request = WebRequest.Create(address) as HttpWebRequest;
        request.Method = "POST";
        request.ContentType = "application/octet-stream";

        request.ContentLength = fileToSend.Length;
        request.Timeout = 2000000000;
        using (Stream requestStream = request.GetRequestStream())
        {
            // Send the file as body request.
            requestStream.Write(fileToSend, 0, fileToSend.Length);
            requestStream.Close();
        }

我正在寻找一个类似的对象,我可以在 Java 中使用它来扮演 HttpWebRequest 的角色,它将包括一个 Stream。

谢谢!

编辑:发现这个Using java.net.URLConnection to fire and handle HTTP requests但它似乎太复杂了......希望有一种更简单的方法来做到这一点?

4

1 回答 1

0

我建议您查看 Apache HTTP 客户端 ( http://hc.apache.org/httpclient-3.x/features.html )。

如果您只需要从某些资源中查询信息,那么标准 JDK 就足够了:

java.net.URL url = new java.net.URL("http://foo.bar");
URLConnection conn = url.openConnection();
//conn.getOutputStream() to place information
//conn.getInputStream() to read resource
于 2013-04-09T11:48:58.767 回答