in my C# application below method it is hitting service and returning data. At the return statement there was a fortify scan issue raised under the category named Server side request forgery. There is no parameter adding to URL in the method. uri as is used to fetch data. I am completely new.
using (var wc = new WebClient())
{
wc.Encoding = System.Text.Encoding.ASCII;
wc.Headers["User-Agent"] = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 2.0.50727; .NET4.0C; .NET4.0E)";
return JsonObject.GetDynamicJsonObject(wc.DownloadString(uri.ToString())); //here issue raised
}
Related
Code which i tried :
string contents = string.Empty;
using (var wc = new System.Net.WebClient())
{
contents = wc.DownloadString("http://www.bizjournals.com/albany/blog/health-care/2015/10/what-this-local-bank-did-to-control-health-care.html");
}
but its throwing error
The remote server returned an error: (416) Requested Range Not
Satisfiable
It appears that some webservers may return a 416 if your client does not send a User-Agent header. Try adding the header like this:
wc.Headers.Add("User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705");
Based on the this sourcecode I'm not able to retrieve the data from the API into XDocument.
I retrieve the error message
{"The remote server returned an error: (400) Bad Request."}
Question:
I don't know what to do?
XDocument xml = XDocument.Parse(new
WebClient().DownloadString("http://api.arbetsformedlingen.se/af/v0/platsannonser/matchning?lanid=1&kommunid=180&yrkesid=2419&1&antalrader=10000"));
You need to send HTTP headers:
using (WebClient client = new WebClient())
{
client.Headers.Add("Accept-Language", " en-US");
client.Headers.Add("Accept", "application/xml");
client.Headers.Add("User-Agent", "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)");
XDocument xml = XDocument.Parse(client.DownloadString("http://api.arbetsformedlingen.se/af/v0/platsannonser/matchning?lanid=1&kommunid=180&yrkesid=2419&1&antalrader=10000"));
}
I use following code to post querystring
string URI = "http://somewebsite.com/default.aspx";
string myParameters = "param1=value1¶m2=value2¶m3=value3";
using (WebClient wc = new WebClient())
{
wc.Headers[HttpRequestHeader.ContentType] = "application/x-www-form-urlencoded";
string HtmlResult = wc.UploadString(URI, myParameters);
}
But somehow default.aspx does not accept that post call.
The point is when I manually in browser go to http://somewebsite.com/default.aspx all code there is working fine.
My questions is following what do I am missing here to archive the same result when I open page manually as I do it with WebClient?
Thank you in advance!
P.S. 1
I just tried to use GET method to that URL and it has no effect also. How is it possible?
What is difference between manual navigation to page and sending GET/POST?
P.S. 2
I even tried this
wc.Headers["Accept"] = "application/x-ms-application, image/jpeg, application/xaml+xml, image/gif, image/pjpeg, application/x-ms-xbap, application/x-shockwave-flash, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*";
wc.Headers["User-Agent"] = "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; MDDC)";
and and Load event of Default.aspx is not hiting. :(
From your description of what you want to achieve, I think you may have chosen the wrong WebClient method. Instead of UploadString, try DownloadString:
using (WebClient wc = new WebClient())
{
string HtmlResult = wc.DownloadString("http://somewebsite.com/default.aspx?param1=value1¶m2=value2¶m3=value3");
}
So that comment is correct one
"What is difference between manual navigation to page and sending
GET/POST?" - see for yourself, for example using Fiddler. –
CodeCaster
I checked all requests with Fiddler and found that there is code of base page class that redirects to Index page. So Load event is never happened.
I have a puzzling situation with following use case.
user login with SSL to the apache server
apache send back ->
header("Context-Type: plain/text");
echo "send in requests";
user submit new request from c#/.net with httpwebrequest for data
apache send back data in json
user with the desired data and happy to continue
I put above scenario in c#/form.timer; and most of time it runs smoothly as desired in the background without noise. However, after some time (usually several hours), then step 3) goes failed with redirected to login page (http status 302) always.
Apache sever and php are pretty out of date (apache 2.2.10 with php 5.2.xx).
In c#, httpwebrequest has settings shown in the code snippet below.
System.Net.ServicePointManager.Expect100Continue = false;
lrequest = (HttpWebRequest)WebRequest.Create(tmp_url);
lrequest.CookieContainer = ThisAddIn.getCookieContainer();
lrequest.KeepAlive = true;
lrequest.AllowAutoRedirect = false;
lrequest.Method = "POST";
lrequest.ContentType = "application/x-www-form-urlencoded";
lrequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 9.0; MSIE 8.0; MSIE 7.0; Windows NT 6.1; WOW64; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; MAAU; InfoPath.2; .NET CLR 1.1.4322; MS-RTC LM 8; .NET4.0C; .NET4.0E; OfficeLiveConnector.1.5; OfficeLivePatch.1.3)";
int tmp_len = dbyte.Length;
lrequest.ContentLength = tmp_len;
Stream dstream = lrequest.GetRequestStream();
dstream.Write = (dbyte,0,tmp_len);
dstream.Close();
lresponse = (HttpWebResponse)lrequest.GetResponse();
dstream = lresponse.GetResponseStream();
reader = new StreamReader(dstream);
rdata = reader.ReadToEnd();
reader.Close();
dstream.Close();
lresponse.Close();
This page uses frames, but your browser doesn't support them.
This error occurs when I am trying to get the information from our smsgatwaye site.
The code is as follows:
WebClient client = new WebClient();
client.Headers.Add("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR1.0.3705;)");
string baseurl = "http://smsoutbox.in/?user=test&password=test#123";
Stream data = client.OpenRead(baseurl);
StreamReader reader = new StreamReader(data);
string s = reader.ReadToEnd();
data.Close();
reader.Close();
I am requesting to http://smsoutbox.in page which ask for username & password if it is valid than it shows my gateway balance on same page in frame.
But when I get the response, I found this error:
This page uses frames, but your browser doesn't support. (Line) instead of balance in response stream.
How can solve this?
View the source of the page yourself, and look at the frames being used. Open each one separately to determine which URL you need to retreive.
The problem might be that WebClient already submits a UserAgent and by adding another "user-agent"-header you're not replacing the original header.
Use this modified WebClient that internally uses HttpWebRequest's UserAgent property:
http://codehelp.smartdev.eu/2009/05/08/improve-webclient-by-adding-useragent-and-cookies-to-your-requests/
Alternatively it should work to correctly modify the UserAgent like this:
client.Headers["user-agent"] = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR1.0.3705;)");