如何将数据获取到使用 httpcommunicator 和 post 方法发送的 servlet

发布于 2024-11-08 14:23:06 字数 1286 浏览 0 评论 0原文

我正在尝试使用 httpcommunicator 类发送数据。 这是我的代码。

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.getInputStream()));
    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();

我将其发送到我的 servlet。 但我不知道如何检索它。 有没有类似 getPerameter 或其他方法?

谢谢。

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.getInputStream()));
    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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

离不开的别离 2024-11-15 14:23:06

我还没有尝试过你的代码,但它对我来说看起来很合理,但可能的例外是,当你完成它时,你似乎没有断开你的 HttpUrlConnection 。

当您运行代码时,会发生什么?你看到一个例外吗?你能看出它已经到达了 servlet 吗?如果通过执行 System.out.println(builder.toString()) 查看 builder 的内容,您会看到什么?

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()?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文