PDA

View Full Version : Http GET, java, socket


sblantipodi
24-11-2006, 20:01
salve,
SocketConnection client = (SocketConnection) Connector.open("socket://dpsoftware.org:80");
InputStream is = client.openInputStream();
OutputStream os = client.openOutputStream();
// send something to server
os.write("GET / HTTP/1.0\n\n".getBytes());
// read server response
int c = 0;
int val=0;
int cont=0;
String upd = "";

while((c = is.read()) != -1) {
// do something with the response
upd += (char) c;

}
System.out.print(upd);
// close streams and connection
is.close();
os.close();
client.close();

QUESTA SOCKET al posto di restituire l'index.html del mio dominio, restituisce l'index.html del mio registrar/hoster :mad:

Devo arrabbiarmi? O posso risolvere la cosa?

dupa
24-11-2006, 20:03
salve,
SocketConnection client = (SocketConnection) Connector.open("socket://dpsoftware.org:80");
InputStream is = client.openInputStream();
OutputStream os = client.openOutputStream();
// send something to server
os.write("GET / HTTP/1.0\n\n".getBytes());
// read server response
int c = 0;
int val=0;
int cont=0;
String upd = "";

while((c = is.read()) != -1) {
// do something with the response
upd += (char) c;

}
System.out.print(upd);
// close streams and connection
is.close();
os.close();
client.close();

QUESTA SOCKET al posto di restituire l'index.html del mio dominio, restituisce l'index.html del mio registrar/hoster :mad:

Devo arrabbiarmi? O posso risolvere la cosa?

Sicuro che questo sia giusto?

Connector.open("socket://dpsoftware.org:80");

sblantipodi
24-11-2006, 20:08
Sicuro che questo sia giusto?

Connector.open("socket://dpsoftware.org:80");

Certo :( il mio dominio e' www.dpsoftware.org
stesso problema sia con www che senza ovviamente :(

dupa
24-11-2006, 20:11
Certo :( il mio dominio e' www.dpsoftware.org
stesso problema sia con www che senza ovviamente :(

Con una cosa del genere, ti funziona?

import java.io.*;
import javax.microedition.io.*;

String url = "http://www.ericgiguere.com/index.html";
HttpConnection conn = null;
InputStream in = null;

try {
conn = (HttpConnection) Connector.open( url );

int rc = conn.getResponseCode();

if( rc == HttpConnection.HTTP_OK ){
in = conn.openInputStream();

// process the page data here
}
}
catch( IOException e ){
// handle errors
}
finally {
if( in != null ){
try { in.close(); } catch( IOException e ){}
}

if( conn != null ){
try { conn.close(); } catch( IOException e ){}
}
}

sblantipodi
24-11-2006, 20:15
Con una cosa del genere, ti funziona?


Con gli stream HTTP non ho problemi, sono le socket che mi stanno facendo dare la testa sullo schermo :mad: :mad:

dupa
24-11-2006, 20:22
QUESTA SOCKET al posto di restituire l'index.html del mio dominio, restituisce l'index.html del mio registrar/hoster :mad:



Mumble.
Considerando che un IP hosta più domini, e che apache differenzia le richieste in base al dominio, non è che chiamando in quel modo tramite socket, in pratica è come se facessi una richiesta http sull'ip al quale è associato il tuo dominio, ma quell'ip come sito di default magari ha quello del tuo hoster..
(spero di essermi spiegato..)

dupa
24-11-2006, 20:25
ora che ci penso mi sa che è proprio così, credo che nella richiesta HTTP devi anche mettere la stringa del dominio che vuoi.

dupa
24-11-2006, 20:27
Fai qualcosa tipo:

GET /index.html HTTP/1.1
Host: www.example.com

sblantipodi
24-11-2006, 21:11
Fai qualcosa tipo:

GET /index.html HTTP/1.1
Host: www.example.com

Forse ci sono problemi di sintassi non so boh:
Your browser sent a request that this server could not understand.

ho usato questo
"GET / HTTP/1.1 Host: www.dpsoftware.org"

dupa
24-11-2006, 21:25
Forse ci sono problemi di sintassi non so boh:
Your browser sent a request that this server could not understand.

ho usato questo
"GET / HTTP/1.1 Host: www.dpsoftware.org"
penso che devi andare a capo.
comunque fai una prova con telnet...

telnet www.dpsoftware.org 80

andbin
24-11-2006, 21:26
La specifica (<qui> (http://www.w3.org/Protocols/rfc2616/rfc2616-sec4.html#sec4)) dice che ogni riga di header deve essere terminata da CR+LF, con un CR+LF finale che termina gli header.

dupa
24-11-2006, 21:27
ok dovrei averti trovato la soluzione:

GET / HTTP/1.0
Host: www.dpsoftware.org

dupa
24-11-2006, 21:28
os.write("GET / HTTP/1.0\nHost: www.dpsoftware.org\n\n".getBytes());

andbin
24-11-2006, 21:32
os.write("GET / HTTP/1.0\nHost: www.dpsoftware.org\n\n".getBytes());\r\n

sblantipodi
24-11-2006, 23:41
os.write("GET / HTTP/1.0\nHost: www.dpsoftware.org\n\n".getBytes());

questo funziona alla perfezione grazie.
MALEDIZIONE ho la porta 80 bloccata :(

Sto utilizzando il JavaME per aprire questa socket e di default la porta 80 e' bloccata sui cellulari per le applicazioni non firmate.
Il mio server ha solo la porta 80 aperta e la 21.... :mad: :mad: :mad:

Vi viene in mente qualcosa? Qualche workaround?
Mi sa che ci sta poco da farsi venire in mente :( :mad: