开发者

how to get data to a servlet which is send using httpcommunicator with post method

I am trying to send data using httpcommunicator class. here is my code.

public String postData(String address,String dataToBePosted) throws MalformedURLException,IOException,ProtocolException{

    /** set up the http connection parameters */
    HttpURLConnection    urlc = (HttpURLConnection) (new URL(address)).openConnection();
    urlc.setRequestMethod("POST");
    urlc.setDoOutput(true);
    urlc.setDoInput(true);
    urlc.setUseCaches(false);
    urlc.setAllowUserInteraction(false);
    urlc.setRequestProperty("Content-type", "text/xml; charset=" + "UTF-8");

    /** post the data */
    OutputStream out = null;
    out = urlc.getOutputStream();
    Writer writer = new OutputStreamWriter(out, "UTF-8");
    writer.write(dataToBePosted);
    writer.close();
    out.close();

    /** read the response back from the posted data */
    BufferedReader bfreader = new BufferedReader(new InputStreamReader(urlc.getInput开发者_运维问答Stream()));
    StringBuilder builder = new StringBuilder(100);
    String line = "";
    while ((line = bfreader.readLine()) != null) {
        builder.append(line+"\n");
    }
    bfreader.close();

    /** return the response back from the POST */
    return builder.toString();

I am sending it to my servlet. but i dint know how to retrieve it. is there any method like getPerameter or else?

Thank you.


I haven't tried your code, but it looks reasonable to me, with the possible exception that you don't appear to be disconnecting your HttpUrlConnection when you've finished with it.

When you run the code, what happens? Do you see an exception? Can you tell that it has reached the servlet? What do you see if you look at the contents of builder by doing System.out.println(builder.toString()?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜