HttpWebRequest Timeout updating status Code in BigCommerce - bigcommerce

I have read a lot, if not all the posts, on timeouts for web requests and the solutions provided have not worked. I am getting orders from Big Commerce and then updating the Big Commerce status code. I can update 2 orders and then I timeout on the third, every single time regardless of day of week or time of day.
App.Config file has:
```
<system.web>
<httpRuntime executionTimeout="180" />
</system.web>
```
Code:
```
Try
Dim strJSON As String = "{""status_id"": 9}"
Dim postBytes As Byte() = System.Text.Encoding.UTF8.GetBytes(strJSON)
For i As Integer = 0 To dto.Rows.Count - 1
strWebOrder = dto.Rows(i).Item("WebOrder")
Dim strHttp As String = "https://api.bigcommerce.com/stores/storeid/v2/orders/" & strWebOrder
Dim request As HttpWebRequest = DirectCast(WebRequest.Create(strHttp), HttpWebRequest)
request.Accept = "application/json"
request.ContentType = "application/json"
request.Headers("X-Auth-Token") = strAuthToken
request.Timeout = 10000
request.AllowWriteStreamBuffering = False
request.SendChunked = True
request.Method = "PUT"
Dim postStream As Stream = request.GetRequestStream()
postStream.Write(postBytes, 0, postBytes.Length)
Dim response As HttpWebResponse = DirectCast(request.GetResponse(), HttpWebResponse)
If response.StatusCode = 200 Then
strErrorRef = "Web " & strWebOrder
strErrorReason = "Order Status Changed to Exported"
strPutMessage = ""
WriteError()
Else
strErrorRef = "Web " & strWebOrder
strErrorReason = "Unable to change Web Order Status to Exported"
strPutMessage = ""
WriteError()
End If
Next
Catch ex As Exception
strErrorRef = "Web " & strWebOrder
strErrorReason = "Unable to change Web Order Status to Exported"
strPutMessage = (ex.ToString)
WriteError()
End Try
```

Have you tried to increase the seconds in your executionTimeout property? If you increase it to 300, this will give your application 5 minutes to execute the command before it’s shut down, rather than the 3 minutes you’re currently using.
I’m not sure what all you’ve tried or read into, but it sounds like the code you have is taking at least 2-3 minutes to get through 2 orders, so that’s why the result you see is the same regardless of the time of day it’s being executed.
Have you tried to refactor the code to break it up a bit, for example:
grab all orders with status_id 9 in one function OR
store these order_ids in an array in another function elsewhere, then in a new function loop through the stored array to update the status_id?
Also, are you using webhooks at all?

Related

Can't arrange items to the expected position

I've witten a script in vba to parse two categories from each container from a webpage. The scraper is able to parse them accordingly. The problem I'm facing at this moment is that I can't place these items across columns. If a column contains views, the next column should contains votes and so on. The way I'm expecting the result is more like:
column1 column2 column3 column4
9 views 0 vote 10 views -2
This is my script so far:
Sub CollectInfo()
Const URL As String = "https://stackoverflow.com/questions/tagged/web-scraping"
Dim Http As New XMLHTTP60, Html As New HTMLDocument
Dim post As HTMLHtmlElement, R&, C&
With Http
.Open "GET", URL, False
.send
Html.body.innerHTML = .responseText
End With
R = 1
For Each post In Html.getElementsByClassName("question-summary")
C = C + 1: Cells(R, C) = post.getElementsByClassName("views")(0).innerText
Cells(R, C + 1) = post.getElementsByClassName("votes")(0).innerText
Next post
End Sub
The way I tried is definitely leading me to the wrong placing. How can I fix it to serve the purpose? Btw, I do not wish to go for the offset (I meant Range("A1").offset(,1)") looping ;rather, I wanna stick to the way I tried above. Thanks.
This will show views and votes by turns. I changed XMLHTTP60 to MSXML2.XMLHTTP60, because on my end it causes automation error.
Sub CollectInfo()
Const URL As String = "https://stackoverflow.com/questions/tagged/web-scraping"
Dim Http As New MSXML2.XMLHTTP60, Html As New HTMLDocument
Dim post As HTMLHtmlElement, R&, C&
With Http
.Open "GET", URL, False
.send
Html.body.innerHTML = .responseText
End With
R = 1
For Each post In Html.getElementsByClassName("question-summary")
C = C + 1
Cells(R, C) = post.getElementsByClassName("views")(0).innerText
C = C + 1
Cells(R, C) = post.getElementsByClassName("votes")(0).innerText
Next post
End Sub

FileInfo returning wrong value?

Okay, so I'm working in VB.NET, manually writing error logs to log files (yes, I know, I didn't make the call). Now, if the files are over an arbitrary size, when the function goes to write out the new error data, it should start a new file with a new file name.
Here's the function:
Dim listener As New Logging.FileLogTraceListener
listener.CustomLocation = System.Configuration.ConfigurationManager.AppSettings("LogDir")
Dim loc As String = DateTime.UtcNow.Year.ToString + DateTime.UtcNow.Month.ToString + DateTime.UtcNow.Day.ToString + DateTime.UtcNow.Hour.ToString + DateTime.UtcNow.Minute.ToString
listener.BaseFileName = loc
Dim logFolder As String
Dim source As String
logFolder = ConfigurationManager.AppSettings("LogDir")
If ex.Data.Item("Source") Is Nothing Then
source = ex.Source
Else
source = ex.Data.Item("Source").ToString
End If
Dim errorFileInfo As New FileInfo(listener.FullLogFileName)
Dim errorLengthInBytes As Long = errorFileInfo.Length
If (errorLengthInBytes > CType(System.Configuration.ConfigurationManager.AppSettings("maxFileSizeInBytes"), Long)) Then
listener.BaseFileName = listener.BaseFileName + "1"
End If
Dim msg As New System.Text.StringBuilder
If String.IsNullOrEmpty(logFolder) Then logFolder = ConfigurationManager.AppSettings("LogDir")
msg.Append(vbCrLf & "Exception" & vbCrLf)
msg.Append(vbTab & String.Concat("App: AppMonitor | Time: ", Date.Now.ToString) & vbCrLf)
msg.Append(vbTab & String.Concat("Source: ", source, " | Message: ", ex.Message) & vbCrLf)
msg.Append(vbTab & "Stack: " & ex.StackTrace & vbCrLf)
listener.Write(msg.ToString())
listener.Flush()
listener.Close()
I have this executing in a loop for testing purposes, so I can see what happens when it gets (say) 10000 errors in all at once. Again, I know there are better ways to handle this systemically, but this was the code I was told to implement.
How can I reliably get the size of the log file before writing to it, as I try to do above?
Well, as with many things, the answer to this turned out to be "did you read your own code closely" with a side order of "eat something, you need to fix your blood sugar."
On review, I saw that I was always checking BaseFileName and, if it was over the arbitrary limit, appending a character and writing to that file. What I didn't do was check to see if that file or, indeed, other more recent files existed. I've solved the issue be grabbing a directory list of all the files matching the "BaseFileName*" argument in Directory.GetFiles and selecting the most recently accessed one. That ensures that the logger will always select the more current file to write to or -if necessary- use as the base-name for another appended character.
Here's that code:
Dim directoryFiles() As String = Directory.GetFiles(listener.Location.ToString(), listener.BaseFileName + "*")
Dim targetFile As String = directoryFiles(0)
For j As Integer = 1 To directoryFiles.Count - 1 Step 1
Dim targetFileInfo As New FileInfo(targetFile)
Dim compareInfo As New FileInfo(directoryFiles(j))
If (targetFileInfo.LastAccessTimeUtc < compareInfo.LastAccessTimeUtc) Then
targetFile = directoryFiles(j)
End If
Next
Dim errorFileInfo As New FileInfo(listener.Location.ToString() + targetFile)
Dim errorLengthInBytes As Long = errorFileInfo.Length

Index was outside the bounds of the array [VB.NET]

Hi i am new to VB and in the process of learning. This error occur sometimes and doesn't occur sometimes which i find it weird.
I receive the error Index was outside the bounds of the array, that points to Button30.Text = Split(newestversion, vbCrLf)(**1**)
My motive is to read line by line from a online hosted text file.
For example,
label1.text = line 1 of the text file
label2.text = line 2 of the text file
This is very much what i want.
Here is my current code (EDITED):
Dim request As System.Net.HttpWebRequest = System.Net.HttpWebRequest.Create("direct link to my online txt file")
Dim response As System.Net.HttpWebResponse = request.GetResponse
Dim sr As System.IO.StreamReader = New System.IO.StreamReader(response.GetResponseStream)
Dim stringReader As String
stringReader = sr.ReadLine()
Button10.Text = stringReader
Dim newestversion As String = sr.ReadToEnd
Dim currentversion As String = Application.ProductVersion
Dim part() As String = Split(newestversion, vbCrLf)
If part.Length < 10 Then
' not enough items in the array. You could also throw and exception or do some other stuff here
Label10.Text = "beta"
Exit Sub
End If
'updates new episode numbers on buttons
Button20.Text = part(0)
Button30.Text = part(1)
Button40.Text = part(2)
Button50.Text = part(3)
Button60.Text = part(4)
Button70.Text = part(5)
Button80.Text = part(6)
Button90.Text = part(7)
Button100.Text = part(8)
Button110.Text = part(9)
End If
Thank You!!
You split your String for line breaks. This gives you an array, having one entry for each line in the String. However, you do not check if this array holds the amount of items you expect. You could do:
Dim newestversion As String = sr.ReadToEnd
Dim currentversion As String = Application.ProductVersion
Dim part() As String = Split(newestversion, vbCrLf)
If part.Length < 10 Then
' not enough items in the array. You could also throw and exception or do some other stuff here
MsgBox(String.Format("Array only has {0} items", part.Length))
Exit Sub
End If
'updates new episode numbers on buttons
Button20.Text = part(0)
Button30.Text = part(1)
Button40.Text = part(2)
...
Edit for the updated question
If you do have a problem like this, just approach it systematically and get as much information as you can. First you have to check if you really get the data you want from the remote source. To do that, add some logging (e.g. a MsgBox(newestversion) or a real log file). Check if the data you get is what you expect. If not, there's already a problem with your request/response code, which is a completely different problem than what I provided a solution for. If newestversion is OK, check if the splitting works by printing out the part() array. Maybe the server uses a different operating system or just uses vbCr as newline and not vbCrlf. If the splitting also works, you are done.

Issue with XmlDocument

I've got an intermittent issue with a sub that goes off to read a twitter rss feed.
95% of the time, it behaves fine and without problems. The remaining 5% it gives me a 400 Bad Request error (even though the rss feed on Twitter is just fine, which suggests that the problem is at my end, not Twitter.)
Code:
Sub RetrieveStories()
'Create a new xmldocument and load the xml into it
Dim rssDoc As New XmlDocument
rssDoc.Load("http://twitter.com/statuses/user_timeline/athersgeo.rss")
'Select each item and put it into our array
Dim nodes As XmlNodeList = rssDoc.SelectNodes("rss/channel/item")
Dim i as integer = 1
divMRSS.InnerHtml = ""
Dim TweetText as string
Dim TweetURL as string
Dim UNameLen as integer = 15
For Each node As XmlNode In nodes
'Using xpath we can acess all the data we need in each node
TweetURL = node.SelectSingleNode("link").InnerText
TweetText = Mid(node.SelectSingleNode("title").InnerText,UNameLen)
TweetText = Linkify(TweetText)
TweetText = Atify(TweetText)
TweetText = Hashify(TweetText)
TweetText = "#athersgeo: " & TweetText
divMRSS.InnerHtml += "" & TweetText & "<BR>" & RelativeTime(node.SelectSingleNode("pubDate").InnerText) & "<BR><HR>"
i = i + 1
If i = 5 then
Exit For
End if
Next
End Sub
Is there something that I'm not closing/disposing of that's sucking up connections? Or have I just coded something blindingly stupid? (Which wouldn't be the first time!)
400 Bad Request is associated with HTTP. From your code it seems the only HTTP request is to load the xmlDocument rssDoc.Load("http://twitter.com/statuses/user_timeline/athersgeo.rss").
But you mention that - the rss feed on Twitter is just fine - how could you ensure that?
As suggested in this blog you could use Fiddler to explore your request going out to twitter - http://blogs.msdn.com/b/hakane/archive/2009/06/30/investigating-http-400-bad-request-errors-in-web-access.aspx

Socket.Receive is dropping the last byte of every packet

I am having trouble with a socket client application written in vb.net using Visual Studio 2005. The client connects to a C language socket server that is running on OpenVMS. The problem that I have is that when the server sends a packet, the client does not receive the last byte (of every packet!). I can dump the packets on the network and the data is all there. My current solution is to keep my socket messages short (247 bytes) and send one extra byte past the end of my data.
I would like to include more information in my messages and I cannot find a way to make this work. If I knew 100% how long the packets will be on the network, I could work around this by including an extra byte in just the right places. However, I don't want to make any assumptions about the length of the packets.
Does anyone have any suggestions about what the best solution is to this problem?
Here is a sample of my client receive code:
Private Sub ReceiveMsg()
Dim nTotalBytes As Integer
Dim nNumBytes As Integer
Dim nMsgType As Short
Dim nMsgLen As Short
Dim ind As Integer
Try
nNumBytes = -1
While (nNumBytes <> 0)
nTotalBytes = 0
RecvBuffer.Initialize()
nNumBytes = ClientSocket.Receive(RecvBuffer, nTotalBytes, 4, SocketFlags.None)
If nNumBytes > 3 Then
SyncLock ClientSocket
AppendText("")
AppendText("Message Received " & Str(nNumBytes) & " Bytes")
nTotalBytes = nTotalBytes + nNumBytes
nMsgType = BitConverter.ToInt16(RecvBuffer, 0)
nMsgLen = BitConverter.ToInt16(RecvBuffer, 2)
If nMsgLen > 8191 Then
AppendText(" Error - Message length invalid: " & Str(nMsgLen))
nMsgLen = 250
End If
While (nTotalBytes < nMsgLen And nNumBytes > 0)
nNumBytes = ClientSocket.Receive(RecvBuffer, nTotalBytes, (nMsgLen - nTotalBytes), _
SocketFlags.None)
AppendText("Message Received " & Str(nNumBytes) & " Bytes")
nTotalBytes = nTotalBytes + nNumBytes
End While
End SyncLock
AppendText("Total Bytes Received = " & Str(nTotalBytes))
AppendText("MsgLen from Message = " & Str(nMsgLen))
Select Case nMsgType
Case 1
AppendText(" Liftpos = " & System.Text.Encoding.ASCII.GetString(RecvBuffer, 4, 1))
For ind = 0 To NUM_LOCATIONS - 1
Number(ind) = System.Text.Encoding.ASCII.GetString(RecvBuffer, 5 + (ind * 12), 12)
Next
RefreshScreen()
Case Else
AppendText(" Unrecognized message type: " & Str(nMsgType))
End Select
End If
End While
Catch ex As Exception
' Tell the main thread to invoke DisconnectedUI
Dim cb As New SimpleCallback(AddressOf DisconnectedUI)
Me.Invoke(cb)
Return
End Try
This problem has been corrected. The TCP/IP server was using a WRITE function modifier that ended up setting the URG bit in the TCP/IP packets. I believe that the server mangled this and different Windows clients had a variety of problems handling these packets.
Actually, sending an extra byte does NOT fix the problem. As long as I keep my socket message under about 247 bytes, it all gets send in a single packet. Therefore sending an extra byte just makes sure that all of the data is received by the client.
If I send a longer message, then depending on the lenght of the message, one byte is lost at the end of each packet. This means that if I send a longer message, I would have to adjust for this by either making the assumption that it will always be the same or by including a tag of some kind to help me find the offset of the data in the message.
I thought that there should be some better solution.