不会发送所有参数来发布http请求

时间:2014-10-29 11:10:48

标签: java http url post request

我正在尝试使用java发送http请求。这是我的代码:

String AnnonseUrl = "http://webpage.no/insert_annonse.php?info="+info+"&tittel="+tittel+"&bedriftsNavn="+bedriftsNavn+"&kontaktEmail="+kontaktEmail+"&varighet="+varighet+"&frist="+frist+"&url="+url+"&sted="+sted+"&kontaktNavn="+kontaktNavn;            

            URL url = new URL(AnnonseUrl);
            URLConnection uc = url.openConnection();
            BufferedReader in = new BufferedReader(
                                    new InputStreamReader(
                                    uc.getInputStream()));

            in.close();

仅提交前三个参数..

如果我将字符串“AnnonseUrl”复制并粘贴到我的浏览器中,那么一切正常。

1 个答案:

答案 0 :(得分:0)

在做帖子时,参数会在Http Body中发送: 试试这个:

URL u = new URL("http://www.stackoverflow.com");
HttpURLConnection conn = (HttpURLConnection) u.openConnection();
conn.setDoOutput(true);
conn.connect();
DataOutputStream wr = new DataOutputStream (
              conn.getOutputStream ());
wr.writeBytes (urlParameters);
wr.flush();
wr.close();

其中urlParameters类似于:

 String urlParameters =
    "tittel="+URLEncoder.encode(tittel,"UTF-8")
    +"&bedriftsNavn"+URLEncoder.encode(bedriftsNavn,"UTF-8");