HTTP Bräuchte hilfe mit Http Requests

Darkj53

Mitglied
Jo jungs,
ich mache gerade meinen einstieg in die Netzwerkprogrammierung und probiere ein bisschen mit Post und get herum, kann mir jemand erklären, was genau an meinem kleinen code Snipplet nicht funktioniert, und am besten, warum das so ist.... wäre seeehr nett, danke im Voraus
lg

Java:
import java.io.*;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.client.*;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.conn.ConnectTimeoutException;
import org.apache.http.impl.client.DefaultHttpClient;




public class HttpConnect{
  //URL der Anmeldemaske
  private String url;
  //URL in der action-Parameter des forms
  private String postURL;
  
  private String location;
  private String session ="";
  private String status = "Ready";
  private String user;
  private String pass;
  private HttpClient client;
  private HttpGet get;
  private HttpPost post;
  private boolean fatalError = false;
  private String errorState = "";
  
  public void setURL (String url,String posturl) {
    this.url = url;
    this.postURL = posturl;
  }
  public void setUsername (String Username) {
    this.user = Username;
  }
  public void setPassword (String Password) {
    this.pass = Password;
  }
  
  
  
  public void Connect()
  {
    client = new HttpClient();
    get = new HttpGet ( url );
    post = new HttpPost ( postURL );
  }
  
  public String getPage(String newURL)
  {
    String result = "";
    try
    {
      get = new HttpGet(newURL);
      client.executeMethod(get);
      
      BufferedReader rd = new BufferedReader(
      new InputStreamReader(get.getResponseBodyAsStream()));
      String line;
      while ((line = rd.readLine()) != null)
      {
        line += rd.readLine();
        System.out.println(line);
        result += line + '\n';
      }
      rd.close();
      
      get.releaseConnection();
    }
    catch (Exception e)
    {
      System.err.println("Connect::execGet()" + e.toString());
      fatalError = true;
      errorState = e.getMessage();
    }
    return result;
  }
  
  private String getSessionID(String content)
  {
    String sSessionID = "";
    Header[] aHeader = get.getResponseHeaders();
    
    for (int l = 0; aHeader != null && l < aHeader.length; l++)
    {
      Header hCookie = aHeader[l];
      HeaderElement[] heCookie = hCookie.getElements();
      for (int i= 0; heCookie != null && i < heCookie.length; i++)
      {
        if ( heCookie[i].getName().equals("JSESSIONID"))
        {
          sSessionID = heCookie[i].getValue();
          break;
        }
      }
      if (sSessionID.length() > 0)
      break;
    }
    
    return sSessionID;
  }
  
  private void doPostLogin()
  {
    try
    {
      session = getSessionID(getPage(url)); //sessionID parsen
      post.setRequestBody(initPostData());
      client.executeMethod(post);
      //String redirectLocation = "";
      //Header locationHeader = post.getResponseHeaders()[3];
      //redirectLocation = locationHeader.getValue();
      post.releaseConnection();
      //post = new PostMethod(redirectLocation);
      //client.executeMethod(post);
      //post.releaseConnection();
    }
    catch (Exception e)
    {
      System.err.println("Connect::execPost()" + e.toString());
      fatalError = true;
      errorState = e.getMessage();
    }
  }
  //pass,user,session sind alle vom Typ String
  private NameValuePair[] initPostData() throws Exception
  {
    if (user != "" || pass != "" || session != "")
    {
      NameValuePair[] data = { new NameValuePair("JSESSIONID", session),
        new NameValuePair("j_username", user),
        new NameValuePair("j_password", pass)
      };
      return data;
    }
    throw (new Exception("Session ID not initialized"));
  }
}

Nicht missverstehen oder so, der Code ist nutzlos, er schlägt ständig beim compiler an, das ich ein objekt verwende, das nicht initialisiert wäre und das andere Problem baut darauf auf... ich arbeite an einem größeren Projekt, dass automatisch mit dem ergebnis einer Seite arbeitet, sie bestenfalls auswertet usw...
 
Zuletzt bearbeitet:
M

Marcinek

Gast
Welcher Fehler kommt?

Wenn es ein compile Fehler ist, wieso behebst du ihn nicht und initialisierst das objekt?
 

Darkj53

Mitglied
weil ich nicht weiß an welcher stelle im programm ich das machen soll....

Compiliere C:\Users\Jens\Dropbox\Informatik\Bot\HttpConnect.java mit Java-Compiler
HttpConnect.java:130:11: error: cannot find symbol
private NameValuePair[] initPostData() throws Exception
^
symbol: class NameValuePair
location: class HttpConnect
HttpConnect.java:48:14: error: HttpClient is abstract; cannot be instantiated
client = new HttpClient();
^
HttpConnect.java:59:13: error: cannot find symbol
client.executeMethod(get);
^
symbol: method executeMethod(HttpGet)
location: variable client of type HttpClient
HttpConnect.java:62:32: error: cannot access AbstractHttpMessage
new InputStreamReader(get.getResponseBodyAsStream()));
^
class file for org.apache.http.message.AbstractHttpMessage not found
HttpConnect.java:86:5: error: cannot find symbol
Header[] aHeader = get.getResponseHeaders();
^
symbol: class Header
location: class HttpConnect
HttpConnect.java:86:27: error: cannot find symbol
Header[] aHeader = get.getResponseHeaders();
^
symbol: method getResponseHeaders()
location: variable get of type HttpGet
HttpConnect.java:90:7: error: cannot find symbol
Header hCookie = aHeader[l];
^
symbol: class Header
location: class HttpConnect
HttpConnect.java:91:7: error: cannot find symbol
HeaderElement[] heCookie = hCookie.getElements();
^
symbol: class HeaderElement
location: class HttpConnect
HttpConnect.java:113:13: error: cannot find symbol
client.executeMethod(post);
^
symbol: method executeMethod(HttpPost)
location: variable client of type HttpClient
HttpConnect.java:134:7: error: cannot find symbol
NameValuePair[] data = { new NameValuePair("JSESSIONID", session),
^
symbol: class NameValuePair
location: class HttpConnect
HttpConnect.java:134:36: error: cannot find symbol
NameValuePair[] data = { new NameValuePair("JSESSIONID", session),
^
symbol: class NameValuePair
location: class HttpConnect
HttpConnect.java:135:13: error: cannot find symbol
new NameValuePair("j_username", user),
^
symbol: class NameValuePair
location: class HttpConnect
HttpConnect.java:136:13: error: cannot find symbol
new NameValuePair("j_password", pass)
^
symbol: class NameValuePair
location: class HttpConnect
13 errors
 

Darkj53

Mitglied
ich mach zum ersten mal was mit apach httpclient... und deswegen weiß ich ned wirklich was ich da tun soll... hab schon die halbe nacht gegooglet... und vor ner stunde oder so festgestellt... dass die imports falsch sind ://///
 
M

Marcinek

Gast
Beispiel hier:

Code:
Compiliere C:\Users\Jens\Dropbox\Informatik\Bot\HttpConnect.java mit Java-Compiler
HttpConnect.java:130:11: error: cannot find symbol
private NameValuePair[] initPostData() throws Exception
          ^


In der DAtei HttpConnect.java in Zeile 130 im 11 Zeichen. Sogar der Pfeil zeigt da drauf. :bahnhof:

Und dann sagt er dir "cannot find symbol". Er kennt die Klasse NameValuePair nicht. Vermutlich ist sie nicht im classpath.

Oder er kann sie nicht kompilieren weil:

symbol: class NameValuePair
location: class HttpConnect
HttpConnect.java:48:14: error: HttpClient is abstract; cannot be instantiated
client = new HttpClient();
^

Jetzt du.
 

Darkj53

Mitglied
Danke für dein Angebot zur schnellen hilfe, aber genau dass ist das Problem... ich weiß ned, wo ich nach diesem konstruktor suchen soll....
 
Ähnliche Java Themen
  Titel Forum Antworten Datum
J Objekt mit RSA und AES verschlüsseln und entschlüsseln HILFE Netzwerkprogrammierung 4
T Brauche Hilfe beim GET-String für HttpURLConnection Netzwerkprogrammierung 4
platofan23 Socket Hilfe mit Socket Thread und ArrayList Netzwerkprogrammierung 6
V Kann man mit Hilfe eines Java-Programms den Zugriff auf bestimmte Internetseiten verhinden? Netzwerkprogrammierung 3
C JSON, API ... Anfänger braucht eure Hilfe Netzwerkprogrammierung 10
KingSquizzi3 Website parsen mit Hilfe von jsoup funktioniert nicht Netzwerkprogrammierung 3
J Hilfe beim programmiern einer App zur Anmeldung im Wlan-Netzwerk Netzwerkprogrammierung 0
B JKS erstellen bitte um hilfe Netzwerkprogrammierung 1
R Hilfe bei FTP Netzwerkprogrammierung 7
R Benötige Hilfe bei Routereinstellungen Netzwerkprogrammierung 7
E RMI RMI - Brauche Hilfe? Netzwerkprogrammierung 7
xDarkSunx Hilfe Chat Login Netzwerkprogrammierung 7
K Datenübertragung UDP Hilfe ????? Netzwerkprogrammierung 5
H Hilfe bei multiplen Clients Netzwerkprogrammierung 7
N Client - Server kurze Hilfe bitte Netzwerkprogrammierung 2
T IP Adresse mit Hilfe der MAC Adresse ermitteln Netzwerkprogrammierung 3
D Hilfe ich komme bei meinem UDP chat Server nicht mehr weiter Netzwerkprogrammierung 9
D Hilfe, meine HttpURLConnection hängt manchmal. Netzwerkprogrammierung 9
eQui Hilfe bei Chatprogramm Netzwerkprogrammierung 3
J Brauche hilfe bei Projekt: Netzwerk Chatprogramm Netzwerkprogrammierung 12
TRunKX Hilfe beim senden und empfangen Netzwerkprogrammierung 2
André B. Brauche Hilfe bei Chat Netzwerkprogrammierung 6
D Netzwerk Game, bitte hilfe. Netzwerkprogrammierung 2
N JMS Newbie braucht hilfe Netzwerkprogrammierung 6
S Noob braucht dringend hilfe bei ftp-client Netzwerkprogrammierung 2
F http Post auf einen Grafana Server Netzwerkprogrammierung 3
H Datei mit Anhang via http "hochladen" Netzwerkprogrammierung 16
S HTTP Post?!? - Java Server Netzwerkprogrammierung 7
R Anfängerbeispiel: Suche Java-Anwendung die http-Anfragen in Tomcat liest Netzwerkprogrammierung 8
O HTTP Wer hilft mit meinem UTF-8 http Request ? Netzwerkprogrammierung 1
G localhost im Backend https vs. http Netzwerkprogrammierung 9
J Simple HTTP Framework (basierend auf expressjs) Netzwerkprogrammierung 1
M HTTP Http requests aufzeichnen Netzwerkprogrammierung 2
J HTTP [Java 9] Neuer HTTP Client - Tutorial Netzwerkprogrammierung 3
J HTTP Befehl via HTTP senden ohne Browser öffnen Netzwerkprogrammierung 3
F HTTP HTTP Rest Client mit TLS1.2 und selbst signiertem Zertifikat Netzwerkprogrammierung 2
M http request and response Netzwerkprogrammierung 0
M Verbindungszeit berechnen (TCP, HTTP/1.0, HTTP/1.1) Netzwerkprogrammierung 2
P HTTP Bild von einem Server per http kopieren Netzwerkprogrammierung 1
U HTTP XML vom Server abholen oder http Abfragen, Entscheidung treffen Netzwerkprogrammierung 0
P HTTP Server / Client Netzwerkprogrammierung 1
S HTTP-Requeste von Browser mit Java sniffen? Netzwerkprogrammierung 9
E HTTP java.lang.IllegalArgumentException: protocol = http host = null Netzwerkprogrammierung 1
M Server mit HTTP Netzwerkprogrammierung 9
H Sockets oder HTTP- Methoden? Netzwerkprogrammierung 3
B Socket HTTP-Request führt zu Endlosschleife Netzwerkprogrammierung 5
D HTTP nochne frage zu http requests Netzwerkprogrammierung 6
K HTTP Eigener Http Response für Datei-Download Netzwerkprogrammierung 4
D Server Client Verbindung - Unexpected End of File - Invalid HTTP Response Netzwerkprogrammierung 4
F HTTP HTTP-Download: Dateien in einem Verzeichnis ermitteln Netzwerkprogrammierung 8
T HTTP Einen HTTP Server erstellen Netzwerkprogrammierung 20
N Problem über http eine Datei zu senden Netzwerkprogrammierung 4
Dit_ HTTP Einfache HTTP-Anfrage Netzwerkprogrammierung 6
nrg FileUpload HTTP POST Netzwerkprogrammierung 5
T Socket Java HTTP-Proxy Netzwerkprogrammierung 3
A HTTP Zugriff auf http-Server - Error 400 Netzwerkprogrammierung 11
B HTTP JSESSIONID in Http(s)URLConnection loswerden! Netzwerkprogrammierung 13
E Socket HTTP-Server Netzwerkprogrammierung 6
C HTTP POST Connect Netzwerkprogrammierung 9
D HTTP Java HTTP Kommunikation Netzwerkprogrammierung 9
R HTTP Apache HTTP Client: Request mit angehängter Datei Netzwerkprogrammierung 2
O Http request Netzwerkprogrammierung 7
Tobse HTTP ServerSocket HTTP Netzwerkprogrammierung 4
I HTTP Datei Uploaden mit http und Sprache anpassen Netzwerkprogrammierung 7
I Socket HTTP Nachrichten über Sockets verschicken Netzwerkprogrammierung 2
N Http Client Netzwerkprogrammierung 3
W Asynchroner HTTP Client / non-blocking I/O Netzwerkprogrammierung 2
R HTTP Problem bei Authentifizierung über (Http)UrlConnection Netzwerkprogrammierung 2
L Body eines http Request auslesen Netzwerkprogrammierung 2
T HTTP Encoding von Http-Streams Netzwerkprogrammierung 2
K Ende eines HTTP Response/Request Netzwerkprogrammierung 6
D http request Netzwerkprogrammierung 11
C HTTP Studienarbeit Kommunikation via HTTP mit POST zwischen Server und Client Netzwerkprogrammierung 7
Kr0e Simpler HTTP Stream server Test Netzwerkprogrammierung 3
K HTTP-Anfrage an einen Server wird nicht beantwortet Netzwerkprogrammierung 3
J org.apache.http.auth.NTCredentials Netzwerkprogrammierung 2
W Problem mit HTTP-Dateiübertragung Netzwerkprogrammierung 6
A org.apache.commons http client in Netbeans einbinden Netzwerkprogrammierung 3
G Http Client mit Redirect Enabled Netzwerkprogrammierung 2
M Http POST liefert kryptischen Content Netzwerkprogrammierung 4
G Sockets und HTTP: Ende des Datenempfangs erkennen Netzwerkprogrammierung 3
B Wie HTTP GET/POST Anfrage versenden? Netzwerkprogrammierung 7
R HTTP Server Memory Leak? Netzwerkprogrammierung 6
G HTTP-Request InputStream-Problem Netzwerkprogrammierung 5
K optimale HTTP Downloads Netzwerkprogrammierung 15
G auf http-request antworten. Netzwerkprogrammierung 3
I http sniffer gesucht Netzwerkprogrammierung 10
G Socket + HTTP Header Netzwerkprogrammierung 2
R HTTP ServerThread Netzwerkprogrammierung 4
J HTTP GET? Netzwerkprogrammierung 6
C FTP vs HTTP Netzwerkprogrammierung 10
M HTTP-Response extrem fragmentiert Netzwerkprogrammierung 4
W HTTP-Upload Netzwerkprogrammierung 2
M HTTP response code: 401 ; ?aber im browser ist es abrufbar? Netzwerkprogrammierung 5
L Java HTTP Request Netzwerkprogrammierung 3
O Axis2 erstellt HTTP/1.1 anstatt HTTP/1.0 Netzwerkprogrammierung 3
B Fehler 401 bei http GET Netzwerkprogrammierung 2
S HTTP Request erstellen Netzwerkprogrammierung 2
E upload progress bei einem http file post Netzwerkprogrammierung 5
B lange Strings in http-Request? Netzwerkprogrammierung 3

Ähnliche Java Themen

Neue Themen


Oben