Click here to Skip to main content
15,867,568 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I want to integrate the function of this page http://dniperu.online/buscador/buscardni_11ab.php in a C# application.

When opening the page, the user has to enter 3 parameters:
- First last name: Vizcarra
- Second last name: Cornejo
- First name(s):Martín Alberto

The page is returning the ID of the person.
To check this ID, we can use this link:
https://api.reniec.cloud/dni/04412417

My code is not working, do you have any suggestion ? I think maybe it's an issue with the cookies.

What I have tried:

C#
var request = (HttpWebRequest)WebRequest.Create("http://dniperu.online/buscador/buscardni_11ab.php?ref=busqueda");
var postData = "APE_PAT=Vizcarra&APE_MAT=Cornejo&NOMBRES=Mart%C3%ADn+Alberto";
var data = Encoding.ASCII.GetBytes(postData);
request.Method = WebRequestMethods.Http.Post;
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = data.Length;

using (var stream = request.GetRequestStream())
{
    stream.Write(data, 0, data.Length);
}

var response = (HttpWebResponse)request.GetResponse();
var responseString = new StreamReader(response.GetResponseStream()).ReadToEnd();
Posted
Updated 24-Jul-20 5:35am
v3

1 solution

The best way to diagnose these issues is to use the network section of the dev tools in the browser to examine the request when you do it via the browser and try and mimic it as best as possible in your code. In your case there are two specific issues, one is that the code on their side has some basic measures to stop people accessing it directly so it obviously checks the referrer header to make sure it has come from the intended embedded iframe. To get around this just spoof the referrer.

C#
request.Method = WebRequestMethods.Http.Post;
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = data.Length;
request.Referer = "http://dniperu.online/buscador/buscardni_11ab.php";


The second issue is that for some reason (again maybe a form of protection?) the response it returns is actually a 500, so that's a custom error page you are seeing in the iframe, not a normal return document. So to parse it you'll need to update your code to read the error response rather than the returned html;

C#
using (var stream = request.GetRequestStream())
{
    stream.Write(data, 0, data.Length);
}

HttpWebResponse response = null;
string responseString;

try
{
    response = (HttpWebResponse)request.GetResponse();
}
catch (WebException ex)
{
    // the 500 response html can now be accessed via ex.Response
    responseString = new StreamReader(ex.Response.GetResponseStream()).ReadToEnd();
}
 
Share this answer
 
v2
Comments
canard29 24-Jul-20 11:42am    
Thank you for your answer. it's working now. I just have to read the HTML to build the object

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900