Cannot write output after reading input

4 respostas
F

Alguém tem idéia do porque desse erro???

private static Document SendRequest(HttpsURLConnection connection,
			String requestToken, String siteID, String catID, String pageNumber) {

		try {
			// Get the XML file into a Document object
			DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
			DocumentBuilder docBuild = factory.newDocumentBuilder();
			Document xmlDoc = docBuild.parse(new File(xmlFileLocation));

			// Set the values of the nodes
			xmlDoc.getElementsByTagName("eBayAuthToken").item(0).getChildNodes().item(0).setNodeValue(requestToken);
			// xmlDoc.getElementsByTagName("Site").item(0).getChildNodes().item(0).setNodeValue(siteID);
			xmlDoc.getElementsByTagName("CategoryID").item(0).getChildNodes().item(0).setNodeValue(catID);
			xmlDoc.getElementsByTagName("EntriesPerPage").item(0).getChildNodes().item(0).setNodeValue(entriesPerPage);
			xmlDoc.getElementsByTagName("PageNumber").item(0).getChildNodes().item(0).setNodeValue(pageNumber);

			// Get the output stream of the connection
			OutputStream out = connection.getOutputStream();
						
			// Transform and write the Document to the stream
			TransformerFactory tf = TransformerFactory.newInstance();
			Transformer tr = tf.newTransformer();
			Source input = new DOMSource(xmlDoc);
			Result output = new StreamResult(out);
			tr.transform(input, output);
			out.flush();			
			out.close();
			
			connection.disconnect();

			// Get the Input stream for the response
			InputStream in = connection.getInputStream();
				
			Document response = docBuild.parse(in);
			in.close();
			in = null;
			connection.disconnect();
			// return the response XML Document
			return response;

		} catch (IOException e) {
			System.out.println("SendRequest IO Error: " + e.toString());
			e.printStackTrace();
			return null;
		} catch (Exception e) {
			System.out.println("Error Sending Request: " + e.toString());
			e.printStackTrace();
			return null;
		}
	}

Quando acesso pela primeira vez.. tudo bem.. funciona.. mas quando eu acesso pela segunda vez... (fiz um for) ele da a seguinte exeção:

SendRequest IO Error: java.net.ProtocolException: Cannot write output after reading input.java.net.ProtocolException: Cannot write output after reading input.

	at sun.net.www.protocol.http.HttpURLConnection.getOutputStream(Unknown Source)
	at sun.net.www.protocol.https.HttpsURLConnectionImpl.getOutputStream(Unknown Source)
	at GetCategoryListings.GetCategoryListingsModified.SendRequest(GetCategoryListingsModified.java:205)
	at GetCategoryListings.GetCategoryListingsModified.main(GetCategoryListingsModified.java:91)

Ajudem!!!

4 Respostas

I

Como não dá pra saber exatamente em que linha está ocorrendo o problema pelo teu post, suponho que seja isto:

//...
connection.disconnect();

// Get the Input stream for the response
InputStream in = connection.getInputStream(); 
//...

Não sei não… mas fechando a conexão e depois tentar pegar um stream sobre ela acho que não dá não… T+

F

Opa. foi mal…

Na verdade não é bem isso. Na verdade eu coloquei isso pra ver no que da…

Mas o erro esta nessa linha:

// Get the output stream of the connection OutputStream out = connection.getOutputStream();

I

De onde vem este objeto connection? Por acaso antes de passar ele para este método tu já não escreveu nele? Dá uma conferida, T+

F

Funcionou cara… valew mesmo!

Criado 14 de dezembro de 2006
Ultima resposta 14 de dez. de 2006
Respostas 4
Participantes 2