📄 myhttpconnection.java
字号:
import java.io.*;
import javax.microedition.io.*;
public class MyHttpConnection
{
//定义连接服务器的接口
public interface Callback
{
void prep_Request( String originalURL, HttpConnection http_Con) throws IOException;
}
public static HttpConnection connect( String url ) throws IOException
{
return connect( url, null );
}
//定义连接服务器的静态方法
public static HttpConnection connect(String url, Callback call ) throws IOException
{
HttpConnection http_Con = null;
String original_URL = url;
while( url != null )
{
//建立并打开连接
http_Con = (HttpConnection)Connector.open( url );
if( call != null )
{
call.prep_Request( original_URL, http_Con );
}
//获得连接响应代码
int rcode = http_Con.getResponseCode();
//根据返回的连接响应代码,得到请求的具体路径
switch( rcode )
{
case HttpConnection.HTTP_MOVED_PERM:
case HttpConnection.HTTP_MOVED_TEMP:
case HttpConnection.HTTP_SEE_OTHER:
case HttpConnection.HTTP_TEMP_REDIRECT:
url = http_Con.getHeaderField( "Location" );
if( url != null && url.startsWith("/*") )
{
StringBuffer buff = new StringBuffer();
buff.append( "http://" );
buff.append( http_Con.getHost() );
buff.append( ':' );
buff.append( http_Con.getPort() );
buff.append( url );
url = buff.toString();
}
http_Con.close();
break;
default:
url = null;
break;
}
}
return http_Con;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -