Improve company productivity with a Business Account.Sign Up

x
?
Solved

Post JSP String to Remote php (URL)

Posted on 2009-07-09
9
Medium Priority
?
479 Views
Last Modified: 2013-12-13
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
0
Comment
Question by:digarati
  • 3
  • 2
5 Comments
 
LVL 1

Author Comment

by:digarati
ID: 24818414
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
 
LVL 1

Author Comment

by:digarati
ID: 24818555
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
 
LVL 27

Accepted Solution

by:
mrcoffee365 earned 2000 total points
ID: 24821994
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
 
LVL 27

Expert Comment

by:mrcoffee365
ID: 25065864
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
 
LVL 27

Expert Comment

by:mrcoffee365
ID: 25075842
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

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

This article discusses four methods for overlaying images in a container on a web page
This holiday season, we’re giving away the gift of knowledge—tech knowledge, that is. Keep reading to see what hacks, tips, and trends we have wrapped and waiting for you under the tree.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

585 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question