How to send a HTTP request to servlet in java application

I'm new in Java. I need to send a HTTP request with parameters to servlet in a java aplication. Here is my code. It can be compiled but always threw an exceptions when I ran it. Can anyone help?
package coreservlets;
import java.io.*;
import java.net.*;
public class PostHTTP
     public static void main(String args[])
          throws IOException, UnknownHostException {
          try
          // URL and servlet
               URL myURL = new URL("http://pc076/servlet/coreservlets.OffHold");
               URLConnection c = myURL.openConnection();
               c.setUseCaches(false);
               c.setDoOutput(true);
               ByteArrayOutputStream byteStream = new ByteArrayOutputStream(512);
               PrintWriter out = new PrintWriter(byteStream, true);
//parameters
               String postData = "REASON_CODE=3B&RSPCODE=JSmith&CASENUM=NA795401&REPLY=123&SOURCE=XYZ&REPLYLINK=http://pc076/servlet/coreservlets.ShowParameters";
               out.print(postData);
               out.flush();
               String lengthString = String.valueOf(byteStream.size());
               c.setRequestProperty("Content-Length", lengthString);
               c.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
               byteStream.writeTo(c.getOutputStream());
               BufferedReader in =     new BufferedReader(new InputStreamReader
                                             (c.getInputStream()));
               String line;
               //String linefeed = "\n";
               //resultsArea.setText("");
               while((line = in.readLine()) != null) {
                    System.out.println(line);
                    //resultsArea.append(linefeed);
          catch(IOException ioe) {
          // Print debug info in Java Console
          System.out.println("IOException: " + ioe);

here are some updates to your code I haven't tested it running
post again if you still have trouble
URL myURL = new URL("http://pc076/servlet/coreservlets.OffHold");
HttpURLConnection c = (HttpURLConnection)myURL.openConnection();
c.setDoInput(true);
c.setDoOutput(true);
ByteArrayOutputStream byteStream = new ByteArrayOutputStream(512);
String lengthString = String.valueOf(byteStream.size());
c.setRequestProperty("Content-Length", lengthString);
c.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
PrintWriter out = new PrintWriter(byteStream, true);
//parameters
String postData = "REASON_CODE=3B&RSPCODE=JSmith&CASENUM=NA795401&REPLY=123&SOURCE=XYZ&REPLYLINK=http://pc076/servlet/coreservlets.ShowParameters";
out.print(postData);
out.flush();
byteStream.writeTo(c.getOutputStream());
// connect
c.connect();
BufferedReader in = new BufferedReader(new InputStreamReader
(c.getInputStream()));
String line;
while((line = in.readLine()) != null)
    System.out.println(line);

Similar Messages

  • How to send a http request to a non java appln

    I have one legacy web application running which can handle only http request . So I need to connect (using http request) and send my request and get the response, without opening that application in browser.
    Any idea
    1) How to connect to non - java appln from a java appln ?
    2) How to use the Httprequest without displaying the page using browser?

    You can try one of three routes:
    Open a socket and write the HTTP request and parse the HTTP response yourself
    Use java.net.URLConnection
    Use Jakarta Common's HttpClient at jakarta.apache.org- Saish

  • How to send a https request using jsf

    hi
    Can anybody tell a sample how to send a https request using JSF ...
    thanks

    Prefix the request URI with the https protocol.
    The answer is too easy and straightforward that I guess that you mean something else. If you just want to know how to configure an SSL environment for your own webapplication, refer to the Java EE tutorial chapter 28: http://java.sun.com/javaee/5/docs/tutorial/doc/

  • How to pass a HTTP request from a simple java class

    Is it possible to pass an HTTP request from a simple java class.if yes how?

    Is it possible to pass an HTTP request from a simple
    java class.if yes how?If you're talking about creating a HttpRequest object and passing it to a servlet, that would be a red flag to me that your design is flawed. You shouldn't have to do that - the application server (Tomcat, Weblogic, etc) should be the only thing that has to worry about creating that kind of object and passing it to you.

  • How to send a http-request from abap?

    I want to send a xml file through a http-request ,what should i do?

    Welcome to SDN
    you can use cl_http_client class to do that. search the abap general forum with key word cl_http_client and you will find lot of examples.
    Regards
    Raja

  • How to send POST HTTP Request through PI .

    Hi ,
    I am trying to send a XML mesage at HTTP server from SAP PI 7.1 .
    but not able to , reason is HTTP guy telling me is that ,i am sending a get request through SAP PI 7.1 and it should be POST.
    Where to change this this thing , so that only post request should go.
    There is one more thing , i am facing following request only in Quality . In Development request is going as Post and every thing running fine ...
    Regards
    PS

    It was always HTTP from our end , some config was missing at HTTP guys end , which solve the problem ..
    So there was no issue at PI end.

  • How does Flash send outgoing HTTP request to server?

    Hello everyone,
    I would like to know how Flash sends outgoing HTTP requests.
    I would like to know when from the inside of a Flash SWF file
    request for another SWF or FLV file is made; how the Flash sends
    this request to server.
    Thank you very much and have a great day,
    Khoramdin

    Hello everyone,
    I would like to know how Flash sends outgoing HTTP requests.
    I would like to know when from the inside of a Flash SWF file
    request for another SWF or FLV file is made; how the Flash sends
    this request to server.
    Thank you very much and have a great day,
    Khoramdin

  • How to transfer the http request from applet to servlet/jsp

    I use the JTree component to create a navigation of a website,but i don't
    know how to connect the tree's nodes with the jsp/servlet.
    I mean how to transfer the http request from the applet to a jsp.
    I use the "<frameset>" mark which will divide the web browse into 2 blocks.
    The left side is Applet,and the right side is the linked jsp page.
    what I say is as the weblogic console layout.
    who can help me!!!
    Thank You!

    I use the JTree component to create a navigation of a website,but i don't
    know how to connect the tree's nodes with the jsp/servlet.
    I mean how to transfer the http request from the applet to a jsp.
    I use the "<frameset>" mark which will divide the web browse into 2 blocks.
    The left side is Applet,and the right side is the linked jsp page.
    what I say is as the weblogic console layout.
    who can help me!!!
    Thank You!

  • Sending an http request

    Hi,
    I want to send an http request with a string(this is an xml string)which will give me a response which contains an xml string again.Can anybody tell me how can i do it in java.Any sample code will be a great help
    thanks in advance.

    This is a example, hope to be useful for you.
    String xml = "<?xml version="1.0" encoding="GBK"?>"
    + "<operation_info>"
    + "<request_code>11000008</request_code>"
    + "<id_number>13148709165</id_number>"
    + "<id_type>2</id_type>"
    + "<session_id>100000002226</session_id>"
    + "<device_uid>100000000005</device_uid>"
    + "</operation_info>";
    String http_url = "http://127.0.0.1:8888/protocol";
    URL sendUrl = new URL(http_url);
    URLConnection urlCon = sendUrl.openConnection();
    urlCon.setDoOutput(true);
    urlCon.setDoInput(true);
    HttpURLConnection httpConnection = (HttpURLConnection) urlCon;
    httpConnection.setRequestMethod("POST");
    httpConnection.setRequestProperty("Content-Type", "text/xml;charset=GBK");
    httpConnection.setRequestProperty("Content-Length", Integer.toString(xml.length()));
    PrintStream ps=null;
    try {
    ps = new PrintStream(httpConnection.getOutputStream());
    }catch(java.net.ConnectException e){
    e.printStackTrace();
    return "error"; ;
    xml = URLEncoder.encode(xml);
    try{
    ps.write(xml.getBytes());
    ps.flush();
    //get response stream
    String str = httpConnection.getResponseMessage();
    InputStream is = httpConnection.getInputStream();
    InputStreamReader isr = new InputStreamReader(is);
    BufferedReader br = new BufferedReader(isr);
    str = "";
    StringBuffer sb = new StringBuffer();
    while ((str = br.readLine())!=null) {
    sb.append(str);
    is.close();
    return sb.toString();
    }catch(Exception e){
    e.printStackTrace();
    return "error"; //exception
    }

  • HT4314 How to send a game request to my friends

    How to send a game request to my friends

    Hi,
    How about your issue now? Is it fixed?
    I think you will get progessional support from other network related forum. Because VC++ forum aims to discuss and ask questions about the Visual C++ IDE, libraries, samples, tools, setup, and Windows programming using MFC and ATL.
    Hope you can understand.
    May
    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click
    HERE to participate the survey.

  • How to send a multicast request to 239.255.255.253, seeking an SLP Directory Agent (DA)?

    Hi,
    How to send a multicast request to 239.255.255.253, seeking an SLP Directory Agent (DA) in C++?
    Thanks in advance.

    Hi,
    How about your issue now? Is it fixed?
    I think you will get progessional support from other network related forum. Because VC++ forum aims to discuss and ask questions about the Visual C++ IDE, libraries, samples, tools, setup, and Windows programming using MFC and ATL.
    Hope you can understand.
    May
    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click
    HERE to participate the survey.

  • How to send a Meeting Request to Exchange/Outlook using JavaMail

    Hi,
    Can any body help me "How to send a Meeting Request to Exchange/Outlook using JavaMail" which will be added to the calendar items.
    If possible, please provide me with the sample code. Please send the sample code to the following mail id: "[email protected]".
    Thanks in advance,
    Ashok.

    I don't have a homework problem. I have a real business need for this, but haven't been able to get my code to work yet. There are at least two references out there, but either the sample code hasn't been posted or I don't know where to look. Can you help?
    Thanks.

  • Cannot process an HTTP request to servlet [Faces Servlet] in [wcb] web application

    Hi All
    l am working on a Wcem 3.0 with Trex with ERP and WAS 7.3.
    When I logon to http://<host>:<port>/wcb/index.html url I am able to see wcbuilder_erp & wcbuilder_erp_ume application ids.
    But suddenly I am getting The website cannot display the page error message when I access the above mentioned URL.
    When I verify the developer log trace it showing as 500 Internal sever error issue.[EXCEPTION] java.lang.VerifyError: Bad return type
    In defaultTrace.trc file it is showing as Cannot process an HTTP request to servlet [Faces Servlet] in [wcb] web application exception.
    09 10 18:08:56:098#+0530#Error#com.sap.engine.services.servlets_jsp.server.HttpHandlerImpl#
    com.sap.ASJ.web.000137#WEC-APP-BF#sap.com/wec~comm~wcb~leanapp#C0000A229CA7094A0000000000001188#2392750000000004#sap.com/wec~comm~wcb~leanapp#com.sap.engine.services.servlets_jsp.server.HttpHandlerImpl#Guest#0##CFAFBF4C38E411E4B4750000002482AE#ffbcbcdd38e611e49d020000002482ae#ffbcbcdd38e611e49d020000002482ae#0#Thread[HTTP Worker [@455349581],5,Dedicated_Application_Thread]#Plain##
    Cannot process an HTTP request to servlet [Faces Servlet] in [wcb] web application.
    [EXCEPTION] java.lang.VerifyError: Bad return type
    Exception Details:
    Location: com/sap/wec/tc/core/runtime/jsf/resource/WecExternalContextFactory.getExternalContext(Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;)Ljavax/faces/context/ExternalContext; @17: areturn
    Reason: Type 'com/sap/wec/tc/core/runtime/jsf/resource/WecExternalContext' (current frame, stack[0]) is not assignable to 'javax/faces/context/ExternalContext' (from method signature)
    Current Frame:
        locals: { 'com/sap/wec/tc/core/runtime/jsf/resource/WecExternalContextFactory', 'java/lang/Object', 'java/lang/Object', 'java/lang/Object' }
        stack: { 'com/sap/wec/tc/core/runtime/jsf/resource/WecExternalContext' }
      Bytecode:
        0000000: bb00 0359 2ab6 0004 2b2c 2db6 0005 b700
        0000010: 06b0                                
        at java.lang.Class.getDeclaredConstructors0(Native Method)
        at java.lang.Class.privateGetDeclaredConstructors(Class.java:2446)
        at java.lang.Class.getConstructor0(Class.java:2756)
        at java.lang.Class.getConstructor(Class.java:1693)
    Any help?
    Regards
    Rami Reddy

    Hi Steffen,
    Thanks for promt response.
    But when I checked with Basis team as of now there are not yet configured TREX, only WCEM components has been configured in WAS 7.3. When they are configuring WCEM components HTTPS protocol not configured.
    But for past 2 weeks I am able to see the below screen using http://<host>:<port>/wcb/index.html url.
    But now I am getting The website cannot display the page error.
    Please clarify me if anything is wrong from my side. And also pl let us know which configurations I have to check for fixing the issue.
    Regards,
    Rami Reddy

  • Is it possible to send several http requests at the same time?

    hi:
    is it possible to send several http requests at the same time in j2me application, or it's device specific.
    It's ok in my NOKIA SYMBIAN C++ application.
    regards
    Message was edited by:
    danielwang

    Is it possible to have 2 threads running at the same
    time at different times eg 1 repeats every 20
    miliseconds and the other 40 for example. Yes.
    http://java.sun.com/docs/books/tutorial/essential/concurrency/index.html

  • How to send data to bam data object through java code

    how to send data to bam data object through java code

    I've made a suggestion in other thread: https://forums.oracle.com/thread/2560276
    You can invoke BAM Webservices (Using Oracle BAM Web Services) or use JMS integration using Enterprise Message Sources (http://docs.oracle.com/cd/E17904_01/integration.1111/e10224/bam_ent_msg_sources.htm)
    Regards
    Luis Fernando Heckler

Maybe you are looking for

  • How to set the cell size in a JFrame

    Hello. Is there any way of setting the cell size in a JFrame. The reason why I want to set the cell size is that I want to be sure of their location that when I am placing components on the JFrame. So is there any way of setting the cell size beforeh

  • Extended class variable corruption...why?

    hello guys, I've beed struggling on this issue for more than 2 hours and, together with a friend, we weren't able to figure it out. I have a class named HandlerMod that extends DefaultHandler (this one org.xml.sax.helpers.DefaultHandler) and in this

  • Searching for a Script...

    I am in the process of making a "Deal or No Deal" type DVD program in DVDSP. I am going to rig it (i.e. the third briefcase has $500 regardless of the case's number) The problem I am running into is that I'm not sure how to "turn off" a case after it

  • Slideshow keeps repeating same photo

    On slide show the same photo keeps repeating, ive tried adjusting settings but can't fix problem thanks

  • Why does LabVIEW for Lego Mindstorms have 2 Functions Palettes?

    LabVIEW for Lego Mindstorms has 2 separate Functions Palettes.  One of them appears when one creates a VI (.vi) for a Project (.lvrbt), and it appears to contain Mindstorms NXT functions.  The other appears when one creates a stand alone VI (.vi), an