HttpURLConnection的POST问题 急!求解决

bbg2345 2007-05-08 01:32:38
我的一个类:
url = new URL("http://localhost:8088/servletTest/servlet");
httpurlconnection = (HttpURLConnection) url.openConnection();
httpurlconnection.setRequestMethod( "POST" );
// httpurlconnection.setRequestProperty("transType", "103");
httpurlconnection.setUseCaches(false);
httpurlconnection.setDoOutput(true);
OutputStreamWriter out = new OutputStreamWriter( httpurlconnection.getOutputStream() );
String request = "transType=103&transMessage=werijfio");
out.write( request );
out.flush();
out.close();
BufferedReader br = new BufferedReader(new InputStreamReader(httpurlconnection
.getInputStream()));

httpurlconnection.connect();

String line = br.readLine();

while (line != null) {

System.out.println(line);

line = br.readLine();
}
我提交到的servlet中有两个变量transType和transMessage 用request获得。
但老是报错:
java.io.IOException: Server returned HTTP response code: 405 for URL: http://localhost:8088/servletTest/servlet
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:800)
at com.test.HttpURLConnectionTest.main(HttpURLConnectionTest.java:66)
高手们 急 解决下。
...全文
606 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
bbg2345 2007-05-08
  • 打赏
  • 举报
回复
java.io.IOException: Server returned HTTP response code: 405 for URL: http://localhost:8088/servletTest/servlet
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:800)
at com.test.HttpURLConnectionTest.main(HttpURLConnectionTest.java:53)
==========================================================
还是这个错误,大家可以判断是这个类错误还是servlet错误呢?
bbg2345 2007-05-08
  • 打赏
  • 举报
回复
好的 谢谢 我再试下。
masse 2007-05-08
  • 打赏
  • 举报
回复
调用方法是对的,再检查一下吧

http://www.exampledepot.com/egs/java.net/Post.html

try {
// Construct data
String data = URLEncoder.encode("key1", "UTF-8") + "=" + URLEncoder.encode("value1", "UTF-8");
data += "&" + URLEncoder.encode("key2", "UTF-8") + "=" + URLEncoder.encode("value2", "UTF-8");

// Send data
URL url = new URL("http://hostname:80/cgi");
URLConnection conn = url.openConnection();
conn.setDoOutput(true);
OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
wr.write(data);
wr.flush();

// Get the response
BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String line;
while ((line = rd.readLine()) != null) {
// Process line...
}
wr.close();
rd.close();
} catch (Exception e) {
}

67,513

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧