Post JSP String to Remote php (URL)

i have a string:

String msgText1="<connector><function name='contact_create' id='contact_id'><parameter name='args' type='pair'><pair name='name' type='pair'><pair name='first' type='string'>"+LINES[i][0]+"</pair><pair name='last' type='string'>"+LINES[i][1]+"</pair></pair><pair name='addr' type='pair'><pair name='city' type='string'>"+LINES[i][5]+"</pair><pair name='country_id' type='integer'>1</pair><pair name='prov_id' type='integer'>13</pair><pair name='postal_code' type='string'>"+LINES[i][7]+"</pair><pair name='street' type='string'>"+LINES[i][4]+"</pair></pair><pair name='email' type='pair'><pair name='addr' type='string'>"+LINES[i][11]+"</pair></parameter></function></connector>";

I want this string to be posted to a .php script on another site.

How do i do this with .JSP?

The PHP script is looking for two values: sec_string and xml_doc
LVL 1
digaratiAsked:
Who is Participating?
 
mrcoffee365Connect With a Mentor Commented:
You might want something like this:

<%@ page language="java" import="java.net.*"%>
<%

String msgText1="<connector><function name='contact_create' id='contact_id'>";
String [] line1 = {"John","Smith","","","3 Main St.","New York","NY","10101","","","","addr"};
String [] line2 = {"Mary","Jones","","","4 Main St.","New York","NY","10101","","","","addr2"};
String[][] LINES = new String[2][11];
LINES[0] = line1;
LINES[1] = line2;
for(int i=0;i<2;i++) {
      msgText1 += "<parameter name='args' type='pair'><pair name='name' type='pair'><pair name='first' type='string'>"+LINES[i][0]+"</pair><pair name='last' type='string'>"+LINES[i][1]+"</pair></pair><pair name='addr' type='pair'><pair name='city' type='string'>"+LINES[i][5]+"</pair><pair name='country_id' type='integer'>1</pair><pair name='prov_id' type='integer'>13</pair><pair name='postal_code' type='string'>"+LINES[i][7]+"</pair><pair name='street' type='string'>"+LINES[i][4]+"</pair></pair><pair name='email' type='pair'><pair name='addr' type='string'>"+LINES[i][11]+"</pair></parameter>";
}
msgText1 += "</function></connector>";

System.out.println(msgText1);

try{
    String sec_string = "password" ;
    String xml_doc = "";
    java.net.URL url = new java.net.URL("http://site.com/parse.php");
    java.net.HttpURLConnection conn = (java.net.HttpURLConnection)url.openConnection();
    conn.setRequestMethod("POST");
    conn.setRequestProperty("Content-Length", "" + Integer.toString(xml_doc.getBytes().length));
    conn.setRequestProperty("Content-Language", "en-US");
    conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
    conn.setDoInput(true);
    conn.setDoOutput(true);
    conn.setUseCaches(false);
    conn.setAllowUserInteraction(true);
    java.io.DataOutputStream printout = new java.io.DataOutputStream (conn.getOutputStream ());
    printout.writeBytes (sec_string);
    printout.writeBytes (msgText1);
    printout.flush();
    printout.close();
    java.io.BufferedReader in = new java.io.BufferedReader(new java.io.InputStreamReader(conn.getInputStream()));
    while ((sec_string = in.readLine()) != null)
    {
         sec_string += msgText1;
    }
    in.close();

    out.println("Success");
}
catch (Exception e)
{
   out.println("Error");
}

%>
0
 
digaratiAuthor Commented:
Here is what i have put together so far:

  <%@ page language="java" import="java.net.*"

try{
    String sec_string = password;
    String xml = "";
    String parameters = "&message=hello+world&";
    java.net.URL url = new java.net.URL("http://site.com/parse.php");
    java.net.HttpURLConnection conn = (java.net.HttpURLConnection)url.openConnection();
    conn.setRequestMethod("POST");
    conn.setRequestProperty("Content-Length", "" + Integer.toString(parameters.getBytes().length));
    conn.setRequestProperty("Content-Language", "en-US");
    conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
    conn.setDoInput(true);
    conn.setDoOutput(true);
    conn.setUseCaches(false);
    conn.setAllowUserInteraction(true);
    java.io.DataOutputStream printout = new java.io.DataOutputStream (conn.getOutputStream ());
    printout.writeBytes (parameters);
    printout.flush ();
    printout.close ();
    java.io.BufferedReader in = new java.io.BufferedReader(new java.io.InputStreamReader(conn.getInputStream()));
    while ((stuff = in.readLine()) != null)
    {
         sec_string += xml;
    }
    in.close();

    out.println("Success")
}
catch (Exception e)
{
   out.println("Error")
}
%>

My Error:

Parse Error in JSP page: Lexical error at line 3, column 4.  Encountered: "{" (123), after : ""
allaire.jrun.jsp.ParseException: Lexical error at line 3, column 4.  Encountered: "{" (123), after : ""
      at allaire.jrun.jsp.JSPParser.writeXML(JSPParser.java:137)
      at allaire.jrun.jsp.JSPParser.generateXML(JSPParser.java:107)
      at allaire.jrun.jsp.JSPParser.generateXML(JSPParser.java:51)
      at allaire.jrun.jsp.Translator.parsePage(Translator.java:192)
      at allaire.jrun.jsp.Translator.translate(Translator.java:390)
      at allaire.jrun.jsp.Translator.translate(Translator.java:92)
      at allaire.jrun.jsp.JSPTranslator.translate(JSPTranslator.java:75)
      at allaire.jrun.jsp.JSPTranslator.translate(JSPTranslator.java:67)
      at allaire.jrun.jsp.JSPServlet.service(JSPServlet.java:216)
      at allaire.jrun.jsp.JSPServlet.service(JSPServlet.java:196)
      at allaire.jrun.servlet.JRunSE.service(JRunSE.java:1416)
      at allaire.jrun.session.JRunSessionService.service(JRunSessionService.java:1082)
      at allaire.jrun.servlet.JRunSE.runServlet(JRunSE.java:1270)
      at allaire.jrun.servlet.JRunRequestDispatcher.forward(JRunRequestDispatcher.java:89)
      at allaire.jrun.servlet.JRunSE.service(JRunSE.java:1552)
      at allaire.jrun.servlet.JRunSE.service(JRunSE.java:1542)
      at allaire.jrun.servlet.JvmContext.dispatch(JvmContext.java:364)
      at allaire.jrun.jrpp.ProxyEndpoint.run(ProxyEndpoint.java:388)
      at allaire.jrun.ThreadPool.run(ThreadPool.java:272)
      at allaire.jrun.WorkerThread.run(WorkerThread.java:75)
0
 
digaratiAuthor Commented:
My current code:

try{
    String sec_string = "password" ;
    String xml_doc = "";
    java.net.URL url = new java.net.URL("http://site.com/parse.php");
    java.net.HttpURLConnection conn = (java.net.HttpURLConnection)url.openConnection();
    conn.setRequestMethod("POST");
    conn.setRequestProperty("Content-Length", "" + Integer.toString(xml_doc.getBytes().length));
    conn.setRequestProperty("Content-Language", "en-US");
    conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
    conn.setDoInput(true);
    conn.setDoOutput(true);
    conn.setUseCaches(false);
    conn.setAllowUserInteraction(true);
    java.io.DataOutputStream printout = new java.io.DataOutputStream (conn.getOutputStream ());
    printout.writeBytes (sec_string);
      printout.writeBytes (xml);
    printout.flush();
    printout.close();
    java.io.BufferedReader in = new java.io.BufferedReader(new java.io.InputStreamReader(conn.getInputStream()));
    while ((sec_string = in.readLine()) != null)
    {
         sec_string += xml_doc;
    }
    in.close();

    out.println("Success");
}
catch (Exception e)
{
   out.println("Error");
}
0
 
mrcoffee365Commented:
The code I posted compiles and works, so I don't see why the question has to be deleted.  Perhaps others would find it useful.
0
 
mrcoffee365Commented:
Okay, my post above with code solving the problem would be a good answer, so I choose #3 and the post id is: ID:24821994
0
All Courses

From novice to tech pro — start learning today.