2016-11-07 36 views
0

请求是从url获取内容并正确处理内容(每次不同),然后将答案发回到同一个url。当我尝试执行GET方法后的setRequestMethod(“POST”)时遇到“无法重置方法:已连接”。我的代码如下Java - 如何为GET和POST打开一个HttpURLConnection

public class MyClass { 

    /** 
    * @param args 
    */ 

    public MyClass() {}; 

    public void process() { 
     String url = "http://www.somesite.com/"; 
     String strPage = null; 
     int n = 0; 

     try{ 
      URL urlObj = new URL(url); 
      HttpURLConnection urlConnection = 
        (HttpURLConnection)urlObj.openConnection(); 
      InputStream in = urlConnection.getInputStream(); 

      BufferedReader reader = new BufferedReader(new InputStreamReader(in)); 

      String strWhole = null; 
      while(null != (strPage = reader.readLine())){ 
       strWhole += strPage; 
      } 

      //handle content here and calculate result 
      ... ... 
      //send result below 

      urlConnection.setRequestMethod("POST"); 
      urlConnection.setDoOutput(true); 
      String urlParameters = "aa=bb&cc=dd&ee=ff"; 

      DataOutputStream wr = new DataOutputStream(urlConnection.getOutputStream()); 
      wr.writeBytes(urlParameters); 
      wr.flush(); 
      wr.close();    

      InputStream in1 = urlConnection.getInputStream(); 

      BufferedReader reader1 = new BufferedReader(new InputStreamReader(in)); 


      while(null != (strPage = reader1.readLine())){ 
       System.out.println(strPage); 
      } 

      reader1.close();    
     } 
     catch(Exception e){ 
      String exception = e.getMessage(); 
      System.out.println(exception); 
      if (reader != null) { 
       reader.close(); 
      } 

      if (reader1 != null) { 
       reader1.close(); 
      } 
     } 

     return; 

    } 

    public static void main(String[] args) { 
     // TODO Auto-generated method stub 
     MyClass dp = new MyClass(); 
     dp.process(); 

    } 

} 

回答

0

您必须首先设置所有参数。以下是我在我的应用程序中使用代码:

 HttpURLConnection connection = (HttpURLConnection)new URL(url).openConnection(); 
    connection.setDoOutput(true); 
    connection.setDoInput(true); 
    connection.setInstanceFollowRedirects(false); 
    connection.setRequestMethod("POST"); 
    connection.setRequestProperty("app_token", "my token"); // optional header you can set with your own data 
    connection.setRequestProperty("charset", "utf-8"); 
    connection.setRequestProperty("Content-Length", String.valueOf(urlParameters.getBytes().length)); 
    connection.setUseCaches (false); 
    DataOutputStream wr = new DataOutputStream(connection.getOutputStream()); 
    wr.writeBytes(urlParameters); 
    wr.flush(); 
    wr.close(); 
    connection.disconnect(); 
    InputStream is = connection.getInputStream(); 
    byte[] b = readWithoutSize(is); 
    is.close(); 

的readWithoutSize是:

public static byte[] readWithoutSize(InputStream is) throws IOException 
    { 
     ByteArrayOutputStream baos = new ByteArrayOutputStream(4096); 
     byte[] buf = new byte[512]; 
     int leu; 
     while ((leu = is.read(buf)) != -1) 
     baos.write(buf,0,leu); 
     return baos.toByteArray(); 
    } 
+0

@Guiherme当我应该调用openConnection()? –

+0

对不起,我修复了代码。 –

+0

@Guiherme谢谢,它不适合我。 kgeorgiy的解决方案是我所需要的:) –

1

这是不可能重用HttpURLConnection实例。但是documentation表示在引擎盖下,Java为您重用连接:

JDK支持HTTP/1.1和HTTP/1.0持久连接。

当应用程序完成读取响应主体或当应用程序由URLConnection.getInputStream()返回InputStream调用close(),JDK的HTTP协议处理程序会尝试清理连接,如果成功的话,把连接到重用一个连接高速缓存通过未来的HTTP请求。

对HTTP keep-Alive的支持是透明地完成的。

因此,不需要手动重用连接。

+0

它调用close()后不起作用,并再次POST,碰到同样的问题。 –

+0

它不应该在'close()'后面工作,只要打开一个新的连接'openConnection'。 – kgeorgiy

+0

对,你的方式是我需要的,谢谢:) –