当前位置: 首页>>代码示例>>Java>>正文


Java HttpConnection.getResponseCode方法代码示例

本文整理汇总了Java中javax.microedition.io.HttpConnection.getResponseCode方法的典型用法代码示例。如果您正苦于以下问题:Java HttpConnection.getResponseCode方法的具体用法?Java HttpConnection.getResponseCode怎么用?Java HttpConnection.getResponseCode使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在javax.microedition.io.HttpConnection的用法示例。


在下文中一共展示了HttpConnection.getResponseCode方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: findType

import javax.microedition.io.HttpConnection; //导入方法依赖的package包/类
/**
 * Finds the type of the content in this Invocation.
 * <p>
 * The calling thread blocks while the type is being determined.
 * If a network access is needed there may be an associated delay.
 *
 * @return the content type.
 *          May be <code>null</code> if the type can not be determined.
 *
 * @exception IOException if access to the content fails
 * @exception IllegalArgumentException if the content is accessed via
 *  the URL and the URL is invalid
 * @exception SecurityException is thrown if access to the content
 *  is required and is not permitted
 */
String findType() throws IOException, SecurityException {
    String type = null;
    Connection conn = openPrim(true);
    if (conn instanceof ContentConnection) {
    	if( conn instanceof HttpConnection ){
    		HttpConnection hc = (HttpConnection)conn;
         hc.setRequestMethod(HttpConnection.HEAD);
	
         // actual connection performed, some delay...
         if (hc.getResponseCode() != HttpConnection.HTTP_OK)
         	return null;
    	}
    	
        type = ((ContentConnection)conn).getType();
        conn.close();

        if (type != null) {
            // Check for and remove any parameters (rfc2616)
            int ndx = type.indexOf(';');
            if (ndx >= 0) {
                type = type.substring(0, ndx);
            }
            type = type.trim();
            if (type.length() == 0) {
                type = null;
            }
        }
    }

    return type;
}
 
开发者ID:mozilla,项目名称:pluotsorbet,代码行数:47,代码来源:ContentReader.java

示例2: setURL

import javax.microedition.io.HttpConnection; //导入方法依赖的package包/类
public void setURL(String host, String url) throws IOException {
    closeStream();

    conn = (HttpConnection) Connector.open("http://" + host + url, Connector.READ, true);
    conn.setRequestMethod(HttpConnection.GET);
    if (conn.getResponseCode() == HttpConnection.HTTP_OK) {
        in = conn.openInputStream();

        // Retrieve MIME type of the stream
        contentType = conn.getHeaderField("Content-Type");
    } else {
        throw new IOException("Unexpected HTTP return: " + conn.getResponseCode());
    }
}
 
开发者ID:cli,项目名称:rdio,代码行数:15,代码来源:ChunkStream.java

示例3: getTime

import javax.microedition.io.HttpConnection; //导入方法依赖的package包/类
public long getTime() throws IOException {
    HttpConnection conn = (HttpConnection) Connector.open(url);
    try {
        int rc = conn.getResponseCode();
        if (rc == 200) {
            BufferedReader reader = new BufferedReader(conn.openInputStream());
            return Long.parseLong(reader.readLine());
        } else {
            throw new RuntimeException("Server returned " + rc);
        }
    } finally {
        conn.close();
    }
}
 
开发者ID:fclairamb,项目名称:tc65lib,代码行数:15,代码来源:HttpBodyTimeClient.java

示例4: urlToProperties

import javax.microedition.io.HttpConnection; //导入方法依赖的package包/类
protected Properties urlToProperties(String url) throws IOException {
        HttpConnection conn = (HttpConnection) Connector.open(url);
        conn.setRequestProperty("user-agent", userAgent);
        try {
            int rc = conn.getResponseCode();

            // If we get a wrong HTTP response code, we might as well just stop trying
            if (rc != HttpConnection.HTTP_OK) {
                if (Logger.BUILD_NOTICE) {
                    Logger.log(this + ".urlToProperties: Could not fetch " + url + " !", true);
                }
                cancel();
            }
            BufferedReader reader = new BufferedReader(conn.openInputStream());

            {
                String line;
                Properties props = new Properties();
                while ((line = reader.readLine()) != null) {
                    int p = line.indexOf(":");
                    if (p != -1) {
                        String name = line.substring(0, p);
                        String value = line.substring(p + 1).trim();
//                        Logger.log(name + " = " + value);
                        props.addProperty(name, value);
                    }
                }
                return props;
            }
        } finally {
            conn.close();
        }
    }
 
开发者ID:fclairamb,项目名称:tc65lib,代码行数:34,代码来源:AutoUpdater.java

示例5: fetch

import javax.microedition.io.HttpConnection; //导入方法依赖的package包/类
public static byte[] fetch(String url) {
	try {
		HttpConnection c = (HttpConnection)Connector.open(url);
		// Execute the request
		int rc = c.getResponseCode();
           if (rc != HttpConnection.HTTP_OK) {
               throw new IOException("HTTP response code " + rc + " when fetching " + url); 
           }
           // Read the data
           return readConnection(c);
	} catch (Exception e) {
		e.printStackTrace();
		return null;
	}
}
 
开发者ID:wvdschel,项目名称:twitsy,代码行数:16,代码来源:HttpFetcher.java

示例6: handleResponse

import javax.microedition.io.HttpConnection; //导入方法依赖的package包/类
private void handleResponse(final HttpConnection connection) throws IOException {
    int responseCode = connection.getResponseCode();
    responseProperties = HttpRequestProperties.HttpResponsePropertyFactory(connection);
    long responseLength = connection.getLength();

    if (responseLength > TransportService.PAYLOAD_SIZE_REPORTING_THRESHOLD) {
        Logger.log("recv", "size " + responseLength);
    }

    if(responseCode >= 200 && responseCode < 300) {
        //It's all good, message was a success.
        this.setResponseCode(responseCode);
        this.setStatus(TransportMessageStatus.SENT);

        //Wire up the input stream from the connection to the message.
        this.setResponseStream(new InputStreamC(connection.openInputStream(), responseLength, this.getTag()) {
            /* (non-Javadoc)
             * @see java.io.InputStream#close()
             */
            public void close() throws IOException {
                //Some platforms were having issues with the connection close semantics
                //where it was supposed to close the connection when the stream was closed,
                //so we'll go ahead and move this here.
                connection.close();

                super.close();
            }
        });
    } else {
        this.setStatus(TransportMessageStatus.FAILED);
        this.setResponseCode(responseCode);

        //We'll assume that any failures come with a message which is sufficiently
        //small that they can be fit into memory.
        byte[] response = StreamUtil.readFully(connection.openInputStream());
        connection.close();
        String reason = responseCode + ": " + new String(response);
        reason = PropertyUtils.trim(reason, 400);
        this.setFailureReason(reason);
    }
}
 
开发者ID:dimagi,项目名称:commcare-j2me,代码行数:42,代码来源:AuthenticatedHttpTransportMessage.java

示例7: run

import javax.microedition.io.HttpConnection; //导入方法依赖的package包/类
/**
 * {@inheritDoc}
 */
public void run() {
  try {
    // Visit the original download URL and read the JAD;
    // if the MIDlet-Version has changed, invoke the callback.
    String url = Build.DOWNLOAD_URL;
    String applicationVersion = getApplicationVersion();
    String userAgent = getUserAgent();
    String language = getLanguage();
    for (int redirectCount = 0; redirectCount < 10; redirectCount++) {
      HttpConnection c = null;
      InputStream s = null;
      try {
        c = connect(url);
        c.setRequestMethod(HttpConnection.GET);
        c.setRequestProperty("User-Agent", userAgent);
        c.setRequestProperty("Accept-Language", language);

        int responseCode = c.getResponseCode();
        if (responseCode == HttpConnection.HTTP_MOVED_PERM
            || responseCode == HttpConnection.HTTP_MOVED_TEMP) {
          String location = c.getHeaderField("Location");
          if (location != null) {
            url = location;
            continue;
          } else {
            throw new IOException("Location header missing");
          }
        } else if (responseCode != HttpConnection.HTTP_OK) {
          throw new IOException("Unexpected response code: " + responseCode);
        }
        s = c.openInputStream();
        String enc = getEncoding(c);
        Reader reader = new InputStreamReader(s, enc);
        final String version = getMIDletVersion(reader);
        if (version == null) {
          throw new IOException("MIDlet-Version not found");
        } else if (!version.equals(applicationVersion)) {
          Application application = Application.getApplication();
          application.invokeLater(new Runnable() {
            public void run() {
              mCallback.onUpdate(version);
            }
          });
        } else {
          // Already running latest version
        }
      } finally {
        if (s != null) {
          s.close();
        }
        if (c != null) {
          c.close();
        }
      }
    }
  } catch (Exception e) {
    System.out.println(e);
  }
}
 
开发者ID:google,项目名称:google-authenticator,代码行数:63,代码来源:UpdateTask.java

示例8: flush

import javax.microedition.io.HttpConnection; //导入方法依赖的package包/类
public void flush() throws TTransportException {
// Extract request and reset buffer
    byte[] data = requestBuffer_.toByteArray();
    requestBuffer_.reset();

    try {
        // Create connection object
        HttpConnection connection = (HttpConnection)Connector.open(url_);

        // Timeouts, only if explicitly set
        if (connectTimeout_ > 0) {
        //  XXX: not available
        //  connection.setConnectTimeout(connectTimeout_);
        }   
        if (readTimeout_ > 0) {
        //  XXX: not available
        //  connection.setReadTimeout(readTimeout_);
        }

        // Make the request
        connection.setRequestMethod("POST");
        connection.setRequestProperty("Content-Type", "application/x-thrift");
        connection.setRequestProperty("Accept", "application/x-thrift");
        connection.setRequestProperty("User-Agent", "JavaME/THttpClient");

        connection.setRequestProperty("Connection", "Keep-Alive");
        connection.setRequestProperty("Keep-Alive", "5000");
        connection.setRequestProperty("Http-version", "HTTP/1.1");
        connection.setRequestProperty("Cache-Control", "no-transform");


        if (customHeaders_ != null) {
            for (Enumeration e = customHeaders_.keys() ; e.hasMoreElements() ;) {
                String key = (String)e.nextElement();
                String value = (String)customHeaders_.get(key);
                connection.setRequestProperty(key, value);
            }
        }
        // connection.setDoOutput(true);
        //  connection.connect();

        OutputStream os = connection.openOutputStream();
        os.write(data);
        os.close();

        int responseCode = connection.getResponseCode();
        if (responseCode != HttpConnection.HTTP_OK) {
            throw new TTransportException("HTTP Response code: " + responseCode);
        }

        // Read the responses
        inputStream_ = connection.openInputStream();

    } catch (IOException iox) {
        System.out.println(iox.toString());
        throw new TTransportException(iox);
    }
}
 
开发者ID:YinYanfei,项目名称:CadalWorkspace,代码行数:59,代码来源:THttpClient.java


注:本文中的javax.microedition.io.HttpConnection.getResponseCode方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。