Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7990379
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T12:59:27+00:00 2026-06-04T12:59:27+00:00

public static byte[] sendRequest(String url ,Object params ,boolean isHttps){ if(isHttps){ HttpsURLConnection urlConnection = (HttpsURLConnection)newURL(url).openConnection();

  • 0
public static byte[] sendRequest(String url ,Object params ,boolean isHttps){
        if(isHttps){
            HttpsURLConnection urlConnection = (HttpsURLConnection)newURL(url).openConnection();
            urlConnection.setDoOutput(true);
            urlConnection.setDoInput(true);
            urlConnection.setRequestMethod("POST");
            urlConnection.setConnectTimeout(5000);
            urlConnection.setReadTimeout(60000);
            urlConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8");
       }

        }else{
            HttpURLConnection urlConnection = (HttpURLConnection)new URL(url).openConnection();
            urlConnection.setDoOutput(true);
            urlConnection.setDoInput(true);
            urlConnection.setRequestMethod("POST");
            urlConnection.setConnectTimeout(5000);
            urlConnection.setReadTimeout(60000);
            urlConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8");

        }


}

In this both if and else contains same lines of code beside type casting . If I move this lines of code to a common method again I want to type cast and do this process again . How can i reduce this redudant lines can anyone help me .

I have a solution in python .

     //pseudo code .
     if(isHttps) :
           urlConnection = https . 
     else : 
         urlConnection = http .
     urlConnection.setDoOutput(true);
     urlConnection.setDoInput(true);
     urlConnection.setRequestMethod("POST");
     urlConnection.setConnectTimeout(5000);
     urlConnection.setReadTimeout(60000);

Because in python variable inside a if also accessible from outside block . In java how can i do this .

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T12:59:28+00:00Added an answer on June 4, 2026 at 12:59 pm

    I assume that the isHttps variable check is there for a reason(?) and therefore the second cast should actually be HttpURLConnection, meaning there is a typo in the question?
    If so the most methods used in the question are available in the parent class URLConnection without a cast, but not all.

    Fortunately HttpsURLConnection is a subclas of HttpUrlConnection so just always casting to that will work here, replace all with (no need for the isHttps check):

    HttpURLConnection urlConnection = (HttpURLConnection)new URL(url).openConnection();
    urlConnection.setDoOutput(true);
    urlConnection.setDoInput(true);
    urlConnection.setRequestMethod("POST");
    urlConnection.setConnectTimeout(5000);
    urlConnection.setReadTimeout(60000);
    urlConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8");
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following function: public static byte[] StringToByte(string str) { int length =
public class b { public static void main(String[] args) { byte b = 1;
public static void main(String[] args) { List<? extends Object> mylist = new ArrayList<Object>(); mylist.add(Java);
public class URLReader { public static byte[] read(String from, String to, String string){ try
I came across this piece of code today: public static byte[] ReadContentFromFile(String filePath) {
I need to zip and unzip string Here is code: public static byte[] ZipStr(String
I have the below methods: public static byte[] ConvertFileToBytes(string filePath) { var fInfo =
public static byte[] Compress(byte[] data) { using (MemoryStream ms = new MemoryStream()) { using
I am trying to encrypt some data with the following code: public static byte[]
public static IQueryable<TResult> ApplySortFilter<T, TResult>(this IQueryable<T> query, string columnName) where T : EntityObject {

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.