1
**While Send  data on Post method getting below error.
I have  validated  xml file its well formed no  syntax error.i have tired apche and Http rest   both of them. getting same error.** 
Error ErrorCode="org.xml.sax.SAXParseException"
ErrorDescription="" ErrorRelatedMoreInfo="Content is not allowed in prolog.">
<Stack>org.xml.sax.SAXParseException: Content is not allowed in prolog.
Below  Code  i am using for  sending data over Post http (rest service).

below is code for that.

public String postHttp(String data, String urlString, String acceptString, String contentTypeString) throws Exception {

            HttpURLConnection conn = null;
            PrintWriter writer = null;
            BufferedReader reader = null;
            if (log.isVerboseEnabled()) {
                log.verbose("Posting data over HTTP.URL:  " + urlString + " Data posted: data: " + data);
            }
            String output = null;
            URL url = new URL(urlString);
            if (log.isDebugEnabled())
                log.debug("The Url is --->" + url);
            try {
                conn = (HttpURLConnection) url.openConnection();
                conn.setDoOutput(true);
                conn.setDoInput(true);
                // This change is made to support XML in Http POST
                if (!YFCObject.isNull(acceptString) && !YFCObject.isNull(contentTypeString)) {
                    conn.setRequestProperty("Accept",acceptString);
                    conn.setRequestProperty("Content-Type",contentTypeString );
                }
                conn.connect();
                writer = new PrintWriter(conn.getOutputStream());
                writer.println(data);
                writer.flush();
                // Get the response
                responseCode = conn.getResponseCode();
                responseMessage = conn.getResponseMessage();
               // TODO: Need to validate. Should this be only for 200?
                if (responseCode==200) {
                    log.verbose("Posting data over HTTP completed.URL:  " + urlString + " Response: " + responseMessage);
                    System.out.println("Posting data over HTTP completed.URL:  "+ urlString + " Response Status: " + responseCode);
                }
                else if (responseCode < 400) {
                    reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
                    output = getResponseData(reader);
                }
                else if (responseCode >= 400){
                    reader = new BufferedReader(new InputStreamReader(conn.getErrorStream()));
                    output = getHTTPStatusErrorOutput(responseCode, responseMessage);
                }

                if (log.isVerboseEnabled()) {
                    log.verbose("Posting data over HTTP completed.URL:  " + urlString + " Response data: " + output);
                }

            } catch (MalformedURLException mfue) {
                output = getErrorOutput(mfue);
            } catch (UnknownHostException uhex) {
                output = getErrorOutput(uhex);
            } catch (Exception ex) {
                output = getErrorOutput(ex);
            } finally {
                if (conn != null) {
                    conn.disconnect();
                    if (writer != null)
                        writer.close();
                    if (reader != null)
                        reader.close();
                }

            }

            return output;

        }

Could you tell is their any mistake in that way.some time its is hang on this point conn.setDoOutput(true); web.postHttp(doc,url,"application/xml","application/xml"); In rest side we are unable to getting any messages. Any help would be appreciated.

  • 1
    Possible duplicate of [SAXParseException: Content is not allowed in prolog](http://stackoverflow.com/questions/20551572/saxparseexception-content-is-not-allowed-in-prolog) – Alexey Soshin Nov 11 '16 at 10:53

0 Answers0