Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7639619
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T08:27:53+00:00 2026-05-31T08:27:53+00:00

I need to read an HTML response from a URL after sending it POST

  • 0

I need to read an HTML response from a URL after sending it POST data. I already have the following two functions but I don’t know how to combine them so that I can send POST data AND get the response.

This function gets a standard HTML response:

public static String getDataFromUrl(String url) throws IOException {
    System.out.println("------------FULL URL = " + url + " ------------");
    StringBuffer b = new StringBuffer();
    InputStream is = null;
    HttpConnection c = null;

    long len = 0;
    int ch = 0;
    c = (HttpConnection) Connector.open(url);
    is = c.openInputStream();
    len = c.getLength();
    if (len != -1) {
        // Read exactly Content-Length bytes
        for (int i = 0; i < len; i++)
            if ((ch = is.read()) != -1) {
                b.append((char) ch);
            }
    } else {
        // Read until the connection is closed.
        while ((ch = is.read()) != -1) {
            len = is.available();
            b.append((char) ch);
        }
    }

    is.close();
    c.close();
    return b.toString();
}

This function sends POST data to a URL:

void postViaHttpConnection(String url) throws IOException {
    HttpConnection c = null;
    InputStream is = null;
    OutputStream os = null;
    int rc;

    try {
        c = (HttpConnection) Connector.open(url);

        // Set the request method and headers
        c.setRequestMethod(HttpConnection.POST);
        c.setRequestProperty("If-Modified-Since", "29 Oct 1999 19:43:31 GMT");
        c.setRequestProperty("User-Agent", "Profile/MIDP-2.0 Configuration/CLDC-1.0");
        c.setRequestProperty("Content-Language", "en-US");

        // Getting the output stream may flush the headers
        os = c.openOutputStream();
        os.write("LIST games\n".getBytes());
        os.flush(); // Optional, getResponseCode will flush

        // Getting the response code will open the connection,
        // send the request, and read the HTTP response headers.
        // The headers are stored until requested.
        rc = c.getResponseCode();
        if (rc != HttpConnection.HTTP_OK) {
            throw new IOException("HTTP response code: " + rc);
        }

        is = c.openInputStream();

        // Get the ContentType
        String type = c.getType();
        processType(type);

        // Get the length and process the data
        int len = (int) c.getLength();
        if (len > 0) {
            int actual = 0;
            int bytesread = 0;
            byte[] data = new byte[len];
            while ((bytesread != len) && (actual != -1)) {
                actual = is.read(data, bytesread, len - bytesread);
                bytesread += actual;
            }
            process(data);
        } else {
            int ch;
            while ((ch = is.read()) != -1) {
                process((byte) ch);
            }
        }
    } catch (ClassCastException e) {
        throw new IllegalArgumentException("Not an HTTP URL");
    } finally {
        if (is != null)
            is.close();
        if (os != null)
            os.close();
        if (c != null)
            c.close();
    }
}

So I need to know how to combine them so that I can get the response AFTER sending POST data.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T08:27:54+00:00Added an answer on May 31, 2026 at 8:27 am

    Try this following code and let me know ant issues

    Note: Url extension is important (Here i am using for wifi only Ex:”;interface=wifi”)

    import java.io.InputStream;
    import java.io.OutputStream;
    
    import javax.microedition.io.Connector;
    import javax.microedition.io.HttpConnection;
    
    import net.rim.blackberry.api.browser.URLEncodedPostData;
    import net.rim.device.api.io.http.HttpProtocolConstants;
    
    public class HttpPostSample {
        HttpConnection hc = null;
        StringBuffer stringBuffer = new StringBuffer();
        InputStream inputStream;
    
        public HttpPostSample(String url) {
            try{
                hc = (HttpConnection)Connector.open(url+";interface=wifi");//you have to use connection extension ";interface=wifi" this is only for wifi 
                URLEncodedPostData oPostData = new URLEncodedPostData(URLEncodedPostData.DEFAULT_CHARSET, false);
                //These are your appending values and tags 
                oPostData.append("property_id","value");
                oPostData.append("property_name","value");
                oPostData.append("category","value");
                oPostData.append("address","value");
                oPostData.append("country","value");
                hc.setRequestMethod(HttpConnection.POST);
                hc.setRequestProperty(HttpProtocolConstants.HEADER_CONTENT_TYPE, oPostData.getContentType());
                byte [] postBytes = oPostData.getBytes();
                hc.setRequestProperty(HttpProtocolConstants.HEADER_CONTENT_LENGTH, Integer.toString(postBytes.length));
                OutputStream strmOut = hc.openOutputStream();
                strmOut.write(postBytes);
                strmOut.flush();
                strmOut.close();
    
                String returnMessage = hc.getResponseMessage();
                System.out.println("============"+returnMessage);
                if(hc.getResponseCode()==HttpConnection.HTTP_OK)
                {
                    inputStream = hc.openInputStream();
                    int c;
                    while((c=inputStream.read())!=-1)
                    {
                        stringBuffer.append((char)c);
                    }
                    System.out.println(">>>>>>>>>>>>>>>>>"+stringBuffer.toString());
                    parseResults(stringBuffer.toString());
    
                }else{
                    parseResults("ERROR");
                }
    
    
            }catch (Exception e) {
                // TODO: handle exception
            }
        }
        private void parseResults(String response)
        {
            if(response.equalsIgnoreCase("ERROR"))
            {
                System.out.println("Error in Connection please check your internet and Connection extension");
            }else{
                System.out.println(response);
            }
        }
    
    }
    

    You can call above function as

    HttpPostSample post=new HttpPostSample(url);
    

    you can see the output in console parseResults Method

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to read the data from a file that can be either comma
I need to read small sequences of data from a 3.7 GB file. The
I have been trying to strip out some data from HTML files. I have
I want to read a source code (HTML tags) of a given URL from
I need to read and serialize objects from and to XML, Apple's .plist format
I need to read the output from ffmpeg in order to even try the
I need to read A Practical Introduction to Data Structures and Algorithm Analysis by
I need to read a setting from the appsettings section (defined in app.config) in
I am new to MySQLdb. I need to read values from a pre-defined database
I need some help pulling cell data from table when a user clicks the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.