When i'm trying to edit the Host key on Request.Headers under console application i'm getting exception that says:
The 'Host' header cannot be modified directly.
Parameter name: name
So how can i change it?
As you've seen the .Net Fx does not allow to edit the host header, but since .Net Fx 4.0 there is a seperate 'Host' definition on the HttpWebRequest object. You can use it like this:
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://127.0.0.1/");
request.Host = "yourdomain.com";
Hope that helps you out.
Related
I need to be able to handle requests with missing Accept header. I am using a WebHostBuilder (and ApiControllers). The application is an old MVC application migrated to .NET Core. I somehow can't find an appropriate place in the WebHostBuilder to add a header if it is missing... or do something else that would allow the request to be processed as if it had the header. Any ideas?
HttpRequest request = new HttpRequest();
request.AddHeader("Host", "api.dwebsir.com");
request.Proxy = ProxyClient.Parse(ProxyType.Http, proxy);
this way of adding custom host header doesnt seems to work.
When i ignore the the proxy and while posting without headers also seems to work.
But when i postdata with proxy and request.Header it is not taking and i am not getting any response
But when i postdata with proxy and i am receiving this The requested URL /as/token.oauth2 was not found on this server.
i am pretty sure the error is with Host as headers i am not getting the proper fix
There is a way to do this, as Explanation given here:
http://blogs.msdn.com/feroze_daud/archive/2005/03/31/404328.aspx
next version of the framework (.NET Framework 4.0) will make it easier.
http://blogs.msdn.com/ncl/archive/2009/07/20/new-ncl-features-in-net-4-0-beta-2.aspx
Hope this helps you.
When inspecting a third party api using chrome browser's F12 tool I found there are several interesting headers listed:
:authority:m.somedomain.com
:method:GET
:path:/api/somevalues
:scheme:https
Along with some headers I'm familiar with, such as accept, accept-encoding, etc.
I'm using .Net 4.0 to make http/https requests. When trying to add these headers starting with a colon, an error is thrown on the first item:
httpRequest.Headers.Add(":authority", "m.somedomain.com");
httpRequest.Headers.Add(":method", "get");
httpRequest.Headers.Add(":path", sPath);
httpRequest.Headers.Add(":scheme", "https");
Error message:
Specified value has invalid HTTP Header characters.
After some searching I found article talking about http/2. However in .NET 4.0 there are only http/1.0 and http/1.1 available.
Does that mean I need to upgrade to newer .NET version?
Thanks in advance.
Possibly. http/2 is supported from .NET version 4.6.2.
Click on the Project tab and select properties at the bottom. And then change your .NET version to 4.6.2.
Also I am 99.9% sure that you shouldn't include semi colon in your headers.
Further:
httpRequest.Headers.Add(":method", "get");
The request-method should not be defined in the header. Do it like so.
httpRequest.Method = "GET";
The scheme is usually indicated by the prefix of your url. ie:
string webAddr = "https://www.google.com/";
HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(webAddr);
I am currently trying to write a portable class library in c# that will allow me to send SOAP POST requests to a web service.
Because you are limited in what you can reference when making a PCL, I cannot simply "Add a service reference." Instead, I am trying to manually make the SOAP request and I am getting stuck on adding headers to the HttpWebRequest.
My code is as follows:
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create("website_here");
byte[] xmlStr = getPullCustInfoXml(postParameters);
request.Method = "POST";
request.Headers.Add() <- trying to add headers here.
I can't seem to add any headers because I get the error:
'System.Net.Http.WebHeaderCollectionExtensions.Add(System.Net.WebHeaderCollection, string, string)' is inaccessible due to its protection level
Why would it be inaccessible even after I casted the request as an HttpWebRequest?
Is there a better way to implement SOAP requests when making a portable class library?
I don't get the same error message when I copy and paste your code.
Make sure HttpWebRequest is from System.Net namespace though. The documentation says Headers.Add methods are all public.
Also, try different overload methods. There are:
Add(NameValueCollection)
Add(String)
Add(HttpRequestHeader, String)
Add(HttpResponseHeader, String)
Add(String, String)
More info: http://msdn.microsoft.com/en-us/library/system.net.webheadercollection(v=vs.110).aspx
I need a C# HTTP library that doesn't depend on HttpWebRequest, as I can't access this from the environment I need to run my code (Unity's WebPlayer).
Ideally this would be light weight, but any suggestions are welcome!
I just need to be able to do simple HTTP GET and POST requests, but better REST support would be good.
Thanks!
Edit: As people have pointed out, HttpWebRequest isn't in System.Web, but the fact remains - I can't use it. I've updated my post above.
This post
http://forum.unity3d.com/threads/24445-NotSupportedException-System.Net.WebRequest.GetCreator shows the same error I'm getting.
Implementing your own simple HTTP client using Socket is not all that difficult.
Just use TcpClient().
For the protocol itself, drop down to a connection-per-request paradigm. A typical GET request would look as follows:
GET /url HTTP/1.1
Host: <hostname-of-server>
Connection: close
For the code itself (from memory)
TcpClient client = new TcpClient();
IPEndPoint target = ... // get an endpoint for the target using DNS class
client.Connect(target);
using(NetworkStream stream = client.GetStream())
{
// send the request.
string request = "GET /url HTTP/1.1\r\nConnection: close\r\n\r\n";
stream.Write(Encoding.ASCII.GetBytes(request));
// then drain the stream to get the server response
}
Note that you will need to wrap this code with a simple class that provides HTTPWebRequest like semantics.
Look at System.Net.HttpWebRequest.
It is in System.dll.
Documentation: http://msdn.microsoft.com/en-us/library/system.net.httpwebrequest.aspx
HttpRequest is located in System.Web, which is probably what you were thinking of.
HttpWebRequest is in the System assembly, not in System.Web (perhaps you're confusing with HttpRequest which is used in ASP.NET). It is available in all .NET Framework versions (including Silverlight, WP7, and Client Profile)