Solved

Post JSP String to Remote php (URL)

Posted on 2009-07-09
9
470 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
9 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 500 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this. Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it i…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
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…
The viewer will learn how to dynamically set the form action using jQuery.

623 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