如何将数据获取到使用 httpcommunicator 和 post 方法发送的 servlet
我正在尝试使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我还没有尝试过你的代码,但它对我来说看起来很合理,但可能的例外是,当你完成它时,你似乎没有断开你的 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()
?