Removal of HTTP webrequest elements

╄→尐↘猪︶ㄣ 提交于 2019-12-23 04:29:13

问题


I'm looking to remove certain elements from a HTTPWebRequest I have attached the elements I need to remove in an image (elements are coloured red):

I've tried:

 System.Net.ServicePointManager.Expect100Continue = False

for one of the elements but to no avail I've also tried:

webRequest.Headers.Remove(HttpRequestHeader.Connection)

Any help would be really appreciated.

here is my code:

    Dim content As blah.Content = New blah.Content
    Dim inputguid As String = Guid.NewGuid.ToString
    Dim service As blah.WebService = New blah.WebService
    Dim str As New System.Xml.XmlDocument
    Dim payload As blah.Payload = New blah.Payload
    System.Net.ServicePointManager.Expect100Continue = False
    'payload
    str.LoadXml(xmlstr)

    'manifest
    service.payloadManifest = New blah.PayloadManifest
    service.payloadManifest.manifest = New blah.Manifest() {New blah.Manifest}
    service.payloadManifest.manifest(0).element = "GetVehicleServiceHistory"
    service.payloadManifest.manifest(0).namespaceURI = "http://www.starstandards.org/STAR"
    service.payloadManifest.manifest(0).contentID = "Content0"
    service.payloadManifest.manifest(0).version = "2.01"
    service.SoapVersion = SoapProtocolVersion.Soap11
    service.UserAgent = "VendorName"

    payload.content = New blah.Content() {content}
    ReDim Preserve payload.content(0)
    payload.content(0).Any = str.DocumentElement
    payload.content(0).id = "Content0"

    service.Timeout = -1
    service.Url = "http://localhost:8080"
    service.ProcessMessage(payload)

and then within the reference.vb of my webservice:

    Protected Overrides Function GetWebRequest(ByVal uri As Uri) As WebRequest
        Dim webRequest As HttpWebRequest = DirectCast(MyBase.GetWebRequest(uri), HttpWebRequest)
        Dim sp As ServicePoint
        sp = ServicePointManager.FindServicePoint(New Uri(Me.Url))
        sp.ConnectionLeaseTimeout = 0
        webRequest.Headers.Remove(HttpRequestHeader.Connection)
        webRequest.KeepAlive = False
        webRequest.Timeout = 100000
        webRequest.ReadWriteTimeout = 100000

        Return webRequest
    End Function

回答1:


You would like to get rid of some of the more detailed headers from your HTTP message?
One easy thing you can do would be this:

webRequest.ProtocolVersion = HttpVersion.Version10

Several of those headers aren't in the 1.0 version of HTTP and only exist in the 1.1 version (Key Differences), so downgrading to the 1.0 version should take care of a lot of those 'extra headers' for you.

If you want to get rid of the Content-Length header, then I'm pretty sure you'll have to switch from POST to GET. If you're doing a POST, then Content-Length is typically required!

If that just doesn't do it for you then you'll have to go the final step and stop using HttpWebRequest (which is doing a lot of the HTTP work for you) and just do a plain TCP connection (perhaps with the TcpClient class?) and specify the exact HTTP message you want to send. It's a lot more work, but that's likely to be the only way to send a very specific message where you control all the HTTP headers being used.

Hope that helps!



来源:https://stackoverflow.com/questions/8476296/removal-of-http-webrequest-elements

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!