Answered by:
Get Posted Data from a URL in c#

Question
-
User-1282720514 posted
HI,
I have a url which takes 2 parameters one is code and other one is name, by submitting the form manually it is giving the outputs.
I want to write a program which will call that url and (pass the parameters and submit the data automatically) and get the response in c#.
Thursday, December 12, 2019 7:46 AM
Answers
-
User665608656 posted
Hi Mrutyunjaya,
In fact, you cannot transfer submit button from the current page to a page under another server.
But if you just pass the code and name values, you need to handle and accept these two parameters in the code behind of the url page.
However, it is not possible to implement the submit function in the url page.
If you just want to submit the values of code and name to a page from the current page, you can transfer directly between the two pages without the need to pass the url of another server indirectly.
Best Regards,
YongQing.
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Tuesday, December 17, 2019 6:34 AM
All replies
-
User753101303 posted
Hi,
Depends on your constraints. If really unfamiliar with that, you could start to give this a try using the outdated https://docs.microsoft.com/en-us/dotnet/api/system.net.webclient.downloadstring?view=netframework-4.8 approach to make it work simply.
You'll always be able to rewrite later this method using a more modern cross platforrm http client such as https://docs.microsoft.com/en-us/aspnet/web-api/overview/advanced/calling-a-web-api-from-a-net-client at a later time when/if needed
A search in your favorite web search engine should have really returned tons of stuff about doing this kind of http query form server side C# code.
You are using .NET 4.x ?
Thursday, December 12, 2019 8:31 AM -
User-1282720514 posted
Hi Patrice,
Thanks for your reply
I have already tried in that way but it's simply giving the page which is asking parameter inputs. Please see below the output.
<html><head>
<title> Display</title></head><body>
<FORM ACTION="/gtft/Print"METHOD=POST>
<H2 ALIGN="CENTER" >Print Online</H2>
<PRE><B>code:</B></PRE>
<INPUT TYPE="TEXT" NAME= "iec" SIZE=10 MAXLENGTH=10>
<BR>
<PRE><B>Name (Min 3 letters):</B></PRE>
<INPUT TYPE="TEXT" NAME= "name" MAXLENGTH=5>
<BR>
<BR>
<INPUT TYPE="SUBMIT" NAME= "" ></FORM>
</body>
</html>I am using .net 4.5
I am able to fire the submit with the parameters by the help of Response.Write ....which is redirecting to a url but not able to capture the redirected url.
I just have a doubt. Can I get the redirected url generated html if possible.
Thursday, December 12, 2019 9:53 AM -
User753101303 posted
This is a bit why I suggested an outdated but simple approach as depending on what you are trying to do the whole process is not that easy. For a POST it would be rather https://docs.microsoft.com/en-us/dotnet/api/system.net.webclient.uploadstring?view=netframework-4.8
You have also https://stackoverflow.com/questions/13039068/webclient-does-not-automatically-redirect
They could also check the user agent (you can then change that) to prevent automating their web page.
Depending on what it does, check if they don't have an API. I always favor using an API over "browser and/or http automation" (and try to refrain otherwise as I consider the absence of an API as a will to NOT allow consuming this "service" programmatically).
Thursday, December 12, 2019 10:05 AM -
User-1282720514 posted
by implementing the first approach I am getting proxy authentication error.
I have set also the default still its giving error.
string apiUrl = url;
var input = new
{
iec = iec_code,
name = name
};
string inputJson = (new JavaScriptSerializer()).Serialize(input);HttpClient client = new HttpClient();
HttpContent inputContent = new StringContent(inputJson, Encoding.UTF8, "application/json");
WebProxy webProxy = new WebProxy("http://proxy.corp.ups.com:8080", true)
{
UseDefaultCredentials = false,
Credentials =CredentialCache.DefaultNetworkCredentials
};
HttpResponseMessage response = client.PostAsync(apiUrl + "/IecPrint", inputContent).Result;
if (response.IsSuccessStatusCode)
{
}is there something I need to put default in place of http://proxy.corp.ups.com:8080" ?
Thursday, December 12, 2019 11:34 AM -
User665608656 posted
Hi Mrutyunjaya,
<FORM ACTION="/gtft/Print"METHOD=POST>Based on the code you provided, are you currently using webform or mvc?
And what page does your "/gtft/Print“ represent? Is it a webform page or a controller action or something else?
I just have a doubt. Can I get the redirected url generated html if possible.What do you mean by this sentence? Do you want to prevent the page from jumping to "/gtft/Print“ page after submitting it, then to get the html of the "/gtft/Print“ page?
We hope you can elaborate your needs and provide more complete information.
Best Regards,
YongQing.
Friday, December 13, 2019 2:44 AM -
User-1282720514 posted
Hi YongQing,
I am calling an url which is hosted in a different server and not in my domain, and that url taking 2 parameters "code and name" and there is one submit button.
After submitting the data, some response is coming, I want to capture the response.
How i am submitting the form code below..frm.submit()
HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
request.Method = "GET";
request.AllowAutoRedirect = false;
WebRequest.DefaultWebProxy.Credentials = CredentialCache.DefaultNetworkCredentials;
HttpWebResponse response = request.GetResponse() as HttpWebResponse;
StreamReader reader = new StreamReader(response.GetResponseStream());
string responseText = reader.ReadToEnd();
responseText = responseText.Replace("<INPUT TYPE=\"TEXT\" NAME= \"code\" SIZE=10 MAXLENGTH=10>", "<INPUT TYPE=\"TEXT\" NAME= \"code\" value='" + code + "' SIZE=10 MAXLENGTH=10>");
responseText = responseText.Replace("<INPUT TYPE=\"TEXT\" NAME= \"name\" MAXLENGTH=5>", "<INPUT TYPE=\"TEXT\" NAME= \"name\" value = '" + name + "' MAXLENGTH=5>");
responseText = responseText.Replace("<INPUT TYPE=\"SUBMIT\" NAME= \"\" >", "<INPUT TYPE=\"SUBMIT\" NAME= \"btnsubmit\" ><script language=javascript>frm.submit();</script>");
Response.Write(responseText);but as the data is posted it gets redirected from my current page i.e Test.aspx so I don't have control over the page to get the response.
I am using webform with .net 4.5 version..
Friday, December 13, 2019 7:19 AM -
User665608656 posted
Hi Mrutyunjaya,
In fact, you cannot transfer submit button from the current page to a page under another server.
But if you just pass the code and name values, you need to handle and accept these two parameters in the code behind of the url page.
However, it is not possible to implement the submit function in the url page.
If you just want to submit the values of code and name to a page from the current page, you can transfer directly between the two pages without the need to pass the url of another server indirectly.
Best Regards,
YongQing.
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Tuesday, December 17, 2019 6:34 AM