Error writing to server

Salted-fish 2018-02-22 11:24:58
用HTTP发送接收XML
客户端


public class XmlTest {
String url;

public XmlTest(String url){

this.url = url;
}


public static void main(String[] args) {

String url = "http://127.0.0.1:8080/ecg/user/report/receive";




String returnStr = "";
HttpURLConnection httpConn = null;
InputStream fis = null;

try {

URL httpurl = new URL(url);
String xmlInfo = getXmlInfo();
httpConn = (HttpURLConnection) httpurl.openConnection();

httpConn.setDoOutput(true);
httpConn.setRequestProperty("User-agent", "MSIE8.0"); //设置代理为IE8
httpConn.setRequestMethod("POST");
httpConn.setConnectTimeout(600000);
httpConn.setReadTimeout(600000);

OutputStream op = httpConn.getOutputStream();
op.write(xmlInfo.getBytes());


op.flush();
op.close();

if(httpConn.getResponseCode() == 200){
fis = httpConn.getInputStream();
returnStr = Stream2String(fis,"utf-8");
}
System.out.println(returnStr);

} catch (Exception e) {
System.out.println(e.getMessage());
} finally {
try {
if(fis!=null){
fis.close();
}
} catch (IOException e) {
e.printStackTrace();
}
httpConn.disconnect();
}
}

private static String Stream2String(InputStream in, String encoding) {

if (in == null) {

return null;
}
StringBuffer out = new StringBuffer();
try {

char[] b = new char[1024];
InputStreamReader inread = new InputStreamReader(in, encoding);

for (int n; (n = inread.read(b)) != -1;) {
String line = new String(b, 0, n);
out.append(line);
}

} catch (Exception e) {

e.printStackTrace();
}

return out.toString();
}

private static String getXmlInfo() {
SAXBuilder builder = new SAXBuilder();
Document document;
try {
document = builder.build(new FileInputStream(new File("C:/Users/panjing/Documents/Tencent Files/807053633/FileRecv/1514438446000.xml")));
Format format = Format.getCompactFormat();
format.setEncoding("UTF-8");// 设置xml文件的字符为UTF-8,解决中文问题
XMLOutputter xmlout = new XMLOutputter();
ByteArrayOutputStream bo = new ByteArrayOutputStream();
xmlout.output(document, bo);
return bo.toString().trim();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (JDOMException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}


return null;
}

}



服务端

@Controller
@RequestMapping("/ecg/user/report")
public class EcgUserReportController extends BaseController {
@RequestMapping("receive")
public Document receiveXml(HttpServletRequest request,HttpServletResponse response) {
SAXReader saxReader = null;
Document doc = null;

try{
saxReader = new SAXReader();
doc = saxReader.read(request.getInputStream());

String xmlStr = doc.asXML();
System.out.println("xmlInfo=" + xmlStr);
ecgUserReportService.saveXmlResult(xmlStr,4127);

} catch (Exception e){
e.printStackTrace();
}
return doc ;
}

}
...全文
2322 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
Salted-fish 2018-02-22
  • 打赏
  • 举报
回复
我逗了,忘了加项目名称了
Salted-fish 2018-02-22
  • 打赏
  • 举报
回复
我用Servers启动服务器,然后在src/test/java 创建客户端,Run As-->Java Application客户端,结果报Error writing to server。
Salted-fish 2018-02-22
  • 打赏
  • 举报
回复
用的是DES加解密过滤器
Salted-fish 2018-02-22
  • 打赏
  • 举报
回复
搞了1会儿,原来是web.xml配置了加密过滤器,结果连接不上。 然后我在filter那加了个 <init-param> <description>不加密的url</description> <param-name>url</param-name> <param-value>/ecg/user/report/receive</param-value> </init-param> 就OK了。此贴终结,顺便问下万能的网友,如果不修改配置,发送端要怎么样才能连接啊?
Salted-fish 2018-02-22
  • 打赏
  • 举报
回复
我把代码简化了下,发送成功.但是接受服务器返回的数据失败。 客户端 public class SendXmlTest { @Test public void test() { try { URL url = new URL("http://127.0.0.1:8080/wootop-api/ecg/user/report/receive"); HttpURLConnection con = (HttpURLConnection) url.openConnection(); con.setDoOutput(true); con.setRequestProperty("User-agent", "MSIE8.0"); //设置代理为IE8 con.setRequestMethod("POST"); con.setConnectTimeout(600000); OutputStreamWriter out = new OutputStreamWriter(con .getOutputStream()); String xmlInfo = getXmlInfo(); //System.out.println("xmlInfo=" + xmlInfo); out.write(new String(xmlInfo.getBytes("UTF-8"))); out.flush(); out.close(); //请求成功,返回数据 System.out.println("发送成功!"); BufferedReader br = new BufferedReader(new InputStreamReader(con .getInputStream())); String line = ""; for (line = br.readLine(); line != null; line = br.readLine()) { System.out.println(line); } System.out.println("接受成功!"); } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } } 服务端 @RequestMapping("receive") public String receiveXml(HttpServletRequest request,HttpServletResponse response) { SAXReader saxReader = null; Document doc = null; try{ saxReader = new SAXReader(); doc = saxReader.read(request.getInputStream()); String xmlStr = doc.asXML(); System.out.println("xmlInfo=" + xmlStr); //ecgUserReportService.saveXmlResult(xmlStr,4127); return xmlStr ; } catch (Exception e){ e.printStackTrace(); } return null ; } 报错: 发送成功! java.io.IOException: Error writing to server at sun.net.www.protocol.http.HttpURLConnection.writeRequests(HttpURLConnection.java:699) at sun.net.www.protocol.http.HttpURLConnection.writeRequests(HttpURLConnection.java:711) at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1585) at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1492) at com.wootop.test.SendXmlTest.test(SendXmlTest.java:43) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44) at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15) at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41) at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:76) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50) at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193) at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52) at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191) at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42) at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184) at org.junit.runners.ParentRunner.run(ParentRunner.java:236) at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86) at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
Sunyiban 2018-02-22
  • 打赏
  • 举报
回复

67,513

社区成员

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

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