I'm using an external api which allows the request body of a PUT request to be streamed.
I've been using a TcpClient to achieve this and it works fine but now we have a client that has a proxy. So I figured I'd switch to using either the HttpClient in .net of the WebRequest api.
But I haven't been able to write directly to a request body stream that actually gets sent to the server immediately.
You can get the stream of a WebRequest like this:
var req = WebRequest.Create("url");
var stream = req.GetRequestStream();
But this will send what you've written to the stream after you call GetResponse. And I'm not really interested in the response.
Is it possible to write directly to the server using a stream?
Related
I am using the HttpWebRequest class to implement a web crawler. I would like to use a proxy server with it which usually can be implemented like this:
HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create("https://www.example.com/");
webRequest.Proxy = new WebProxy(proxyUrl, proxyPort);
webRequest.Proxy.Credentials = new NetworkCredential(proxyPassword, proxyPassword);
HttpWebResponse webResponse = (HttpWebResponse)webRequest.GetResponse();
using (Stream stream = webResponse.GetResponseStream())
{
StreamReader reader = new StreamReader(stream, Encoding.UTF8);
Console.WriteLine(reader.ReadToEnd());
}
However, I need to submit an additional parameter to the proxy server. Because the target site uses HTTPS, the parameters must be submitted in the initial CONNECT request that establishes the secure HTTPS tunnel. Otherwise the proxy server is unable to read them due the end-to-end encryption.
The header for the initial CONNECT request should look like this:
CONNECT example.com:443 HTTP/1.1
X-CustomProperty: 12345
So far I was unable to find a solution to achieve this with neither the HttpWebRequest nor the WebClient class from the .NET Framework. Is there any option to pass additional headers in the initial CONNECT request? I am also happy with a solution that uses an additional library.
I have a raw socket I want to make HTTP requests over. I would like to get back nicely-parsed-for-me http responses. Ideally I could feed this raw socket to HttpClient - something in a standard library. "TheWrapperClass" around the socket would allow me to use higher level instructions like again - the ones from HttpClient/HttpClientHandler like : clientHandler.ClientCertificates or clientHandler.Credentials etc.
Something like this maybe?:
HttpClientHandler clientHandler = new HttpClientHandler();
clientHandler.SocketFactory = mySocket/FactoryGoesHere???
HttpClient client = new HttpClient(new CustomMessageHandler());
var resp = await client.GetAsync("http://whatever");
I'm thinking of something like SSLSocketFactory from Java - is there an equivalent of this in .NET that I just haven't found yet?
At the end of the day - I really only want to have a library to invoke that writes HTTP to the wire easily and reads HTTP from the wire easily. If I had that I don't need HttpClient. Alternatively I need a way to use HttpClient to send bytes down the socket I give the class.
Edit:
I tried using a HttpMessageHandler but the HttpRespons is one that I hand craft. I need something that reads a stream and parses the http for me.
If you don't have a need for executing JavaScript, SimpleBrowser might fit for you:
https://github.com/SimpleBrowserDotNet/SimpleBrowser
It will not expose a raw socket for you to use as a bi-directional stream, but it will allow you to navigate via HTTP and receive an HTTP response. The HTTP response can either be the raw text response from the web server, or an parsed XML (XHTML) document.
I have requirement of having proxy server where request from browser will be forwarded to my custom proxy server. Afterwards Proxy server will read request from client and talk to requested resource using some thing like this --
HttpWebRequest request =(HttpWebRequest)WebRequest.Create ("http://www.google.com");
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
then once I have response back in response object write returned HTML to browser , at this point I am stuck and do not know how to write information to browser ?
Can someone please help ???
I have a url which sends sms to the provided mobile numbers. I want to just call this url from windows application but not to open in browser. I just want to execute that url from my windows application.
example of url
http://mycompany.com/sms.aspx?mobilenum=9123444777&Message=this is a test message
I have tried in this way. But it is opening in a browser.
I dont want this to be opened in a browser. I just want to execute this line internally.
System.Diagnostics.ProcessStartInfo sInfo = new
System.Diagnostics.ProcessStartInfo("http://mycompany.com
/sms.aspx?mobilenum=9123444777&Message=this is a test message");
System.Diagnostics.Process.Start(sInfo);
Thanks.
Have you tried to use a HttpWebRequest?
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(#"http://mycompany.com/sms.aspx?mobilenum=9123444777&Message=this%20is%20a%20test%20message");
WebResponse response = request.GetResponse();
response.Close();
You can't "execute" url like that... URL is an address of a resource that you can use only by sending a request to the remote server. So instead you want to post data using a query string (make an HTTP request). You can achieve that for example by using WebClient class...
http://msdn.microsoft.com/en-us/library/system.net.webclient%28v=vs.110%29.aspx
I have a REST API implemented in microsoft Web API.
In my client i use HttpRequestMessage and HttpResponseMessage.
When i am sending a small class, I serialize it to JSON and then send it.
Soon, my class becomes bigger, and I need to JSON the class, zip it (in memory) and send to server. I can no longer use the same technique, I need to send the zip in chunks.
What is the proper way to achieve it ? I have read this post Posting a File and Associated Data to a RESTful WebService preferably as JSON
Need some good articles, I dont know where to start.
On the client side this should work pretty much out of the box...
var httpClient = new HttpClient();
httpClient.DefaultRequestHeaders.TransferEncodingChunked = true;
var content = new CompressedContent(new StreamContent(new FileStream("c:\\big-json-file.json",FileMode.Open)),"UTF8");
var response = httpClient.PostAsync("http://example.org/", content).Result;
You can find an implementation of CompressedContent in WebApiContrib. If you are using earlier than .net 4.5 the request will be buffered client side before sending. Unfortunately the underlying HttpWebRequest doesn't support buffered streaming until .net 4.5