2016-11-07 38 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

對,你的方式是我需要的,謝謝:) –