今天干活时有个需求,要和.net进行一次交互,想在Struts的Action中接收.net通过HTTPt发过来的XML格式的字符流!然后就解析这个XML字符串通过一些处理然后存数据库。
在做完解析过程和存数据库后,想自己测试一下,所以就想到了通过JDK的HttpURLConnection向自己的Action发送一个用于测试的XML格式的字符流,然后就出现了以下的代码
Java代码
1.public class TestClientRegist extends TestCase {
2.
3.public void testRegist(){
4. try{
5.
6. // 用于测试,读的自己本地的XML文件
7. FileInputStream input = new FileInputStream(new File("regist.xml"));
8.
9. // Action地址
10. URL url = new URL("http://localhost:8080/fstm/rmi/client/regist.do");
11. HttpURLConnection conn = (HttpURLConnection)url.openConnection();
12. conn.setDoOutput(true);
13. conn.setRequestMethod("POST");
14.
15. byte[] b = new byte[1024];
16. int l = 0;
17. OutputStream out = conn.getOutputStream();
18. out.write("data=".getBytes());
19. while((l = input.read(b, 0, b.length)) != -1){
20. out.write(b, 0, l);
21. }
22. out.close();
23. InputStream in = conn.getInputStream();
24. in.close();
25. }catch(Exception ex){
26. ex.printStackTrace();
27. }
28. }
29.}
public class TestClientRegist extends TestCase {
public void testRegist(){
try{
// 用于测试,读的自己本地的XML文件
FileInputStream input = new FileInputStream(new File("regist.xml"));
// Action地址
URL url = new URL("http://localhost:8080/fstm/rmi/client/regist.do");
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
conn.setDoOutput(true);
conn.setRequestMethod("POST");
byte[] b = new byte[1024];
int l = 0;
OutputStream out = conn.getOutputStream();
out.write("data=".getBytes());
while((l = input.read(b, 0, b.length)) != -1){
out.write(b, 0, l);
}
out.close();
InputStream in = conn.getInputStream();
in.close();
}catch(Exception ex){
ex.printStackTrace();
}
}
}
这些代码是没问题的啦。但没用过这东西,刚开始出了好多问题。
1,第一次在下面没有获取“输入流”,发现没执行时没有反应,Action那边也没有调用
加上以后就好了。问了个“高人”才明白,这里需要建立一次连接,当执行完输出流后。还要接收HTTP的响应。还遇到一些别的问题就不在罗嗦了~~~~大家有什么问题可以来多讨论讨论。
有什么高见请赐教。一定认真接受。
下面在给出Action接收的代码
Java代码
1.public ActionForward execute(ActionMapping mapping, ActionForm form,
2. HttpServletRequest request, HttpServletResponse response) {
3.
4. try {
5. InputStream in = request.getInputStream();
6.
7. BufferedReader br = new BufferedReader(new InputStreamReader(in));
8. StringBuffer data = new StringBuffer();
9. String line = null;
10. while((line = br.readLine()) != null){
11. data.append(line);
12. }
13. logger.debug(data);
14. clientMgr.doRegistManager(data.toString());
15.// 设置返回状态
16.response.setStatus(HttpServletResponse.SC_OK);
17. } catch (Exception ex) {
18. response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
19. logger.warn("", ex);
20. }
21. return null;
22. }
在做完解析过程和存数据库后,想自己测试一下,所以就想到了通过JDK的HttpURLConnection向自己的Action发送一个用于测试的XML格式的字符流,然后就出现了以下的代码
Java代码
1.public class TestClientRegist extends TestCase {
2.
3.public void testRegist(){
4. try{
5.
6. // 用于测试,读的自己本地的XML文件
7. FileInputStream input = new FileInputStream(new File("regist.xml"));
8.
9. // Action地址
10. URL url = new URL("http://localhost:8080/fstm/rmi/client/regist.do");
11. HttpURLConnection conn = (HttpURLConnection)url.openConnection();
12. conn.setDoOutput(true);
13. conn.setRequestMethod("POST");
14.
15. byte[] b = new byte[1024];
16. int l = 0;
17. OutputStream out = conn.getOutputStream();
18. out.write("data=".getBytes());
19. while((l = input.read(b, 0, b.length)) != -1){
20. out.write(b, 0, l);
21. }
22. out.close();
23. InputStream in = conn.getInputStream();
24. in.close();
25. }catch(Exception ex){
26. ex.printStackTrace();
27. }
28. }
29.}
public class TestClientRegist extends TestCase {
public void testRegist(){
try{
// 用于测试,读的自己本地的XML文件
FileInputStream input = new FileInputStream(new File("regist.xml"));
// Action地址
URL url = new URL("http://localhost:8080/fstm/rmi/client/regist.do");
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
conn.setDoOutput(true);
conn.setRequestMethod("POST");
byte[] b = new byte[1024];
int l = 0;
OutputStream out = conn.getOutputStream();
out.write("data=".getBytes());
while((l = input.read(b, 0, b.length)) != -1){
out.write(b, 0, l);
}
out.close();
InputStream in = conn.getInputStream();
in.close();
}catch(Exception ex){
ex.printStackTrace();
}
}
}
这些代码是没问题的啦。但没用过这东西,刚开始出了好多问题。
1,第一次在下面没有获取“输入流”,发现没执行时没有反应,Action那边也没有调用
加上以后就好了。问了个“高人”才明白,这里需要建立一次连接,当执行完输出流后。还要接收HTTP的响应。还遇到一些别的问题就不在罗嗦了~~~~大家有什么问题可以来多讨论讨论。
有什么高见请赐教。一定认真接受。
下面在给出Action接收的代码
Java代码
1.public ActionForward execute(ActionMapping mapping, ActionForm form,
2. HttpServletRequest request, HttpServletResponse response) {
3.
4. try {
5. InputStream in = request.getInputStream();
6.
7. BufferedReader br = new BufferedReader(new InputStreamReader(in));
8. StringBuffer data = new StringBuffer();
9. String line = null;
10. while((line = br.readLine()) != null){
11. data.append(line);
12. }
13. logger.debug(data);
14. clientMgr.doRegistManager(data.toString());
15.// 设置返回状态
16.response.setStatus(HttpServletResponse.SC_OK);
17. } catch (Exception ex) {
18. response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
19. logger.warn("", ex);
20. }
21. return null;
22. }