HttpURLConnection urlConn; DataOutputStream out; DataInputStream in; BufferedReader d; System.out.println("Preparing the URL Connection"); urlConn = (HttpURLConnection)url.openConnection(); urlConn.setRequestMethod("POST"); urlConn.setDoOutput(true); urlConn.setDoInput(true); urlConn.setUseCaches(false); urlConn.setAllowUserInteraction(false); System.out.println("Getting the output stream"); out = new DataOutputStream( urlConn.getOutputStream ()); System.out.println("Writing the bytes"); out.writeBytes("toAsp=" + URLEncoder.encode("Ping Me")); out.flush(); out.close(); System.out.println("Output stream sent"); *** This is where the program fails ****** in = new DataInputStream( urlConn.getInputStream() ); System.out.println("Getting the input stream back"); d = new BufferedReader(new InputStreamReader(in)); System.out.println("Made buffered reader"); System.out.println(d.readLine()); d.close(); in.close(); You can try this, maybe right, maybe wrong.
利用Servlet来解决:public class ReceiptServlet extends HttpServlet { public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { ... // set content type header before accessing the Writer response.setContentType("text/html"); PrintWriter out = response.getWriter();
// then write the response out.println("<html>" + "<head><title> Receipt </title>" + ...);
out.println("<h3>Thank you for purchasing your books from us " + request.getParameter("cardname") + ...); out.close(); } ... }不知道可否解决你的问题?
The code I've written is what I had found for you, not from me. It still has a bug, so, want everyone to see it, but maybe still can help you. good luck
DataOutputStream out;
DataInputStream in;
BufferedReader d;
System.out.println("Preparing the URL Connection");
urlConn = (HttpURLConnection)url.openConnection();
urlConn.setRequestMethod("POST");
urlConn.setDoOutput(true);
urlConn.setDoInput(true);
urlConn.setUseCaches(false);
urlConn.setAllowUserInteraction(false);
System.out.println("Getting the output stream");
out = new DataOutputStream( urlConn.getOutputStream ());
System.out.println("Writing the bytes");
out.writeBytes("toAsp=" + URLEncoder.encode("Ping Me"));
out.flush();
out.close();
System.out.println("Output stream sent");
*** This is where the program fails ******
in = new DataInputStream( urlConn.getInputStream() );
System.out.println("Getting the input stream back");
d = new BufferedReader(new InputStreamReader(in));
System.out.println("Made buffered reader");
System.out.println(d.readLine());
d.close();
in.close(); You can try this, maybe right, maybe wrong.
而且有时socket还受限制,吃力不讨好
是吗?
如果hangcom兄台不用Socket,只用url能解决这个问题当然最好。定高分奉送。
Mars_lee应该可以成功,只是我在urlConn.getOutputStream ()时不知怎么处理,还往指教。
首先这个socket的端口肯定是80或者是其他Web site的监听端口。
然后按照Http协议的规范写你发送的数据包即可。不过太复杂。
你把你所要传输的数据向Stream中写即可。
可是奇怪!程序怎么写成那样了?
HttpServletResponse response)
throws ServletException, IOException
{
...
// set content type header before accessing the Writer
response.setContentType("text/html");
PrintWriter out = response.getWriter();
// then write the response
out.println("<html>" +
"<head><title> Receipt </title>" +
...);
out.println("<h3>Thank you for purchasing your books from us " +
request.getParameter("cardname") +
...);
out.close();
}
...
}不知道可否解决你的问题?
It still has a bug, so, want everyone to see it, but maybe still can help you.
good luck
现在我正准备给asp页面提交一幅图像,希望成功!祝我好运。
Thanks
DataInputStream dis;
String temp_message="tempmessage";
String temp_url_s;
temp_url_s="xxx.asp?par1=xxx&par2=xxx&parN=xxx";//写上你的asp
try{
//temp_url_s 为调用action_to_nt 前设定的url暂时串
nt_action=new URL(getDocumentBase(),temp_url_s);
dis=new DataInputStream(nt_action.openStream());
while((temp_message=dis.readLine())!=null){
//temp_message 为取回的信息
}dis.close();
}catch(MalformedURLException e){
//temp_message="MALerr";
}catch(IOException e){
//temp_message="IOerr";
}
可是这种方法似乎不应该能把file之类的东东传上去
发送命令
POST (asp地址) HTTP/1.0
Content-Length: parm=value&...(参数和值)
现在正在尝试读rfc文档,因为文件的提交不太一样。很辛苦!有没有好方法?
如果你想上传文件也很简单啊,我做了个DLL文件,用来直接从FILE控件中读取文件,我不明白为什么要用SOCKET?
真要用的话,就用VB写个DLL文件了。
如果你想上传文件也很简单啊,我做了个DLL文件,用来直接从FILE控件中读取文件,我不明白为什么要用SOCKET?
真要用的话,就用VB写个DLL文件了。
我的Post确实是FORM里面的ACTION=POST,可惜客户端不是浏览器,而是java的一个Code Behide 工程。服务器端asp 接收数据。所以需要java发送一个类似与html的 Http请求。现在解决不了文件。