Postrequest mit Parameterübergabe

neela

Bekanntes Mitglied
Hallo

Könnt ihr mir verrate wie ich bei einen Postrequest eine Datei als Paramter übergeben kann

Folgenden Code habe ich mit diesem bekomme ich auch eine connection hin, nur halt bei der nötigen Paramterübergabe bin ich grade planlos ???:L Onkel google verrät mir auch nix dazu
Java:
url = new URL(lUrl);
		HttpURLConnection connection = null;
		connection = (HttpURLConnection) url.openConnection();
		connection.setRequestMethod("POST");
		connection.
		connection.setDoInput(true);
		connection.setDoOutput(true);
		connection.setUseCaches(false);
		connection.setRequestProperty("Content-Type",
				"application/x-www-form-urlencoded");

		connection.setRequestProperty("Content-Length", String.valueOf(body
				.length()));

		OutputStreamWriter writer = new OutputStreamWriter(connection
				.getOutputStream());
		writer.write(body);
		writer.flush();

		
		input = connection.getInputStream();
		String lResponse = readFile(input).toString();

		log.info("XML-Response:" + nn + lResponse);

	}

	private StringBuffer readFile(InputStream input)
			throws FileNotFoundException, IOException {
		String lLine;
		StringBuffer lResult = new StringBuffer();
		BufferedReader bufferreader = new BufferedReader(new InputStreamReader(
				input));
		while ((lLine = bufferreader.readLine()) != null) {
			lResult.append(lLine + nn);
		}
		bufferreader.close();
		return lResult;
	}


Vielen dank für eure Hilfe
 
Zuletzt bearbeitet:

neela

Bekanntes Mitglied
hab momenten ein totals blackout

nun wird mir die Datei die eingelesen wird aus gegeben aber
nicht mehr die "ResponseDatei"

Java:
void run() throws MalformedURLException, FileNotFoundException,
			IOException, ConnectException, AuthenticationException {
		URL url = null;
		url = new URL("http://" + aimsHost
				+ "/aims/servlet/com.esri.esrimap.Esrimap?ServiceName="
				+ aimsService);

		log.debug("Request AIMS for Monitoring");
		System.setProperty("http.proxyPort", "5300");
		System.setProperty("http.proxyHost", aimsHost);


		url = new URL("http://" + aimsHost
				+ "/aims/servlet/com.esri.esrimap.Esrimap?ServiceName="
				+ aimsService);
	
		HttpURLConnection connection = null;
		connection = (HttpURLConnection) url.openConnection();
		connection.setRequestMethod("POST");
		connection.setDoInput(true);
		connection.setDoOutput(true);
		connection.setUseCaches(false);
		connection.setRequestProperty("Content-Type",
				"application/x-www-form-urlencoded");

		File xmlfile= new File ("C:\\Profiles\\lv14hofm\\Desktop\\ArcIMSMonitor\\reqdata\\shape.xml");
		
		InputStream input1= new FileInputStream(xmlfile);
		String body = input1.toString()+URLEncoder.encode( "value1", "UTF-8" );
		connection.setRequestProperty("Content-Length", String.valueOf(body.length()));


		OutputStreamWriter writer = new OutputStreamWriter(connection
				.getOutputStream());
		writer.write(body);
		writer.flush();

	
		
	
		//input = connection.getInputStream();
		String lResponse = readFile(input1).toString();

		log.info("XML-Response:" + nn + lResponse);

	}

	private StringBuffer readFile(InputStream input)
			throws FileNotFoundException, IOException {
		String lLine;
		StringBuffer lResult = new StringBuffer();
		BufferedReader bufferreader = new BufferedReader(new InputStreamReader(
				input));
		//File xmlfile= new File ("C:\\Profiles\\l14hof\\Desktop\\data\\dreieck.xml");
		//System.out.println(xmlfile.toString());
		//BufferedReader bufferreader = new BufferedReader(new FileReader(xmlfile));
		while ((lLine = bufferreader.readLine()) != null) {
			lResult.append(lLine + nn);
		}
		bufferreader.close();
		return lResult;
	}
 
S

SlaterB

Gast
tja, dann vielleicht Zeile 41 wieder ins Programm aufnehmen und Zeile 42 so ändern dass Zeile 41 auch ne Wirkung hat?

ob der FileUpload usw. funktioniert kann ich übrigens nicht beurteilen, hab nur den Link genannt ;)
 

neela

Bekanntes Mitglied
aber die datei übergebe ich ja an das input1 und wenn ich die beiden zeile so ersetze
Java:
input1 = connection.getInputStream();
		String lResponse = readFile(input1).toString();
kommt natürlich auch nicht das richtige bei raus
 
S

SlaterB

Gast
hatte denn
> input = connection.getInputStream();
> String lResponse = readFile(input).toString();
im ersten Post funktioniert?

ob der InputStream der Connection was ordentliches liefert kann ich nicht beurteilen,
was ist denn 'nicht das richtige'?
 

neela

Bekanntes Mitglied
der input aus dem ersten post hat mir folgenden "Fehlermeldung " geliefert da ich da ja keine Datei eingebunden hatte
Code:
<?xml version="1.0" encoding="Cp1252"?>
<ARCXML version="1.1">
<RESPONSE>
<ERROR machine="blu3" processid="2632" threadid="2700">AXLParser: Fatal error at line 2, column 1. Message: Invalid document structure</ERROR>
</RESPONSE>
</ARCXML>
 
Zuletzt bearbeitet:

neela

Bekanntes Mitglied
mit hilfe des folgenden Codes kann ich nun den Request absetzen
Java:
void run() throws MalformedURLException, FileNotFoundException,
			IOException, ConnectException, AuthenticationException {

		URL url = new URL("http://" + aimsHost
				+ "/aims/servlet/?ServiceName="
				+ aimsService);
		log.debug("Request AIMS for Monitoring");
		System.setProperty("http.proxyPort", "5800");
		System.setProperty("http.proxyHost", aimsHost);

		// Connection aufbauen
		HttpURLConnection connection = null;
		connection = (HttpURLConnection) url.openConnection();
		connection.setRequestMethod("POST");
		connection.setDoInput(true);
		connection.setDoOutput(true);
		connection.setUseCaches(false);
		connection.setRequestProperty("Content-Type",
				"application/x-www-form-urlencoded");

		// Inputdatei als Parameter übergeben
		File xmlfile = new File(
				"C:\\Profiles\\lesktop\\reqdata\\blubber.xml");
		FileInputStream xmlread = new FileInputStream(xmlfile);
		ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
		byte[] buffer = new byte[16384];
		for (int len = xmlread.read(buffer); len > 0; len = xmlread
				.read(buffer)) {
			byteArrayOutputStream.write(buffer, 0, len);
		}
		xmlread.close();

		String body = (new String(byteArrayOutputStream.toByteArray()) + URLEncoder
				.encode("ArcXMLRequest", "UTF-8"));
		connection.setRequestProperty("Content-Length", String.valueOf(body
				.length()));

		// Request
		OutputStream writer = connection.getOutputStream();
		writer.write(body.getBytes());
		writer.flush();
		writer.close();

		// Response lesen
		DataInputStream input = new DataInputStream(connection.getInputStream());
		int len;
		byte[] buff = new byte[4096];

		while (-1 != (len = input.read(buff))) {
			log.info("XML-Response:" + nn + new String(buff, 0, len));
		}
	}
der nun noch vorhandene Fehler liegt wahrscheinlich an der datei selbst, Aber das were ich nun mal checken
Code:
<?xml version="1.0"  encoding="UTF-8"?>
<ARCXML version="1.1">
<RESPONSE>
<ERROR machine="blu3" processid="2632" threadid="2692">AXLParser: Fatal error at line 77, column 7. Message: Expected comment or processing instruction</ERROR>
</RESPONSE>
</ARCXML>
 
Ähnliche Java Themen

Ähnliche Java Themen


Oben