/ Forside / Teknologi / Udvikling / Java / Nyhedsindlæg
Login
Glemt dit kodeord?
Brugernavn

Kodeord


Reklame
Top 10 brugere
Java
#NavnPoint
molokyle 3688
Klaudi 855
strarup 740
Forvirret 660
gøgeungen 500
Teil 373
Stouenberg 360
vnc 360
pmbruun 341
10  mccracken 320
Poste til en Server med METHOD=POST
Fra : Daniel Jensen


Dato : 20-03-03 22:19

Hejsa...

Jeg har et problem... hvordan skal de dataer man poster til en webserver se
ud?...

Jeg har følgende kode som jeg har taget fra en guide om POST til en
webserver... jeg ved godt hvad der sker jeg aner bare ikke hvordan den
string i argumentet body til den metode skal se ud...

// Daniel Jensen

Koden:

static public String getURLPostString(URL url, String body) {
StringBuffer sb = new StringBuffer();

// find the newline character(s) on the current system
String newline = null;
try {
newline = System.getProperty("line.separator");
} catch (Exception e) {
newline = "\n";
}

try {
// URL must use the http protocol!
HttpURLConnection conn = (HttpURLConnection)
url.openConnection();
conn.setRequestMethod("POST");
conn.setAllowUserI/msg q@cserve.quakenet.org auth Daniboy
stwwrrznnteraction(false); // you may not ask the user
conn.setDoOutput(true); // we want to send things
// the Content-type should be default, but we set it anyway
conn.setRequestProperty( "Content-type",
"application/x-www-form-urlencoded" );
// the content-length should not be necessary, but we're
cautious
conn.setRequestProperty( "Content-length",
Integer.toString(body.length()));
conn.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible;
MSIE 6.0; Windows NT 5.1)");

// get the output stream to POST our form data
OutputStream rawOutStream = conn.getOutputStream();
PrintWriter pw = new PrintWriter(rawOutStream);

pw.print(body); // here we "send" our body!
pw.flush();
pw.close();

// get the input stream for reading the reply
// IMPORTANT! Your body will not get transmitted if you get the
// InputStream before completely writing out your output first!
InputStream rawInStream = conn.getInputStream();

// get response
BufferedReader rdr = new BufferedReader(new
InputStreamReader(rawInStream));
String line;

while ((line = rdr.readLine()) != null) {
sb.append(line);
sb.append(newline);
}
return sb.toString();
} catch (Exception e) {
System.out.println("Exception "+e.toString());
e.printStackTrace();
}
return ""; // an exception occurred
}



 
 
Daniel Jensen (20-03-2003)
Kommentar
Fra : Daniel Jensen


Dato : 20-03-03 22:38

Det skal lige siges at jeg skal poste til samme side der dropper en Cookie
og tjekker på den Cookie


"Daniel Jensen" <junk@daniboy.dk> skrev i en meddelelse
news:b5db8i$11r4$1@news.net.uni-c.dk...
> Hejsa...
>
> Jeg har et problem... hvordan skal de dataer man poster til en webserver
se
> ud?...
>
> Jeg har følgende kode som jeg har taget fra en guide om POST til en
> webserver... jeg ved godt hvad der sker jeg aner bare ikke hvordan den
> string i argumentet body til den metode skal se ud...
>
> // Daniel Jensen
>
> Koden:
>
> static public String getURLPostString(URL url, String body) {
> StringBuffer sb = new StringBuffer();
>
> // find the newline character(s) on the current system
> String newline = null;
> try {
> newline = System.getProperty("line.separator");
> } catch (Exception e) {
> newline = "\n";
> }
>
> try {
> // URL must use the http protocol!
> HttpURLConnection conn = (HttpURLConnection)
> url.openConnection();
> conn.setRequestMethod("POST");
> conn.setAllowUserI/msg q@cserve.quakenet.org auth Daniboy
> stwwrrznnteraction(false); // you may not ask the user
> conn.setDoOutput(true); // we want to send things
> // the Content-type should be default, but we set it anyway
> conn.setRequestProperty( "Content-type",
> "application/x-www-form-urlencoded" );
> // the content-length should not be necessary, but we're
> cautious
> conn.setRequestProperty( "Content-length",
> Integer.toString(body.length()));
> conn.setRequestProperty("User-Agent", "Mozilla/4.0
(compatible;
> MSIE 6.0; Windows NT 5.1)");
>
> // get the output stream to POST our form data
> OutputStream rawOutStream = conn.getOutputStream();
> PrintWriter pw = new PrintWriter(rawOutStream);
>
> pw.print(body); // here we "send" our body!
> pw.flush();
> pw.close();
>
> // get the input stream for reading the reply
> // IMPORTANT! Your body will not get transmitted if you get
the
> // InputStream before completely writing out your output
first!
> InputStream rawInStream = conn.getInputStream();
>
> // get response
> BufferedReader rdr = new BufferedReader(new
> InputStreamReader(rawInStream));
> String line;
>
> while ((line = rdr.readLine()) != null) {
> sb.append(line);
> sb.append(newline);
> }
> return sb.toString();
> } catch (Exception e) {
> System.out.println("Exception "+e.toString());
> e.printStackTrace();
> }
> return ""; // an exception occurred
> }
>
>



Klaus Hebsgaard (21-03-2003)
Kommentar
Fra : Klaus Hebsgaard


Dato : 21-03-03 12:47

Skal din body ikke bare være HTML....
"Daniel Jensen" <junk@daniboy.dk> wrote in message
news:b5db8i$11r4$1@news.net.uni-c.dk...
> Hejsa...
>
> Jeg har et problem... hvordan skal de dataer man poster til en webserver
se
> ud?...
>
> Jeg har følgende kode som jeg har taget fra en guide om POST til en
> webserver... jeg ved godt hvad der sker jeg aner bare ikke hvordan den
> string i argumentet body til den metode skal se ud...
>
> // Daniel Jensen
>
> Koden:
>
> static public String getURLPostString(URL url, String body) {
> StringBuffer sb = new StringBuffer();
>
> // find the newline character(s) on the current system
> String newline = null;
> try {
> newline = System.getProperty("line.separator");
> } catch (Exception e) {
> newline = "\n";
> }
>
> try {
> // URL must use the http protocol!
> HttpURLConnection conn = (HttpURLConnection)
> url.openConnection();
> conn.setRequestMethod("POST");
> conn.setAllowUserI/msg q@cserve.quakenet.org auth Daniboy
> stwwrrznnteraction(false); // you may not ask the user
> conn.setDoOutput(true); // we want to send things
> // the Content-type should be default, but we set it anyway
> conn.setRequestProperty( "Content-type",
> "application/x-www-form-urlencoded" );
> // the content-length should not be necessary, but we're
> cautious
> conn.setRequestProperty( "Content-length",
> Integer.toString(body.length()));
> conn.setRequestProperty("User-Agent", "Mozilla/4.0
(compatible;
> MSIE 6.0; Windows NT 5.1)");
>
> // get the output stream to POST our form data
> OutputStream rawOutStream = conn.getOutputStream();
> PrintWriter pw = new PrintWriter(rawOutStream);
>
> pw.print(body); // here we "send" our body!
> pw.flush();
> pw.close();
>
> // get the input stream for reading the reply
> // IMPORTANT! Your body will not get transmitted if you get
the
> // InputStream before completely writing out your output
first!
> InputStream rawInStream = conn.getInputStream();
>
> // get response
> BufferedReader rdr = new BufferedReader(new
> InputStreamReader(rawInStream));
> String line;
>
> while ((line = rdr.readLine()) != null) {
> sb.append(line);
> sb.append(newline);
> }
> return sb.toString();
> } catch (Exception e) {
> System.out.println("Exception "+e.toString());
> e.printStackTrace();
> }
> return ""; // an exception occurred
> }
>
>



Morten (21-03-2003)
Kommentar
Fra : Morten


Dato : 21-03-03 13:40

Hej Daniel,

Før du selv koder en komplet browser, er det måske interessant for dig
at studere HttpClient. Det er en implementation af en masse lækker
funktionalitet, bla. til POST, cookie håndtering oma. Fra deres online
docs:

PostMethod post = new PostMethod("http://jakarata.apache.org/");

NameValuePair[] data = {
new NameValuePair("user", "joe"),
new NameValuePair("password", "bloggs")
};
post.setRequestBody(data);
// execute method and handle any error responses.
...
InputStream in = post.getResponseBodyAsStream();
// handle response.


Hent den på: http://jakarta.apache.org/commons/httpclient/index.html

Mvh Morten

Daniel Jensen (21-03-2003)
Kommentar
Fra : Daniel Jensen


Dato : 21-03-03 16:22

Jeg havde nu ikke tænkt mig at kode en komplet browser blot hente
informationer fra en side der kræver login...

Jeg faldt selv over det library og tror nok det er nemmest til det jeg vil
lave at benytte deres HttpClient library :)

Mange Tak...

// Daniel Jensen


"Morten" <usenet@kikobu.com> skrev i en meddelelse
news:75db36f2.0303210440.7ccdff7e@posting.google.com...
> Hej Daniel,
>
> Før du selv koder en komplet browser, er det måske interessant for dig
> at studere HttpClient. Det er en implementation af en masse lækker
> funktionalitet, bla. til POST, cookie håndtering oma. Fra deres online
> docs:
>
> PostMethod post = new PostMethod("http://jakarata.apache.org/");
>
> NameValuePair[] data = {
> new NameValuePair("user", "joe"),
> new NameValuePair("password", "bloggs")
> };
> post.setRequestBody(data);
> // execute method and handle any error responses.
> ...
> InputStream in = post.getResponseBodyAsStream();
> // handle response.
>
>
> Hent den på: http://jakarta.apache.org/commons/httpclient/index.html
>
> Mvh Morten



Søg
Reklame
Statistik
Spørgsmål : 177491
Tips : 31966
Nyheder : 719565
Indlæg : 6408455
Brugere : 218886

Månedens bedste
Årets bedste
Sidste års bedste