I
I
Ilfar2017-07-06 20:27:46
Java
Ilfar, 2017-07-06 20:27:46

How to make a get request in Java?

Help me translate from C# to Java or what should I learn to write such code in Java?

private string GET(string sUrl, string Data)
        {
            string Out = "";
            HttpWebRequest req = (HttpWebRequest)WebRequest.Create(sUrl + "?" + Data);
                //req.Timeout = -1;
                //MessageBox.Show("1");
                 req.KeepAlive = false;
            //req.KeepAlive = true;
            //req.ServicePoint.Expect100Continue = false;
            // req.Credentials = CredentialCache.DefaultCredentials;//
            StreamReader sr = null;
            try
            {
             HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
                Stream stream = resp.GetResponseStream();              
                 sr = new StreamReader(stream, Encoding.Default);
                //textBox1.Text += String.Format("{0}\n", sr);
                Out = sr.ReadToEnd();
                MessageBox.Show(Out);
                sr.Close();                
            }
                catch(Exception ex) {
                    MessageBox.Show("error" + " " + ex.Message);
                }
            finally
            {
                if (sr != null)
                    sr.Close();
            }
            return Out;
        }

Answer the question

In order to leave comments, you need to log in

2 answer(s)
E
Eugene, 2017-07-07
@zolt85

Apache HttpClient

P
Pavel, 2017-07-29
@Jeckit

okHttp help.
The simplest thing you can use to solve your problem.
okHttp
Great tutorial on okHttp

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question