Solved

Post JSP String to Remote php (URL)

Posted on 2009-07-09
9
464 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
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

821 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