sunfruit[请访问http://www.fruitres.cn]

--我相信JAVA能走得更远 QQ:316228067

[JAVA]使用HttpURLConnection进行POST方式提交

--sunfruit

用HttpURLConnection进行Post方式提交,下面给出一个例子

    URL url = null;
    HttpURLConnection httpurlconnection = null;
    try
    {
      url = new URL("http://xxxx");

      httpurlconnection = (HttpURLConnection) url.openConnection();
      httpurlconnection.setDoOutput(true);
      httpurlconnection.setRequestMethod("POST");
      String username="username=02000001";
      httpurlconnection.getOutputStream().write(username.getBytes());
      httpurlconnection.getOutputStream().flush();
      httpurlconnection.getOutputStream().close();
      int code = httpurlconnection.getResponseCode();
      System.out.println("code   " + code);

    }
    catch(Exception e)
    {
      e.printStackTrace();
    }
    finally
    {
      if(httpurlconnection!=null)
        httpurlconnection.disconnect();
    }

其中HttpURLConnection中的addRequestProperty方法,并不是用来添加Parameter 的,而是用来设置请求的头信息,比如:
       setRequestProperty("Content-type","text/html");
       setRequestProperty("Connection",   "close");
       setRequestProperty("Content-Length",xxx);

当然如果不设置的话,可以走默认值,上面的例子中就没有进行相关设置,但是也可以正确执行

posted on 2006-03-13 15:47 sunfruit 阅读(38015) 评论(12)  编辑  收藏 所属分类: JAVA SE & EE

评论

# re: [JAVA]使用HttpURLConnection进行POST方式提交 2006-07-31 16:55 南柯一梦

我对你的崇拜不是一句两句能说清!!  回复  更多评论   

# re: [JAVA]使用HttpURLConnection进行POST方式提交 2006-08-01 14:57 sun fruit

呵呵,不敢当,相互学习吧  回复  更多评论   

# re: [JAVA]使用HttpURLConnection进行POST方式提交[未登录] 2007-07-11 17:04 sun

崇拜ing.....

还有个问题要请教
在httpurlconnection.setRequestMethod("POST");下边加上
了httpurlconnection.setRequestProperty("Content-type", "text/html");
为什么就不行了?得不到参数值  回复  更多评论   

# re: [JAVA]使用HttpURLConnection进行POST方式提交 2007-07-11 20:53 fruit

加上httpurlconnection.setRequestProperty("Content-type", "text/html"); ,表明提交的类型是text/html,这个只能通过数据流获得  回复  更多评论   

# re: [JAVA]使用HttpURLConnection进行POST方式提交 2007-07-24 18:26 anosn

非常感谢~~!  回复  更多评论   

# re: [JAVA]使用HttpURLConnection进行POST方式提交不成功 2008-03-26 17:25 肖圣春

public class PostDemo {
private static String FILE_NAME = SubmitMMSMsg.dir + ".zip";
static URL url = null;
static HttpURLConnection httpurlconnection = null;
public static void doPost(){
try
{
url = new URL("http://mpublish.maxtong.com/interface.php");

httpurlconnection = (HttpURLConnection) url.openConnection();
httpurlconnection.setDoOutput(true);
httpurlconnection.setRequestMethod("POST");
// String name ="zip_name=";
// File f = new File("d:\\1.txt");
// FileOutputStream is = new FileOutputStream(f);
// BufferedOutputStream bos = new BufferedOutputStream(is);
// bos.write(getFileString().getBytes());
httpurlconnection.getOutputStream().write(getFileString().getBytes());
httpurlconnection.getOutputStream().flush();
httpurlconnection.getOutputStream().close();
int code = httpurlconnection.getResponseCode();
System.out.println("code " + code);
System.out.println(httpurlconnection.getResponseMessage());  回复  更多评论   

# re: [JAVA]使用HttpURLConnection进行POST方式提交 2008-06-03 16:09 dengqianyong

httpurlconnection.getOutputStream().write(username.getBytes());
httpurlconnection.getOutputStream().write("&".getBytes());
httpurlconnection.getOutputStream().write(password.getBytes());
httpurlconnection.getOutputStream().flush();
httpurlconnection.getOutputStream().close();

比如这段代码是通过post方法登入服务器,然后该怎么做?我想拿到登入后的下个页面该怎么写代码?请指教!谢谢
  回复  更多评论   

# re: [JAVA]使用HttpURLConnection进行POST方式提交 2008-08-26 11:52 张丽云

大侠:
如果我要跨域上传文件应该怎么办呢,望多多指教。qq:379878654 回复邮箱也可以,谢谢了  回复  更多评论   

# re: [JAVA]使用HttpURLConnection进行POST方式提交 2011-11-11 11:37 1111111111

很好的例子,3q  回复  更多评论   

# re: [JAVA]使用HttpURLConnection进行POST方式提交[未登录] 2012-07-21 21:18 aaa

aaaaaaaaaaaaaaaaaaaaaaaa  回复  更多评论   

# re: [JAVA]使用HttpURLConnection进行POST方式提交 2013-04-19 20:52 岚刹

很好!谢谢!  回复  更多评论   

# re: [JAVA]使用HttpURLConnection进行POST方式提交 2013-09-17 11:12 温云

我提交过去的中文是乱码,怎么解决?转码都不行  回复  更多评论   


只有注册用户登录后才能发表评论。


网站导航: