Why is Web Request content truncated - asp.net

I have a Web Service which is doing some screen scraping of an aspx website.
I can get it to log in successfully, but then when I submit a request, it returns a server error. When I check it out with Fiddler, it shows that the content (the query string) is being truncated so it is not all submitted. The content is quite long over 3600 chars. (Not my choice, it's just the way the website was created and what it expects.)
HttpWebRequest webRequest = WebRequest.Create(REQUESTUSAGE) as HttpWebRequest;
webRequest.CookieContainer = this.Cookies;
webRequest.Method = "POST";
webRequest.ContentType = "application/x-www-form-urlencoded";
StreamWriter requestWriter = new StreamWriter(webRequest.GetRequestStream());
requestWriter.Write(GetPostDataForRequest());
WebResponse response = null;
try
{
response = webRequest.GetResponse();
}
catch (Exception ex)
{}
The GetPostDataForRequest returns the content, but like I said, Fiddler shows it is missing the last 600 chars or so for no apparent reason. The debugger shows the string is returned as expected, but somehow it does not get written correctly.
So how to I get it to submit the full string?

OK, I got this resolved. I was not closing the requestWriter.

There are usually limits to the request size - take a look at maximum length of HTTP GET request?
It appears that you are running into a browser issue, not a server issue. Can you issue the request with a command line tool (i.e. something like wget) to verify that it is not a problem with the server?
You can also try a different browser, which may have different limits.

Related

Can I take the SessionID returned from the SOAP API and use it in a URL Access call to SSRS?

Two things; first is I keep getting a 401 exception on the last line. I had thought that re-using the session would allow me to not only NOT have to resend the credentials but would also let me access the report by URL. It does neither...
Second, what do I do with the response once I have it to display it in the browser for the User?
This is what I have so far but I am unsure where to go with this from here....
var rs = new ReportExecutionService.ReportExecutionService();
rs.Credentials = new System.Net.NetworkCredential("UserID", "Pswd", "myDomain");
var execInfo = rs.LoadReport("/Nav Reports/OpenSalesOrderByCustomer", null);
var format = "HTML4";
string requestUri = string.Format(
#"https://reports.myServer.com/ReportServer/?{0}&rs:SessionId={1}&rs:Format={2}",
execInfo.ReportPath,
execInfo.ExecutionID,
format
);
WebRequest request = WebRequest.Create(requestUri);
request.UseDefaultCredentials = false;
request.Credentials = new System.Net.NetworkCredential("UserID", "Pswd", "myDomain");
var response = request.GetResponse();
For Background info -->
I am trying to setup Remote Report processing with SSRS on my asp.net web forms app. I would simply use the Report Viewer control but it's hideous looking and not acceptable to our user base. The URL Access returns a much better looking and formatted report and is acceptable but I have to pass Security Credentials. I would use SOAP API except it returns a non-styled html 'blob' and also removes the Toolbar functionality that we want to use.
So, I seem to be left with figuring out a way to use the SOAP API to authenticate a session and then somehow use that to use URL Access.
Just call the SOAP API, get the bytes, and return those bytes to the user with appropriate headers, content-type: application/pdf, content-disposition:attachment;filename=report.pdf. You don't need to do multiple calls to the report server.

strange cache issue with HttpWebRequest

We have a web app that calls a url of an outside search provider to load search results. Each time our page is hit it can pass in a current page number. This page number is included in the url sent to the 3rd party. I've noticed that while they report 45 pages of results, if I go to one of the pages that includes their results and then try to navigate to another page that has additional results from them, the same results from the first page are loaded.
I tried setting up my HttpWebRequest object to disable caching but everything I've tried doesn't seem to work. And, considering the url changes each time due to the page number, I wouldn't think that it would really be a cache issue. But here's where it gets interesting.
If I copy the url that I'm retrieving in code and paste it into chrome it loads the correct results. I then refresh the web app page and it too now loads the results for that page. This makes no sense to me. The code is running locally but since it's running within asp.net, it isn't using chrome to create the web request, so why does this happen?
Here's the code I have that calls the url and returns the result.
public static string FetchPage(string url)
{
//Specify the encoding
Encoding enc = System.Text.Encoding.GetEncoding(1252);
HttpWebRequest.DefaultCachePolicy = new HttpRequestCachePolicy(HttpRequestCacheLevel.Default); ;
//Create the http request
var request = (HttpWebRequest)WebRequest.Create(url);
request.CachePolicy = new HttpRequestCachePolicy(HttpRequestCacheLevel.NoCacheNoStore);
//Create the http response from the http request
var httpWebResponse = (HttpWebResponse)request.GetResponse();
//Get the response stream
var responseStream = new StreamReader(httpWebResponse.GetResponseStream(), enc);
//Read the response stream
string htmlStream = responseStream.ReadToEnd();
//Close the web response
httpWebResponse.Close();
//Close the response stream
responseStream.Close();
return htmlStream;
}
Please try these following lines to set no-cache in both HttpWebRequest and WebResponse
For Request
Request.Headers.Set(HttpRequestHeader.CacheControl, "max-age=0, no-cache, no-store");
Request.CachePolicy = new System.Net.Cache.RequestCachePolicy(System.Net.Cache.RequestCacheLevel.NoCacheNoStore);
For Response
Response.Headers.Add(HttpRequestHeader.CacheControl, "no-cache");

How to capture IIS 500 server error detail when received in a WebException?

I have code that uses an HttpWebRequest to request a .ASP page (asp 3.0), with some parameters in the URL. That ASP page will generate a PDF by getting some data from a database. Due to error in the ASP code, I'm getting a WebException with IIS's 500 server error when I try to get the response from the request. And that's fine, but the exception's message doesn't really say anything other than 500 error occurred.
If I copy paste the URL that I'm requesting in IE, I do get some details since I have friendly errors disable and IIS configured to send the real error text to the user.
"Technical Information (for support personnel)
Error Type:
ADODB.Recordset (0x800A0CC1)
Item cannot be found in the collection corresponding to the requested name or ordinal.
"
I've done a QuickWatch on the Exception and visited all of the properties, but not one contained this data.
Is there a way to get this information on the WebException?
Thanks.
Is there a way to get this information on the WebException?
Yes, you can read the response body:
try
{
...
}
catch (WebException ex)
{
using (var stream = ex.Response.GetResponseStream())
using (var reader = new StreamReader(stream))
{
string text = reader.ReadToEnd();
// text will contain the response from the server
}
}

ASP.NET Facebook graph api

PROBLEM:2nd call to the graph api fails every time with Bad Request 400 error
If I do the following things, I can never get past #4.
Authenticate with facebook to get authtoken. It redirects back to the page with code querystring param
I get the authtoken from param and make a call to the following url
string url = "https://graph.facebook.com/me?access_token=" + Token; (any graph api call works fine on the first call)
I get json data back. No problem. Now I have the id info from facebook.
I try to repeat the process. Every call to https://graph.facebook.com/me/xxxxxx fails. If I try getting a new token or using the initial token I get Bad Request 400 error.
There must be an order of operations that needs to occur (requests and getting tokens) that I don't understand.
(IT IS NOT AN apikey or apisecret PROBLEM)
What you describe should work. Be sure that when you get the 400-Bad Request error that you catch the WebException and read the content of the Response. It should provide you with the reason the API call failed. The catch portion of your try/catch block would look something like this:
catch (WebException ex)
{
using (StreamReader reader = new StreamReader(ex.Response.GetResponseStream()))
{
string jsonMessageString = reader.ReadToEnd();
}
}
Try this API, it's new and supported. (ie. I support it) See if it's authentication resolves your issue. If you're like me then this would have saved you hours of time fiddling with it. Well worth the 50 bucks.

show webservice soapexception response in the client in ASP.NET

I am trying to make a Test Webservice and throw a SoapException. But when I open the service through browser, it shows Internal server error - 500.
If i try to consume it manually by sending a manually created SoapRequest (in stringbuilder), I get the same error "Servererror - 500" in Visual Studio itself in the line "WebResponse response = req.GetResponse()"
Is there anyway I can actually see the "fault in response XML".
It sounds like there is something wrong with the service and you need to debug it on the server side rather than the client side. I come to this conclusion because you have a problem with your client code and a web browser.
Assuming you are using .NET have you enabled display of ASP.NET errors on the server? See this article for info.
Update:
So you are throwing an error on the server and want to get the error text on the client? An error on the server is supposed to result in a 500 error message and is unlikely to return any XML to the client. Perhaps you can pass something to the SoapException constructor?
Have you looked at the docs for SoapException? They have some examples of passing information using the Detail property of SoapException.
Can you get to the asmx (assuming you are using asmx) in the browser to see if it is working at all?
after surfing through for 5-6 hours finally got it......here it is:
When you are getting the response manually, use the following:
try
{
WebResponse response = req.GetResponse();
Stream str = response.GetResponseStream();
StreamReader rdr = new StreamReader(str);
Response.Write(rdr.ReadToEnd());
Response.End();
}
catch (WebException webEx)
{
Stream str = webEx.Response.GetResponseStream();
StreamReader rdr = new StreamReader(str);
Response.Write(rdr.ReadToEnd());
Response.End();
}

Resources