canot find sybol : method write(byte[]) - urgent help
Hello Guys;
I have a simple Java code tha post xml to another url.
I'm getting an error I'm not sure how to resolve.
the error states:
Code:
cannot ind symbol
symbol : method write(byte[])
location: calss java.io.OutputStreamWriter
out.write(request.getBytes(sendEncoding));
^
My Code:
Code:
static int port = 80;
static String hostname = "xx.xx.xx.xx";
static List<String> IDs = new ArrayList<String>(); //id for the download
static List<String> statuses = new ArrayList<String>(); //http status for the download request 200,201,204,400
static List<String> filenames=new ArrayList<String>();
static List<String> sites=new ArrayList<String>();
static String outputFolder = "/xxx/input_data/xxx/";
boolean downloadFailed = false; //if a download fails in the download() function, wait 5 mins then retry
public String push2(String XML_IN) {
String response = "";
String request = XML_IN;
String sendEncoding = "UTF8";
HttpURLConnection urlConn = null;
OutputStreamWriter out = null;
InputStream in = null;
try {
URL url = new URL("http://url");
urlConn = (HttpURLConnection)url.openConnection();
urlConn.setRequestMethod("POST");
urlConn.setDoOutput(true);
urlConn.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
urlConn.setRequestProperty("Accept","application/xml");
urlConn.setRequestProperty("charset","UTF-8");
urlConn.connect();
System.out.println("CATCH-->Entering push mode");
out = new OutputStreamWriter(urlConn.getOutputStream());
out.write(request.getBytes(sendEncoding));
out.flush();
out.close();
int status = urlConn.getResponseCode();
System.out.println("Status code:" + status);
//if http code 200 is not returned, there's an error.
if (status!=200) {
in = urlConn.getErrorStream();
}
else {
in = urlConn.getInputStream();
}
InputStreamReader inReader = new InputStreamReader(in, sendEncoding);
StringBuffer responseBuffer = new StringBuffer();
char[] buffer = new char[1024];
int bytes;
while ((bytes = inReader.read(buffer)) != -1) {
responseBuffer.append(buffer, 0, bytes);
}
response = responseBuffer.toString();
System.out.println(response);
if (status!=200) {
failSession("HTTP status code wasn't 200, so must be something wrong - FAILED");
}
}
catch (Exception e) {
e.printStackTrace();
}
finally {
if (out != null) {
try {out.close();} catch (Exception e) {}
}
if (in != null) {
try {in.close();} catch (Exception e) {e.printStackTrace();}
}
if (urlConn != null) {
urlConn.disconnect();
}
}
return response;
}
Re: canot find sybol : method write(byte[]) - urgent help
Have you looked at the API for OutputStreamWriter? If not, why not?
Re: canot find sybol : method write(byte[]) - urgent help
Quote:
Originally Posted by
Fubarable
Have you looked at the API for OutputStreamWriter? If not, why not?
I have looked at it, still lead me to no solution. I'm a php programmer and have not touched Java for many years, so would appreciate any specific pointers or code example.
Re: canot find sybol : method write(byte[]) - urgent help
Here's the API then.
As you can see there is no write method that takes byte[].
And that's no great surprise since a Writer write characters, not bytes.
Re: canot find sybol : method write(byte[]) - urgent help
thanks.
I've changed this bit:
out.write(request.getBytes(sendEncoding));
to just:
out.write(request);
while this works, I also needed to apply some url_encoding to the xml string being sent, and while it complies sucessfuly, when running it I get: FATAL ERROR : Caught a fatal signal or exception
this is how I'm url encoding it:
Code:
URL url = new URL("http://xxxxxx/");
urlConn = (HttpURLConnection)url.openConnection();
urlConn.setRequestMethod("POST");
urlConn.setDoOutput(true);
urlConn.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
urlConn.setRequestProperty("Accept","application/xml");
urlConn.setRequestProperty("charset","UTF-8");
//urlConn.setRequestProperty("Content-Type","application/xml;charset=" + sendEncoding);
urlConn.connect();
System.out.println("CATCH-->Entering push mode");
out = new OutputStreamWriter(urlConn.getOutputStream());
//out.write(request.getBytes(sendEncoding));URLEncoder.encode(request,"UTF-8")
String urlEncode = URLEncoder.encode(request,"UTF-8");
out.write(urlEncode);
System.out.println("CATCH-->XML Request:" + urlEncode);
out.flush();
out.close();
Re: canot find sybol : method write(byte[]) - urgent help
What's the full error plus stack trace.
If it's a JVM crash then that's something we really wouldn't be able to handle here.
Re: canot find sybol : method write(byte[]) - urgent help
thats the only error available to me.
my issue is that, the code works fine until I url_encode it. we'll it kind of works, it successfuly posts the xml, but I'm getting a status code 500 back and I think I need to url encode me xml request and that's what the stickig point is. Any insight or other way of doing would be much appriciated.
Re: canot find sybol : method write(byte[]) - urgent help
I'd look at the other end and see what the error is.
Assuming you have access to wherever it is you are sending this stuff.
Re: canot find sybol : method write(byte[]) - urgent help
Thanks Tolls.
From your opinon am I setting all the correct urlConn.setRequestProperty headers in my code above?
Re: canot find sybol : method write(byte[]) - urgent help
No idea.
I only ever look at these things when I'm actually doing them.
I rarely bother remembering the ins and outs...and then I test it and tweak as needed.
Re: canot find sybol : method write(byte[]) - urgent help