gpt4 book ai didi

java - 如何在J2ME 中使用HTTP 范围 header ?

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:17:23 26 4
gpt4 key购买 nike

  1. 是否可以在 HTTP GET 请求中使用 HTTP 中的范围 header ?
  2. 如果是,那么如何?

我只想从服务器下载指定字节的字节。即如果我想下载从 0 到 255 的字节。

最佳答案

查看此示例代码,

      HttpConnection connection = null;
InputStream inputstream = null;
try
{
connection = (HttpConnection) Connector.open(url); // Enter your URL here.
//HTTP Request
connection.setRequestMethod(HttpConnection.GET);
connection.setRequestProperty("Content-Type","//text plain");
connection.setRequestProperty("Connection", "close");
// HTTP Response
System.out.println("Status Line Code: " + connection.getResponseCode());
System.out.println("Status Line Message: " + connection.getResponseMessage());
if (connection.getResponseCode() == HttpConnection.HTTP_OK)
{
System.out.println(
connection.getHeaderField(0)+ " " + connection.getHeaderFieldKey(0));
System.out.println(
"Header Field Date: " + connection.getHeaderField("date"));
String str;
inputstream = connection.openInputStream();
int length = (int) connection.getLength();
if (length != -1)
{
byte incomingData[] = new byte[length];
inputstream.read(incomingData);
str = new String(incomingData);
}
else
{
ByteArrayOutputStream bytestream =
new ByteArrayOutputStream();
int ch;
while ((ch = inputstream.read()) != -1)
{
bytestream.write(ch);
}
str = new String(bytestream.toByteArray());
bytestream.close();
}
System.out.println(str);
}
}
catch(IOException error)
{
System.out.println("Caught IOException: " + error.toString());
}
finally
{
if (inputstream!= null)
{
try
{
inputstream.close();
}
catch( Exception error)
{
/*log error*/
}
}
if (connection != null)
{
try
{
connection.close();
}
catch( Exception error)
{
/*log error*/
}

关于java - 如何在J2ME 中使用HTTP 范围 header ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5340122/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com