2012-07-31 131 views
0

我想將我的機器名稱添加到http請求標頭中,作爲JAVA中的自定義 變量。如何在發佈請求時在HTTP標頭中設置自定義變量

這裏是我的代碼:

private void sendIssuesToWebService(Issue issue) { 
     // TODO Auto-generated method stub 

     System.out.println("\n\n"); 
     //LOG.info("Sending issue : "+issue.getKey()); 


     HttpURLConnection ycSend = null; 
     BufferedReader in1 = null; 
     JSONObject j = null ; 
     try{ 

      URL urlSend = null; 

      try{ 
       urlSend = new URL(targetURL+"Issue/ImportIssue"); 
      } 
      catch (MalformedURLException e) { 
       // TODO Auto-generated catch block 

       LOG.severe(" URL Malformed Exception while authentication "); 
       e.printStackTrace(); 
      } 



      try{ 
       ycSend = (HttpURLConnection) urlSend.openConnection(); 
      } 
      catch (IOException e) { 
       // TODO Auto-generated catch block 

       LOG.severe(" Error in Http URL Connection while authentication"); 
       e.printStackTrace(); 
      } 


      ycSend.setDoOutput(true); 

      ycSend.setRequestMethod("POST"); 
      ycSend.setRequestProperty("Content-Type", "application/json"); 


      try{ 

       OutputStreamWriter outSend = null; 


       outSend = new OutputStreamWriter(ycSend.getOutputStream()); 
       outSend.write(IssueAndHistoryJsonStr); 


       outSend.flush(); 

      }catch(Exception e) 
      { 
       LOG.severe("Cannot write output stream while sending issue "); 

      } 

      System.out.println(ycSend.getResponseCode()+"\n\n\n"); 

      if(ycSend.getResponseCode()!=200){ 

       in1 = new BufferedReader(new InputStreamReader(
         ycSend.getErrorStream())); 


       String inputLine; 
       while ((inputLine = in1.readLine()) != null) 
        inputResponse=inputResponse+inputLine; 
       //System.out.println("$$$$$$$"+inputResponse); 

       try{ 
        j = new JSONObject(inputResponse); 
        LOG.info(j.get("errors").toString()); 
       } 
       catch(Exception e){ 



        String errorTitle = inputResponse.substring(inputResponse.indexOf("<title>")+7,inputResponse.indexOf("</title>")); 

        LOG.severe(errorTitle); 



       } 

       LOG.severe(" Error in Sending an Issue to the web service and the issue key is "+issue.getKey()); 
       //LOG.severe("Error is : "+j.get("errors")); 
      } 
      else 
      { 

       LOG.info("Issue "+issue.getKey()+" Sent successfully"); 
       countIssues++; 
       System.out.println("\n\n"); 
      } 
      //LOG.info("Issue ***** " +issue.getKey()+ " sent with response : "+inputResponse); 




     } 

     catch(Exception e) 
     { 
      LOG.severe(" Error in Sending an Issue to the web service and the issue key is "+issue.getKey()); 
      //LOG.severe(yc.getResponseMessage()); 
      //LOG.severe("Error is : "+j.get("errors")); 
      e.printStackTrace(); 
     } 

    } 

回答

2

我相信你只是想添加類似內容類型另一個requestProperty。

ycSend.setRequestProperty("Machine-Name", MachineName); 
-1

也許this將有所幫助。或者,你可以看看Apache HTTPClient這樣做。

用於獲取主機名,你可以這樣做:

try { 
    InetAddress addr = InetAddress.getLocalHost(); 

    // Get hostname 
    String hostname = addr.getHostName(); 
} catch (UnknownHostException e) { 
    //DO SOMETHING 
} 
相關問題