?? httpclient.java
字號:
/*
* Created on 1 mars 2004
*
* To change the template for this generated file go to
* Window - Preferences - Java - Code Generation - Code and Comments
*/
package javaexplorer.http;
/**
* @author veeb7280
*
* To change the template for this generated type comment go to
* Window - Preferences - Java - Code Generation - Code and Comments
*/
import java.net.*;
import java.io.*;
import javaexplorer.util.Log;
import javaexplorer.util.options.*;
public class HttpClient {
/**
* M閠hode utilis閑 pour r閏up閞er des flux partiels de donn閑s
* en utilisant HTTP 1.1 (non dispo en java)
* @param url
* @throws IOException
*/
public static InputStream getInputStream(URL url, long restart){
try{
InputStream input = null;
Socket socket = null;
String authent_proxy = "";
if( isUseProxy(url) ){
socket = new Socket(HttpOptions.getOptions().getProxyHost(),HttpOptions.getOptions().getProxyPort());
}
else{
socket = new Socket(url.getHost(), (url.getPort()== -1 ? url.getDefaultPort():url.getPort()));
}
BufferedWriter out = new BufferedWriter(new
OutputStreamWriter(socket.getOutputStream()));
input = socket.getInputStream();
BufferedReader in = new BufferedReader(new InputStreamReader(input));
//Ecriture de la commande de r閏up en partial
sendPartialGet(out, url, restart);
//Lecture la r閜onse
int code = readResponse(in);
switch(code){
case 206:
return input;
case 407:
//On retente l'envoi
sendPartialGet(out, url, restart);
int code2 = readResponse(in);
if(code2 == 206) return input;
default :
socket.close();
return null;
}
}catch( Exception e){
javaexplorer.util.Log.addError(e);
return null;
}
}
public static void sendPartialGet(BufferedWriter out, URL url, long restart) throws IOException{
StringBuffer sb = new StringBuffer();
sb.append("GET ").append(url.getFile()).append(" HTTP/1.1\n");
sb.append("Host: " + url.getHost()+"\n");
sb.append("Range: bytes=").append(restart).append("-\n");
if( HttpOptions.getOptions().isUseProxy()){
sb.append(getBasicProxyAuth()).append("\n");
}
//Fin de flux
sb.append("\n");
Log.addDebug("Requete :\n" + sb.toString());
out.write(sb.toString());
Log.addDebug("requete envoyee");
out.flush();
}
/**
* Envoi d'une requete sur le flux
* On lit la r閜onse et on retourne le code retourn
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -