Vb .Net: Help returning 401 Error

  • Thread starter Thread starter old_School
  • Start date Start date
O

old_School

Guest
I'm not getting any logical errors but my code is not returning a 401. Instead it says its redirecting and cant show the code because redirect is not supported or when using postman it just shows the html for a page:


code:

Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest
Dim oImporter As Importer
Dim oResponse As Domain.Structures.Structures.ReturnResponse
Dim Input_sr As StreamReader
Dim sData As String = String.Empty
Dim bolError As Boolean = False
Dim obj As New Domain.Models.Order.Order
Dim sError As New order_response_error
Dim sResponse As New order_response_success

Try
Input_sr = New StreamReader(context.Request.InputStream)
sData = Input_sr.ReadToEnd
Input_sr.Close()

If context.Request.ContentType.StartsWith("application/json") Then
obj = JsonConvert.DeserializeObject(Of Domain.Models.Order.Order)(sData)
If obj.api_key = ConfigurationManager.AppSettings("api_key") Then
oImporter = New Importer(sData)
oResponse = oImporter.ImportOrder
context.Response.StatusCode = HttpStatusCode.OK

If oResponse.ReturnCode = "Error" Then
bolError = True
context.Response.StatusCode = HttpStatusCode.Conflict
sError.Error = oResponse.ReturnCode + ": " + oResponse.Message
Else
context.Response.StatusCode = HttpStatusCode.OK
sResponse.vendor_order_id = oResponse.Message
End If
Else

bolError = True
context.Response.StatusCode = HttpStatusCode.Unauthorized
sError.Error = "Error: Access Denied"
End If
Else
bolError = True
context.Response.StatusCode = HttpStatusCode.BadRequest
sError.Error = "Error: Invalid content type"
End If
Catch ex As Exception
bolError = True
context.Response.StatusCode = HttpStatusCode.BadRequest
Utils.ErrorEmail("Order Error!", ex, AttachmentName:="Order.xml", AttachmentData:=sData)
sError.Error = String.Format("Error", ex.Message)
End Try

context.Response.ContentType = "application/json"

If bolError Then
context.Response.Write(JsonConvert.SerializeObject(sError))
Else
context.Response.Write(JsonConvert.SerializeObject(sResponse))
End If
End Sub



Code snippet:

bolError = True
context.Response.StatusCode = HttpStatusCode.Unauthorized
sError.Error = "Error: Access Denied"

Continue reading...
 
Back
Top