C# - How To Raw Http Request That The HttpWebRequest Class Sends

Sep 27, 2010

I know you are all going to answer "use a debugging proxy server like Fiddler" but it's not that simple.

Here's my situation: I have some code that runs on a server, in an ASP.NET page code-behind (aspx.cs), which (among other things) establishes a connection to another server, grabs some stuff, and then formats it and returns it to the browser.

The problem is that the other server is doing the wrong thing, and so I want to be able to pass a debugging flag into the page (via the query string, e.g. ?debug=true) so that it will print out the completely raw HTTP request that it is sending to the other server so I can see what the heck is wrong. This code is running in several places so I want to be able to just pass in this flag on dev, staging, or production and just see the request, without having to figure out whether the production servers can talk to some proxy server that exists somewhere, etc.

You would think that it would be easy to do this, right? So I feel like I'm crazy or something but I looked at the reference for HttpWebRequest and its parent class WebRequest and -- nothing. No can do. You would think Microsoft would have thought of this. The closest thing is that you can access the "Headers" collection but when I tried it, it omitted some really important headers like "content length" -- so it must be "lying" to me (I know it's lying, because I know for a fact that the remote server is returning a 200 status -- the request is successful, it's just returning bad/different/wrong data)

Here is the asked-for code example:

HttpWebRequest req = (HttpWebRequest)WebRequest.Create("http://www.whatever.com");
req.Method = ... whatever ...;
... other setup for the request ...
/* At this point we are about to send the request.
What does the raw HTTP request look like? */
HttpWebResponse resp = (HttpWebResponse)req.GetResponse();

View 5 Replies


Similar Messages:

Sending HTTP Post That Sends XML?

Nov 11, 2010

I am trying to add Canada Post shipping information to my website. Canada Post has this link to a sample site showing what to send. [URL] I have worked with Canada Post support and they have no sample code for C# or Visual Studio. Can some get me started on how to send the above information from a button click event?

View 1 Replies

Is There A Limit On The Size Of A Http Argument Value In A HttpWebRequest

Apr 4, 2011

I am testing out a few different public RESTful APIs each differing in http argument value name, but in concept all work similarly. However none of the companies are connected so it must be something on my end.

Within .NET I get the following error when trying to obtain the HttpWebResponse:
Message: "The server committed a protocol violation. Section=ResponseStatusLine"
Status: ServerProtocolViolation {11}
Using Fiddler, I am finding that the headers coming back are non-existent and this is the cause of the issue (not even present and improperly formatted):
HTTP/1.0 200 This buggy server did not return headers
The parameter I am sending in is a lengthy text news article. I have stripped ALL puntuation, characters, etc just to make sure nothing wans interferring. I build the text out using a StringBuilder and then create the URI like below:
Dim sb As New StringBuilder
sb.Append("&apiKey=" + HttpUtility.UrlEncode("123456789"))
sb.Append("&inputText=" + HttpUtility.UrlEncode(Me.MyTestText))
Dim ApiHttpWebRequest As HttpWebRequest = CType(WebRequest.Create(New Uri(BaseRequestURL + sb.ToString.Trim())), HttpWebRequest)
Dim ApiHttpWebResponse As HttpWebResponse = CType(ApiHttpWebRequest.GetResponse(), HttpWebResponse)

This code DOES work with most text I send it. The interesting thing I am finding is that if I shorten the text I am sending then it works! I can't figure out exactly the threashold or breaking point. The stringbuilders values expand after the HttpUtility.UrlEncode function is run against it. However the text in size is only about 5-8 kilobytes in size and well below any size limitations set forth by the 'httpWebRequest' .config settings as laid out in the MSDN or by that of any of the APIs I am using (for example 1 limit is 150 kilobytes of text). I even tried setting all of those values to "-1" in the web.config which means: "A value of -1 indicates that no size limit will be imposed on the response headers."

View 2 Replies

C# - How To Change The Request IP In HttpWebRequest

May 13, 2010

I'm developing a website that will connect to a credit card processing gateway webservice. For security purposes this webservice accepts requests only from IP addresses that were previously informed to them.

Since I'm developing locally, my IP changes almost every day. Is there a way for me to change the IP address of a HttpWebRequest so that I can test the Webservice calls locally?

This webservice is accessed through a https address and the methods must be sent via POST.

View 2 Replies

C# - How To Simulate Http Request Using WatiN With Specific HTTP Referrer And Query String

Dec 6, 2010

When I use WatiN to go to a specific web page, how can I fake the HTTP referrer with a query string (i.e. request is from google search with query string q=search_term)? So I can verify that the response header has the 301 redirect for specific referrer URL.

View 2 Replies

HttpHandlers / Modules :: Redirect A Page From Http To Https Using Http Module Begin Request Handler?

Jul 15, 2010

i redirect a page from http to https using http module begin request handler .i am calling webservice using ajax but it is saying webserice not defined .which otherwise works fineits work fine when rediect page in page_load instead .but i need to add function for https to http in every page. i still not know why ajax is not working when i use http module for redirect

View 3 Replies

WCF / ASMX :: Sending XML Inside A Soap Request With HttpWebRequest?

Aug 31, 2010

Im trying to send some xml inside a soap request to a webservice using the HttpWebRequest class. I know this isnt the best way, but my hands are tied by company policy so i have to do it this way. My webmethod has an input parameter "SourceXML" so my soap request has this node

<SourceXML></SourceXML>

I need to insert the xml in this node but whenver I insert it and try to send it to my web method I get an error "400 bad request"

Is this even possible ? I need to insert the xml as a string literal and read it in the web method

[Code]....

[Code]....

View 2 Replies

HttpHandlers / Modules :: Sending Second HttpWebRequest In Sync With First Request

Dec 6, 2010

Using a Windows application I am sending a web request to a web page and getting the response back using the following code

HttpWebRequest oRequest1 = (HttpWebRequest)WebRequest.Create(url);
oRequest1.ContentType = "text/xml";
using (Stream stream = oRequest1.GetRequestStream())
{
stream.Write(fileBytes1, 0, fileBytes2.Length); //fileBytes1 - contains the data of first request
}
HttpWebResponse Response1 = (HttpWebResponse)oRequest1.GetResponse();
->The above code works fine
But I need to enhance the code to accomodate following additional scenario
->Send another request to the same web app and
->get the response back

The first request sent contains the credentials to access the web application the second request sent doesn't have credentials, it must use the credentials of first request. I am using the same (following) code for the second request too. I get an error saying that it's looking for credentials

HttpWebRequest oRequest2 = (HttpWebRequest)WebRequest.Create(url);
oRequest2.ContentType = "text/xml";
using (Stream stream = oRequest2.GetRequestStream())
{
stream.Write(fileBytes2, 0, fileBytes2.Length);
}
HttpWebResponse Response2 = (HttpWebResponse)oRequest2.GetResponse();

So how do I accomplish this? Should I be sending the second request using same session or same connection?

View 2 Replies

WCF / ASMX :: Request Failed With HTTP Status 400: Bad Request Accessing Web Service

May 15, 2010

I have a webservice which works 100% fine on my developer machine. Where Web Service is installed on LOCALHOST on my developer machine,Then i went to my servers, I installed webservice on one server and map it with the server where the website is hosted, Then i tried accessing this service using BROWSER from my web server, it worked fine, That means the mapping was done perfect.Then i run my program on web server (website). It worked fine on page1, then on page2, but when i did the same and call same function on page3, It popped me any error of

View 4 Replies

Determine If A HTTP Request Is A Soap Request On HttpApplication.AuthenticateRequest

Jan 21, 2010

I there a way to know if a request is a soap request on AuthenticateRequest event for HttpApplication? Checking ServerVariables["HTTP_SOAPACTION"] seems to not be working all the time.

public void Init(HttpApplication context) {
context.AuthenticateRequest += new EventHandler(AuthenticateRequest);
}
protected void AuthenticateRequest(object sender, EventArgs e) {
app = sender as HttpApplication;
if (app.Request.ServerVariables["HTTP_SOAPACTION"] != null) {
// a few requests do not enter here, but my webservice class still executing
// ...
}
}
I have disabled HTTP POST and HTTP GET for webservices in my web.config file.
<webServices>
<protocols>
<remove name="HttpGet" />
<remove name="HttpPost" />
<add name="AnyHttpSoap" />
</protocols>
</webServices>
Looking at ContentType for soap+xml only partially solves my problem. For example,
Cache-Control: no-cache
Connection: Keep-Alive
Content-Length: 1131
Content-Type: text/xml
Accept: */*
Accept-Encoding: gzip, deflate
Accept-Language: ro
Host: localhost
mymethod: urn:[URL]

Some clients instead of having the standard header SOAPAction: [URL], have someting like in example above. "mymethod" represents the method in my web service class with [WebMethod] attribute on it and [URL] is the namespace of the webservice. Still the service works perfectly normal. The consumers use different frameworks (NuSOAP from PHP, .NET, Java, etc).

View 4 Replies

First Request Fails With HTTP 400 (Bad Request) After Reading HttpRequest.InputStream

Oct 24, 2010

I develop an asmx web service (i.e. ASP.NET 2.0). There's a piece of code that may read the contents of the HTTP request (via HttpContext.Current.Request.InputStream) while processing it. I realise that InputStream may only be read once for a request, and I make sure I never try to read it more than once.

The problem seems to be that if InputStream happens to be read during the early stages of the application's lifecycle (e.g. after pskill w3wp, during Application_Start), the HTTP request fails with a HTTP 400 - Bad Request error, with no explanation given, no exception thrown and no entry in the httperr log. If it is read later (e.g. within the web method itself), requests run fine whether InputStream is read or not. Application_Start runs fine if InputStream isn't read.

Is this some sort of ASP.NET bug? IIS bug? Or am I doing something wrong by daring to read InputStream? And if so, is there another way to get a look at the "raw" contents of the request without disturbing the inner workings of IIS/ASP.NET? In short, adding this code within Application_Start is enough to reproduce this error: using (StreamReader reader = new StreamReader(HttpContext.Current.Request.InputStream))reader.ReadToEnd();

View 1 Replies

WCF / ASMX :: Accessing Response Of HttpWebRequest, Even When Request Returns 500 Internal Server Error

Oct 22, 2010

Hoping someone can point me to a solution, haven't been able to find one yet.

I'm using the standard way for send HttpWebRequest

[Code]....

As you can see from Fiddler/SOAPUI it does return the SOAP fault, however using HttpWebRequest i can't capture the response.

Returning 500 triggers my exception handling and Capturing the WebException doesn't expose the response.

Does anyone know how to capture this response xml in the case of 500 Internal Server Errors.

View 1 Replies

How To Hunt Down HTTP Request

Feb 25, 2010

It uses asp.net (webforms), bunch of jquery plugins and whatnot.

Problem: there's unnecessary HTTP request to localhost/undefined and that makes every request take ~1s longer than needed.

Question: is there any tactic to find guilty code?

Clue: undefined makes me think that there's JS involved.

Firebug log:

GET /undefined HTTP/1.1
Host: localhost:17817
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.1; lv; rv:1.9.2) Gecko/20100115
Firefox/3.6
Accept: image/png,image/;q=0.8,/*;q=0.5
Accept-Language: lv,en-us;q=0.7,en;q=0.3
Accept-Encoding: gzip,deflate
Accept-Charset: ISO-8859-13,utf-8;q=0.7,*;q=0.7
Keep-Alive: 115
Connection: keep-alive
Referer: http://localhost:17817/Default.aspx

if you right click a line in the code, it gives you a conditional breakpoint. Check the name of the variable that has the URL value, and set a condition when typeof URL === 'undefined' or url === 'undefined' and look at the stack

Just set breakproint at line 3400 (with no conditions). It does not go through there.

3397 ajax: function( s ) {
3398 // Extend the settings, but re-extend 's' so that it can be
3399 // checked again later (in the test suite, specifically)
3400 s = jQuery.extend(true, s, jQuery.extend(true, {}, jQuery.ajaxSettings, s));

Narrowed issue down a bit => commented out 1 ascx controller that renders some images and uses gallerific jquery plugin and magic request dissapeared.

Problem was with galleriffic plugin initialization. It couldn't find anchor and it's href for creating thumbnails when there were no images or something. It's cool now...

View 3 Replies

C# - Http Request Issue In Asp.Net

Jul 24, 2010

I am using C# + VSTS 2008 + .Net 3.5 + ASP.Net + IIS 7.0 + ADO.Net + SQL Server 2008. I want to develop an ASP.Net aspx page which has the following function,

1 It could accept 3 Url parameters param1, param2 and param3, and the request looks like this,

http://mysite.com/foo.aspx?parame1=abc¶m2=def¶m3=ghi

2 When the page is responsed to client browser, I want to display a text input and a submit button nearby in the result html page, and value of text input is the same as param1, in this sample, abc will be displayed in text box, in the browser address bar, I want to keep the original long url http://mysite.com/foo.aspx?parame1=abc¶m2=def¶m3=ghi;

3 When the user change the value in text input, and click submit button, I want to send this request again to foo.aspx, and changing the param1 value to the value which user entered in text input, and at the same time, keep values of parame2 and param3 the same as last request's response. For example, when user requests http://mysite.com/foo.aspx?parame1=abc¶m2=def¶m3=ghi, and the page displays, when user changes text input from abc to google, the new request will be http://mysite.com/foo.aspx?parame1=google¶m2=def¶m3=ghi

Any reference samples? My question is I do not know how to implement so many functions in one aspx page.

View 1 Replies

Generating HTTP Request?

Aug 21, 2010

In how many ways can an HTTP request be generated?

View 2 Replies

Get MAC Address From HTTP Request

Jul 22, 2010

picking up the Users MAC address from the HTTP request.

The users will be from OUTSIDE my network.

View 3 Replies

HTTP Response And Request In Binary

Feb 21, 2010

I need to I can make the web request to a webservice which take a XML argument. And is expected to return a Binary response. I am able to make the request but while recieving the response back I am unable to get the response in binary. When I read the response
using streamreader see the header and some attached "HEBRISH" words probably binary but unable to sepreate it out. Please help in seprating out the binary the response data.

View 1 Replies

MVC :: Handle Dates In Mvc When Request Is A Http GET?

May 10, 2010

how do u handle dates in mvc when request is a http GET? I remember some discussoin on this last year but cant find the posts.. things like culture unaware, splitting the date field to 3 text fields insted of 1.

View 6 Replies

How To Edit HTTP Request With Fiddler

Aug 5, 2010

Is it possible to edit HTTP request using fiddler so that my asp.net application hosted elsewhere return true for the following code block

HttpContext.Current.Request.Url.Host == "localhost"

View 1 Replies

Why Parameters Are Encoded When Sent Through HTTP Request

Feb 17, 2011

I am curious to know the reasons why the HTTP request encodes(URLEncoded) all the parameters before sending it across the network in POST request.

View 1 Replies

Making Http Request From Code Behind

Mar 3, 2011

I am trying to implement Message API

I am not sure how I will be calling this from code-behind and in their snippet it says:

https://platform.3cinteractive.com/api/send_message.php

POST
username=aRDSe3vcaMzh06YrMcxcQw==&password=1BSvQc6lpNlnp4ufWgRLPHNJ7RMrL8CcaWCzL1Vtw+Y=&phone_number=+11234567890&trigger_id=1105&message=howdy

View 2 Replies

MVC :: IIS Responding With 404 On HTTP HEAD Request?

Jul 28, 2010

I'm currently trying to setup testing with Selenium. It seems that Selenium sends a HEAD request to my route /Home/Index before it sends the GET request. This causes all my tests to fail. I was wondering how I would go about getting HEAD requests to respond correctly (not 404).

View 3 Replies

Load Images With Http Request?

Nov 15, 2010

i have a usercontrol which displays 1 inage this control is used several times on my page. the problem i have some images can be large so i need to load them using http request i think, i want to display a loading image until the actual image is loaded, what would be the best way to do this?

View 2 Replies

Read A HTTP Post Request With Some Data

Nov 9, 2010

I am using an Zencoder API to transcode video files. Once the job is completed they will do a HTTP POST with XML or JSON data containing the Job ID and other info to the url we provide.

So if URL is www.abc.com/GetZencoderResponse.aspx or .ashx, how can I read the data they post?

excuse me for the underlines, i thought that would make my query stand out.

View 6 Replies

Send A Plaintext Raw Http Post Request?

Jan 29, 2010

All I need is to send a http post request I pulled from fiddler.

I do not want to use HttpWebRequest class. It makes it hard to set up a request, does not allow to change host,

and when it does send it it looks nothing like the request I want. The server is very sensitive and unless I copy the request headers 100% it will return an empty page. Why can't I just type in the headers and send it that way? What would be 5 minutes of
work in php is taking the whole evening in asp.net.

This is what I am trying to send, simple as it gets: ...

View 9 Replies







Copyrights 2005-15 www.BigResource.com, All rights reserved