how to change Console App output result only not whole html - c#

When I run my Html code it only display JSON result but if I use console to call the http it display the whole code of the web form. What can I change to make it only display the JSON result on console app?
[Html Code]
[console result]
using (var client = new WebClient()) //WebClient
{
client.Encoding = System.Text.Encoding.UTF8;
client.Headers.Add("Content-Type:application/json");
client.Headers.Add("Accept:application/json");
var result = client.DownloadString("http://localhost:49299/test.aspx");
Console.WriteLine(result);
Console.ReadLine();
}

You are reading the whole web page response, which includes all the HTML and the JavaScript.
Apparently, what you want is the result of a JavaScript function that is being run on the page by the browser. They way I would solve that is by letting the JavaScript do a XHR call back to the server containing the result.
Since you are using ASP.NET you could set up a ASP.NET Web API project to respond to such REST calls.

Your server code just only return html that contain javascript. If you request to the server by a web browser, the browser will execute javascript and you see the right data. Meanwhile, if you make requests by C# code, javascript will not be executed. So you need to know
How asp.net webforms return json data here How to return a JSON object in standard web forms .Net
How to consume google geocoder api by C#. You can manually request to google api by C# or just utilize a wrapper. May be this https://github.com/chadly/Geocoding.net. I have not yet tried this before

Related

how can i get html that is generated via AJAX with HTML Agility Pack?

I am trying parse Web Page. part of that page generated via AJAX,
WebClient.DownloadString I can get whole HTML except that code which is generated via AJAX? can you someone help me please?
My code is:
var client = new WebClient();
client .Headers.Add(HttpRequestHeader.UserAgent, "UserAgent,Mozilla/5.0 (Windows NT 5.1; rv:14.0) Gecko/20100101 Firefox/14.0.1");
client.Headers.Add(HttpRequestHeader.Cookie, "USER_PW=xxxxxxxxx; PHPSESSID=xxxxxxxxxxxxxxxxxx");
var html = client.DownloadString("xxxxxxxxxx");
I need list of vacancies...
its possible to get the content which is generated via AJAX But its not straight forward task, All you get is the page source (the same which you can see when you right click and say view page source)
To get the ajax content you must note down the url that the AJAX call hits and then make another call to this url to get the content. You can get this if you inspect the Network Tab of the developer window in any browser or even by looking at the javascript code within.
Disadvantages: This also means you will just get the response of the AJAX call but what if the javascript is manipulating this response (like creating a table out of json response) . This you might have to manually do it on your end.
So it also means you will end up in coding the same logic as in the javascript to get the result HTML, And this seems a lot of pain and too many holes for error.
Advantage: If all you are concerned with is only the data (like data from the website database) in the HTML and not the exact HTML itself, Then this approach will work for you.

send Request and get response from php in my asp.net

My website is Asp.net mvc and I want to use one Company post my Product
their web is php and web service is php in document is clear how use php but I want call php functions and get response on my asp website how can I send request
<?PHP
$soap = new SoapClient("http://www.froservice.ir/F-W-S-L/F_Gateway.php?wsdl");
$ResNum = $_REQUEST['ResNum'];
$RefNum = $_REQUEST['RefNum'];
$State = $_REQUEST['State'];
$VerifyUrl = "http://www.YourSite/verify.php";
$Res = $soap->FVerifyEndbuy($ResNum,$RefNum,$State,$VerifyUrl,$Username,$Password);
$Res=urldecode($Res);
echo $Res;
?>
how call this function?
You can try adding a service reference to your MVC project. Right-click References, Add Service Reference, use the soap client URL. You will then have a class that you can call methods on like any other class. But I wouldn't guarantee that service is completely interoperable with ASP.NET service references. If it doesn't work, you can always build a request and parse the response manually with HttpClient

C# Send web request with disabled javascript

I want to download content of one website programatically and it looks like this content is loaded by ajax calls. When I simply disable javascript in my browser, only 1 request is made by this page and all content is loaded without AJAX.
What I need to achieve is to make a web request which will tell web page that I have disabled javascript so it returns me all the content and not just empty body tag with no content at all.
Any suggestions how to do that?
You need to mimic browser.
Steps:
Use Fiddler and see what is sent by browser.
Set the same headers/cookies/user agent via C# code.
If does not work - compare request your code makes with browser's one by using Fiddler as proxy for your C# code (set proxy to http://localhost:8888)

How to create a JSON variable from a SOAP Response?

I am using ASP.NET 4.0 and need to return a SOAP (XML) Response to a JSON variable within javascript on the page. Than I would like to be able to call the variable and it's properties as you would with any JSON variable. The Soap Web Service (.asmx) file is not on the server where I need to build the client-side (receiving the request and putting it into a JSON variable). Also, to make this more complicated, the XML Request that gets send to the Web Service needs to send a UserName and Password to be able to return the items.
The URL for the Web Service is here: http://ws.idssasp.com/members.asmx?wsdl
Figured I would create a Visual Studio Web Application Project (C#), which I was able to do and connect to the Web Service just fine, however, This needs to be on a page that javascript uses to output the items that come from the methods of the web service. So, a .aspx file would not work in this case, since it would need to output only the result of the web service response in a JSON variable within a tag (probably in the head of the page, but doesn't matter to me where). Or it could dynamically create a .JS file (which would probably be better, since it would be cached and wouldn't need to call the web service multiple times if the js file exists on my server). However, I'm not sure on what to build in Visual Studio to accomplish this? And I'm not sure on how it would be used to output it onto the page. I suppose the JSON variable could also be stored within a Members.json file on the server and could just call that to load up the json needed.
How to return a JSON array from SOAP, XML, Response... after sending a request to another server with UserName and Password in the header of the SOAP Request. There is a page here that explains the XML needed for the Request and what the response will look like: http://ws.idssasp.com/members.asmx?op=GetMemberList&pn=0
On this same page, they show you how to do it via PHP, but PHP is not available, and only have ASP.NET 4.0 available to me. Here is their PHP way of doing it:
$clientWS = new SoapClient('http://ws.idssasp.com/Members.asmx?wsdl');
$namespaceWS = 'http://ws.idssasp.com/Members.asmx';
$dmsClientU = '';
$dmsClientP = '';
$headerBodyWS = array('UserName' => $dmsClientU, 'Password' => $dmsClientP);
$headerWS = new SOAPHeader($namespaceWS, 'AuthorizeHeader', $headerBodyWS, false);
$clientWS->__setSoapHeaders(array($headerWS));
$results = $clientWS->GetMemberList();
print_r( $results );
How would I be able to do the same thing here is ASP.NET 4.0, but instead of returning the XML result, return a JSON variable that gets used within a script tag on the page?
Or maybe I am overthinking this and there is a better solution?
If you are connecting to the web service and retrieving objects without issues, you should be able to construct JSON objects out of the properties of the SOAP responses.
I suggest creating a web service in ASP.NET, converting the SOAP response to JSON in the C# server code, then using AJAX in the JavaScript of your page to retrieve the JSON from your web service. Basically, you would be creating your own specialized conversion web service for your project that sits in the middle.
Keep the credentials you need server-side for your .asmx conversion service. Whatever you do, do not put credentials in the client-side JavaScript for a web service call, even if it lets you avoid writing server-side code.
For some reference on ASP.NET web services:
http://msdn.microsoft.com/en-us/library/bb398998%28v=vs.100%29.ASPX
http://msdn.microsoft.com/en-us/library/bb763183%28v=vs.100%29.ASPX

Invoke a tag javascript click function from Webclient

I create a web scraping application in C# 4.0. I use Web Client in this application.
now i am facing problem in a A tag click event. Please see the code below
2
Please see this image.
I try to download the html in that page. how to execute this href code from Webclient.
Please help.
You cannot execute javascript from WebClient. You can simulate such request and achieve the required response from the server but it takes some reverse-engineering.
To understand how to get the desired response from the server first you have to perform this operation through browser and record request that is generated by clicking on that link (for that you can use Fiddler Web Debugger). Than you have to recreate such request from your WebClient, that is - send all the required data to the server properly formatted, along with cookies and correct request type (GET or POST - synchronous or asynchronous).
Why you cannot use WebClient for javascript execution is nicely described here
And how you can create a request that resembles the one created by javascript click can be deduced from this.

Categories

Resources