HTTP Mediawiki Upload

chaosjava

Mitglied
Hallo Leute,

ich wollte mich mal erkundigen, ob sich hier jemand mit MediaWiki auskennt?
Ich möchte über die Api.php den HTTP Post "action=upload"(ab Version 1.16) verwenden, doch bei mir erscheint der Fehler:
[XML]<?xml version="1.0"?><api><error code="unknownerror" info="Unknown error: ``mustbeloggedin''" /></api>[/XML]

Also ich nutze das Jakarta Commons HTTPClient Package (HttpComponents - HttpComponents Overview)

Im MediaWiki Api.php seht folgendes drin:
* action=upload *

Upload a file, or get the status of pending uploads. Several methods are available:
* Upload file contents directly, using the "file" parameter
* Have the MediaWiki server fetch a file from a URL, using the "url" parameter
* Complete an earlier upload that failed due to warnings, using the "sessionkey" parameter

Note that the HTTP POST must be done as a file upload (i.e. using multipart/form-data) when
sending the "file". Note also that queries using session keys must be
done in the same login session as the query that originally returned the key (i.e. do not
log out and then log back in). Also you must get and send an edit token before doing any upload stuff.

This module requires read rights.
This module requires write rights.
This module only accepts POST requests.

Parameters:
filename - Target filename
comment - Upload comment. Also used as the initial page text for new files if "text" is not specified
Default:

text - Initial page text for new files
token - Edit token. You can get one of these through prop=info
watch - Watch the page
ignorewarnings - Ignore any warnings
file - File contents
url - Url to fetch the file from
sessionkey - Session key returned by a previous upload that failed due to warnings

Examples:

Upload from a URL:

api.php?action=upload&filename=Wiki.png&url=http%3A//upload.wikimedia.org/wikipedia/en/b/bc/Wiki.png

Complete an upload that failed due to warnings:

api.php?action=upload&filename=Wiki.png&sessionkey=sessionkey&ignorewarnings=1

Habe auch dieses Tutorial versucht:API:Edit - Uploading files - MediaWiki
---> Aber bekomme es trotzdem nicht auf die Reihe.

Den POST "action=login" bekomme ich feherfrei hin, aber der Upload will noch nicht.

HIER MEIN JAVA-CODE:
Java:
import java.io.File;




import org.apache.commons.httpclient.*;
import org.apache.commons.httpclient.methods.*;


public class PostExample {
 @SuppressWarnings("deprecation")
public static void main( String[] args ) {
   String url = "http://localhost/mediawiki/api.php?wikidb2_session=bvmkhka800njr96vu9s4p4a273";
  // String login= JOptionPane.showInputDialog("Geben Sie Ihren Benutzernamen ein:");
  // String password= JOptionPane.showInputDialog("Geben Sie Ihr Password ein:");
   try {
    HttpClient client = new HttpClient();
    PostMethod method = new PostMethod( url );
    MultipartPostMethod test = new MultipartPostMethod(url);
    
	
   
    NameValuePair[] data = {
    	
    		
    		//new NameValuePair("action", "login"),
    		//new NameValuePair("lgname", "Useraaa"),
    		//new NameValuePair("lgpassword", "pass"),
    		//new NameValuePair("lgdomain",""),
    		//new NameValuePair("wikidb2_sessionid", "r13gkdfk5u8chjfim4spmrh101"),
    		//new NameValuePair("lgtoken","29c72efbb6fe9f24600ccbe69ca2a2ab"),
    		//new NameValuePair("filename", "2.jpg"),
    		//new NameValuePair("url", "http://www.level-east.ch/uploads/media/IT_Software_01.png"),
    		//new NameValuePair("token", "+\\"),
    		//new NameValuePair("format", "xml")
    		};
   
    
    //Hier ist Upload-Versuch
    File f1 = new File("3.jpg");
    
    
    System.out.println("File1 Length = " + f1.length());
    
    test.addParameter("action", "upload");
    test.addParameter("file", f1);
   test.addParameter("filename", "3.jpg");
    test.addParameter("token", "+\\");
    test.addParameter("format","xml");
    
    
    int statusCode1 = client.executeMethod(test);
    
    System.out.println("statusLine>>>" + test.getStatusLine());
    String contents2 = test.getResponseBodyAsString();
    test.releaseConnection();
    System.out.println( contents2 );

          //Das ist die Post-Methode für den Login
	 //method.addParameters(data);
	


          //NICHT RELEVANT
	 // Execute the POST method
  // int statusCode = client.executeMethod( method );

    //if( statusCode != -1 ) {
//      String contents = method.getResponseBodyAsString();
    
     // FileWriter fw = new FileWriter ("results.xml");
   
     // fw.write(contents);
      //method.releaseConnection();
   //   System.out.println( contents );
      //fw.close();
   // }
   }
   catch( Exception e ) {
    e.printStackTrace();
   }
 }
}

Ich wäre für alle relevanten Hifestellungen dankbar!

Viele Grüße,

chaosjava
 

madboy

Top Contributor
Laut der Fehlermeldung bist du nicht eingeloggt oder behandelst Cookies nicht/falsch. Also erst einloggen oder schauen, wie sich mit dem HTTPClient Cookies speichern lassen. Es sollte da einen CookieManager geben, der alles automatisch regelt (wenn ich mich recht erinnere).
 

chaosjava

Mitglied
Also irgendwie will er bei mir die Cookies nicht so wirklich setzen.

Habe probiert:
Java:
test.getParams().setCookiePolicy(CookiePolicy.RFC_2109);

und danach gleich:
Java:
System.out.println(test.getParams().getCookiePolicy());

und er gibt mir nur
Code:
default
aus.
Sollte er mir nicht die Cookies ausgeben, die von Mediawiki kommen?

Hier noch eine Logdatei, die unteranderem das ausgibt:
Code:
Date:			Tue Jul 06 15:49:54 CEST 2010 (1278424194038)
Thread:		main
Message #:	43
Level:		DEBUG
NDC:			
Category:	httpclient.wire.header
Message:		<< "Set-Cookie: wikidb2_session=3fqkf7iql0l1nal9ngos9jlu41; path=/; HttpOnly[\r][\n]"
Location:	org.apache.commons.httpclient.Wire.wire(Wire.java:70)
Thrown:

und etwas weiter dann das:
Code:
Date:			Tue Jul 06 15:49:54 CEST 2010 (1278424194038)
Thread:		main
Message #:	52
Level:		DEBUG
NDC:			
Category:	org.apache.commons.httpclient.HttpMethodBase
Message:		Cookie accepted: "$Version=0; wikidb2_session=3fqkf7iql0l1nal9ngos9jlu41; $Path=/"
Location:	org.apache.commons.httpclient.HttpMethodBase.processCookieHeaders(HttpMethodBase.java:1651)
Thrown:

Dies liefert er mir beim Login, aber beim Upload kann ich solche Einträge nicht finden, obwohl ich da auch set.CookiePolicy verwende.
 

madboy

Top Contributor
Ah, jetzt ja ;-)

Du erstellst eine neue Instanz des HTTPClient für das Login und den Upload? Dann weiß natürlich die zweite Instanz nix von den Cookies der ersten Instanz... Verwende genau eine Instanz des HTTPClient, dann sollte es klappen. Alternativ kannst du die Cookies auch "von Hand" setzen, wie in dem geposteten Link beschrieben.

Das Cookie wird beim Login gesetzt und muss danach immer wieder übergeben werden, sonst weiß dein MediaWiki nicht dass du schon eingeloggt bist. Genau das regelt die CookiePolicy.
 

chaosjava

Mitglied
Hallo madboy,

danke erstmal für deine gute Hilfe!
Hat mir echt geholfen!
Hatte 2 HTTPClient Instanzen, verwende jetzt nur eine und es klappt!;)

VG chaosjava
 

chaosjava

Mitglied
Hallo Leute,

das Problem mit dem Api.php ist jetzt geklärt, aber ich möche mein Java Programm jetzt erweitern und einen Upload für Mediawiki Seiten, die diese action= upload noch nicht unterstützen.

Da es z.B. bei Mediawiki Version 1.15 und kleiner noch keine "action = upload" gibt, wollte ich es mit Java über die normale Seite von Mediawiki machen(Spezial:Upload).
Doch irgendwie bekomme ich das mit den Post-Methoden nicht hin.
Weiß einer welche werte ich übersenden muss?

Hier noch ein ausschnitt aus meinem java code:

Java:
public static void upload2(String file){
 		String url = "http://localhost/mediawiki/index.php?title=Spezial:Hochladen";
 		File f1 = new File(file);
 		try {
 			
 			MultipartPostMethod test = new MultipartPostMethod(url);
 			System.out.println("==============COOKIE==========");
 			test.getParams().setCookiePolicy(CookiePolicy.RFC_2109);
 						
 			test.addParameter("wpSourceType", f1);
 			test.addParameter("wpUploadFile", f1.getName());
 			test.addParameter("wpDestFile", f1.getName());
 			test.addParameter("wpEditToken", EditToken.token());
 			test.addParameter("wpUpload", "Datei hochladen"); 
 			
 			
 			
 			
 			System.out.println("============UPLOAD2=================");
 			int statusCode1 = client.executeMethod(test);
 			if( statusCode1 != -1 ) {
 				String response = test.getResponseBodyAsString();
 				System.out.println(response.contains("Warning"));
 				
 				
 				System.out.println("statusLine>>>" + test.getStatusLine());
 				String contents2 = test.getResponseBodyAsString();
 				FileWriter fw = new FileWriter ("resultsupload.xml");
 				fw.write(contents2);
 				test.releaseConnection();
 				fw.close();
 				
 				
 			}
 		}
 		catch( Exception e ) {
 			e.printStackTrace();
 		}
 		
 	}

Hat jemand Hilfestellungen?
 
Ähnliche Java Themen
  Titel Forum Antworten Datum
J FTP Upload über Proxy funktioniert nicht Netzwerkprogrammierung 1
D CSV File Upload Netzwerkprogrammierung 5
P nanoHttp upload.html page lädt nicht Netzwerkprogrammierung 4
M HTTP File Upload mit Prozessbar Funktioniert nicht. Netzwerkprogrammierung 8
5 File Upload/ ClassNotFoundException Netzwerkprogrammierung 9
B FTPS Upload Netzwerkprogrammierung 3
M Google Image Upload Netzwerkprogrammierung 12
M Apache Solr doc & pdf Upload Netzwerkprogrammierung 8
C apache commons net ftp bei upload unvollständig Netzwerkprogrammierung 3
D FTP Pfadangabe für ftp-upload funktioniert nicht Netzwerkprogrammierung 5
R HTTP HttpURLConnection Large File Upload Netzwerkprogrammierung 1
1 Upload problem! org.apache.commons.net.ftp Netzwerkprogrammierung 3
E Applet zum Datei-Upload Netzwerkprogrammierung 3
P Bilder: FTP-Upload funktioniert nicht richtig Netzwerkprogrammierung 2
W HTTP-Upload Netzwerkprogrammierung 2
K Datei-Upload per FTP Netzwerkprogrammierung 2
E Upload großer Dateien? Netzwerkprogrammierung 5
E upload progress bei einem http file post Netzwerkprogrammierung 5
eskimo328 progress bar mit upload speed Netzwerkprogrammierung 19
J Java Programm für Upload von Dateien per HTTP Netzwerkprogrammierung 7
L file upload / download über http Netzwerkprogrammierung 5
J File upload mit ftp Netzwerkprogrammierung 4

Ähnliche Java Themen

Neue Themen


Oben