What is the proper way of setting headers in a URLConnection?

前端 未结 3 827
粉色の甜心
粉色の甜心 2020-12-10 01:32

My code is like the following:

URLConnection cnx = address.openConnection();
cnx.setAllowUserInteraction(false);         
cnx.setDoOutput(true);
cnx.addReque         


        
3条回答
  •  不思量自难忘°
    2020-12-10 02:20

    The headers must be set prior to getting the InputStream to have any affect - an IllegalStateException will be thrown if the connection is already open.

    As far as the User-Agent header specifically, it should be sent if it has been set.

    See the URLConnection JavaDoc.

提交回复
热议问题