Solved

Post JSP String to Remote php (URL)

Posted on 2009-07-09
9
444 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
9 Comments
 
LVL 1

Author Comment

by:digarati
Comment Utility
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
Comment Utility
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 26

Accepted Solution

by:
mrcoffee365 earned 500 total points
Comment Utility
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 26

Expert Comment

by:mrcoffee365
Comment Utility
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 26

Expert Comment

by:mrcoffee365
Comment Utility
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Introduction Many web sites contain image galleries; a common design for these galleries includes a page with a collection of thumbnail images.  You can click on each of the thumbnail images to see the larger version of the image.  This is easily i…
Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

728 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now