http post 发送请求,没有返回结果

zhangfengyi 2016-03-16 06:14:17
通过http post向服务端发出请求,没有返回结果,哪里出了问题。
服务端servlet:

package myBean;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServlet;

public class AcceptForm extends HttpServlet {
private static final long serialVersionUID = 1L;
public void init() throws ServletException {
// Put your code here
}

public void service(ServletRequest req,ServletResponse response)
throws ServletException,IOException
{
String username=req.getParameter("message");
String strs=new HttpUtil().httpPost("张**","150429197*1X","6225880122*2","1352*5");
System.out.println(strs);
System.out.flush();
System.out.close();
}
}

客户端avajava:

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;

public class test1 {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
String url = "http://112.125.122.8:8080/webtest2/AcceptForm";
String message="1MGU1MjUyNWZhZjJiYjcy";
StringBuffer sb = new StringBuffer();
try {
URL urls = new URL(url);
HttpURLConnection uc = (HttpURLConnection) urls.openConnection();
uc.setRequestMethod("POST");
uc.setRequestProperty("content-type",
"application/x-www-form-urlencoded");
uc.setRequestProperty("charset", "UTF-8");
uc.setDoOutput(true);
uc.setDoInput(true);
uc.setReadTimeout(10000);
uc.setConnectTimeout(10000);
OutputStream os = (OutputStream) uc.getOutputStream();
DataOutputStream dos = new DataOutputStream(os);
dos.write(message.getBytes("utf-8"));
dos.flush();
os.close();
BufferedReader in = new BufferedReader(new InputStreamReader(uc.getInputStream(), "utf-8"));
String readLine = "";
while ((readLine = in.readLine()) != null) {
sb.append(readLine);
}
in.close();
System.out.println("结果: "+sb.toString());
} catch (Exception e) {
e.printStackTrace();

}
}
}
...全文
2082 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
MamyA 2016-03-18
  • 打赏
  • 举报
回复
doPost和doGet只是区分http操作的不同类型罢了,最后都会到service方法的,所以你写在service中是可以的。这个是一定的,在javax中有定义的。 另外HttpServlet要想输出结果出去,是需要格式化输出流的,如OutputStream,PrintWriter等,不能用系统输出通道,这个是不能输出到请求端的。 如果确认输出通道没问题,而且已进入service方法,那就检查 String strs=new HttpUtil().httpPost("张**","150429197*1X","6225880122*2","1352*5"); 这段代码是否有返回值strs。。
代码间的舞者 2016-03-17
  • 打赏
  • 举报
回复
回答十楼的问题, 你应该去确认服务端这段代码有没有返回值!

String strs=new HttpUtil().httpPost("张**","150429197*1X","6225880122*2","1352*5");
zhangfengyi 2016-03-16
  • 打赏
  • 举报
回复
引用 8 楼 kx00450 的回复:
1、你客户端请求使用post, 服务器端 代码需要放在dopost 中, 2、客户端没有得到数据是你服务端没有返回数据,需要用response 输出 如 response.getWriter().print("123123"); 才能在客户端 OutputStream os = (OutputStream) uc.getOutputStream(); 中获取数据。
OK
kx00450 2016-03-16
  • 打赏
  • 举报
回复
1、你客户端请求使用post, 服务器端 代码需要放在dopost 中, 2、客户端没有得到数据是你服务端没有返回数据,需要用response 输出 如 response.getWriter().print("123123"); 才能在客户端 OutputStream os = (OutputStream) uc.getOutputStream(); 中获取数据。
Intboy 2016-03-16
  • 打赏
  • 举报
回复
引用 6 楼 Q80470101 的回复:
[quote=引用 5 楼 Q80470101 的回复:] [quote=引用 4 楼 zhangfengyi 的回复:] [quote=引用 3 楼 Q80470101 的回复:] 你的doPost()如何实现的? 参考: javaweb学习总结(五)——Servlet开发(一) - 孤傲苍狼 - 博客园 http://www.cnblogs.com/xdp-gacl/p/3760336.html

        String username=req.getParameter("message");
        String strs=new HttpUtil().httpPost("张**","150429197*1X","6225880122*2","1352*5");
        System.out.println(strs);
        System.out.flush();
        System.out.close();
[/quote][/quote] 你先看看我给你的参考资料[/quote]估计你给的资料太长了。。。
家里敷泥呀 2016-03-16
  • 打赏
  • 举报
回复
引用 4 楼 zhangfengyi 的回复:
[quote=引用 3 楼 Q80470101 的回复:] 你的doPost()如何实现的? 参考: javaweb学习总结(五)——Servlet开发(一) - 孤傲苍狼 - 博客园 http://www.cnblogs.com/xdp-gacl/p/3760336.html

        String username=req.getParameter("message");
        String strs=new HttpUtil().httpPost("张**","150429197*1X","6225880122*2","1352*5");
        System.out.println(strs);
        System.out.flush();
        System.out.close();
[/quote]
家里敷泥呀 2016-03-16
  • 打赏
  • 举报
回复
引用 5 楼 Q80470101 的回复:
[quote=引用 4 楼 zhangfengyi 的回复:] [quote=引用 3 楼 Q80470101 的回复:] 你的doPost()如何实现的? 参考: javaweb学习总结(五)——Servlet开发(一) - 孤傲苍狼 - 博客园 http://www.cnblogs.com/xdp-gacl/p/3760336.html

        String username=req.getParameter("message");
        String strs=new HttpUtil().httpPost("张**","150429197*1X","6225880122*2","1352*5");
        System.out.println(strs);
        System.out.flush();
        System.out.close();
[/quote][/quote] 你先看看我给你的参考资料
zhangfengyi 2016-03-16
  • 打赏
  • 举报
回复
引用 3 楼 Q80470101 的回复:
你的doPost()如何实现的? 参考: javaweb学习总结(五)——Servlet开发(一) - 孤傲苍狼 - 博客园 http://www.cnblogs.com/xdp-gacl/p/3760336.html

        String username=req.getParameter("message");
        String strs=new HttpUtil().httpPost("张**","150429197*1X","6225880122*2","1352*5");
        System.out.println(strs);
        System.out.flush();
        System.out.close();
家里敷泥呀 2016-03-16
  • 打赏
  • 举报
回复
你的doPost()如何实现的? 参考: javaweb学习总结(五)——Servlet开发(一) - 孤傲苍狼 - 博客园 http://www.cnblogs.com/xdp-gacl/p/3760336.html
zhangfengyi 2016-03-16
  • 打赏
  • 举报
回复
引用 1 楼 tianfang 的回复:
servlet 处理POST需要写在post方法中,实现HttpServlet方法
放在DoPost方法中也是相同的结果,着急啊。。。。。。
tianfang 2016-03-16
  • 打赏
  • 举报
回复
servlet 处理POST需要写在post方法中,实现HttpServlet方法
zhangfengyi 2016-03-16
  • 打赏
  • 举报
回复
客户端sb.toString值为null, 是不是服务端String username=request.getParameter("message");没有接收到上送信息

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
 
public class test1 {
 
    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        String url = "http://112.125.122.8:8080/webtest2/AcceptForm";
        String message="1MGU1MjUyNWZhZjJiYjcy";
        StringBuffer sb = new StringBuffer();
        try {
        URL urls = new URL(url);
        HttpURLConnection uc = (HttpURLConnection) urls.openConnection();
        uc.setRequestMethod("POST");
        uc.setRequestProperty("content-type",
        "application/x-www-form-urlencoded");
        uc.setRequestProperty("charset", "UTF-8");
        uc.setDoOutput(true);
        uc.setDoInput(true);
        uc.setReadTimeout(10000);
        uc.setConnectTimeout(10000);
        OutputStream os = (OutputStream) uc.getOutputStream();
        DataOutputStream dos = new DataOutputStream(os);
        dos.write(message.getBytes("utf-8"));
        dos.flush();
        os.close();
        BufferedReader in = new BufferedReader(new InputStreamReader(uc.getInputStream(), "utf-8"));
        String readLine = "";
        while ((readLine = in.readLine()) != null) {
        sb.append(readLine);
        }
        in.close();
        System.out.println("结果: "+sb.toString());
        } catch (Exception e) {
        e.printStackTrace();
         
      }
  }
}

67,512

社区成员

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

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