how to pass query parameters in java with Http client

爷,独闯天下 提交于 2019-12-05 06:58:52

Here is your code.. Use it. It is giving response as 401-Unauthorised to me and the same response at browser URL, may this cause of VPN some some other issue. IF you use

private static String ENDPOINT ="http://google.com"; 

It will give you 200-OK.

import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;

       public class Test {
          private static String ENDPOINT ="http://wisekar.iitd.ernet.in/active/api_resources.php/method/mynode";
          public static void main(String[] args) throws IOException 
          { 
              String url = ENDPOINT;
              String charset = "UTF-8";
              String param1 = "mykey";

              String query = String.format("key=%s", 
                   URLEncoder.encode(param1, charset));
              java.net.URLConnection connection = new URL(url + "?" + query).openConnection();
              connection.setRequestProperty("Accept-Charset", charset);
              if ( connection instanceof HttpURLConnection)
              {
                 HttpURLConnection httpConnection = (HttpURLConnection) connection;
                 System.out.println(httpConnection.getResponseCode());
                 System.out.println(httpConnection.getResponseMessage());
              }
              else
              {
                 System.err.println ("error!");
              }
          }
   }
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!