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:
Welcher Fehler kommt?

Wenn es ein compile Fehler ist, wieso behebst du ihn nicht und initialisierst das objekt?
 
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
 
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 ://///
 
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.
 
Danke für dein Angebot zur schnellen hilfe, aber genau dass ist das Problem... ich weiß ned, wo ich nach diesem konstruktor suchen soll....
 

Neue Themen


Zurück
Oben