簡體   English   中英

使用給定的URI使用Java發送八位字節流

[英]Sending octet stream with java given a uri

在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。

謝謝!

編輯:找到了使用java.net.URLConnection來觸發和處理HTTP請求,但似乎太復雜了……希望有一種更簡單的方法嗎?

我建議您查看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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM