PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Friday, November 11, 2022

[FIXED] How to get response parameter in HttpClient

 November 11, 2022     httpclient, java     No comments   

Issue

I am trying to pass some data in one application and get the response from that. For that I am using HttpClient and want to use the value of response in my application. It may be string, int, or boolean

I wrote the following code

HttpClient client = new DefaultHttpClient();

HttpPost request = new HttpPost(
    "http://index.html?email=" + email + 
    "&password=" + password
);

try {
    HttpResponse response = client.execute(request);
    HttpEntity entity = response.getEntity();
    InputStream is = entity.getContent();
    BufferedReader bf = new BufferedReader(
        new InputStreamReader(is,"UTF-8")
    );

    String str = bf.readLine();
        

} catch (Exception e) {
    System.out.println("Error is :- " + e);
}

This link will return parameter login=true/false. Now how can I get the value of the login parameter using response?


Solution

All attributes stored in the request are strings. You will have to manually cast it to whatever type they are.

For example you are trying to get a boolean attribute from the request. You will have to do this:

String attr = request.getAttribute("nameOfAttribute");
Boolean bool = Boolean.getBoolean(attr);


Answered By - Akolopez
Answer Checked By - Dawn Plyler (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing