In .Net how can I get a response to WebClient.Open write instead of Stream was not readable
-
It doesn't compile. Please send c# code. GetResponse is not a member of io stream and rs not defined. How should MyDataStream and rs be defined? I am not sure of the response. The doc that I have on this service is very limited and not written for .Net.
http://stackoverflow.com/questions/8222092/sending-http-post-with-system-net-webclient[^]
A guide to posting questions on CodeProject
Click this: Asking questions is a skill. Seriously, do it.
Dave Kreskowiak -
I understand that I have been coding for many years but I have never used WebClient before and do not know what to look for. I was expecting that is there is some setting or parameter that needs to be set to get the response. Do you have any explicit things to look for? Or do you have any code that does something similar that you can provide?
QuickBooksDev wrote:
I have been coding for many years
Then you should understand how to debug your code.
QuickBooksDev wrote:
I have never used WebClient before
Then the documentation is the first place to look, see http://msdn.microsoft.com/en-us/library/a3xa6ys0(v=vs.110).aspx[^].
-
QuickBooksDev wrote:
I have been coding for many years
Then you should understand how to debug your code.
QuickBooksDev wrote:
I have never used WebClient before
Then the documentation is the first place to look, see http://msdn.microsoft.com/en-us/library/a3xa6ys0(v=vs.110).aspx[^].
The doc is where I started and copied the code from their examples. I just does not show getting a response from OpenWrite. There is very little of my code to actually debug. I am not sure if I am sending the wrong data to the service or that the WebClient is coded wrong.
-
The doc is where I started and copied the code from their examples. I just does not show getting a response from OpenWrite. There is very little of my code to actually debug. I am not sure if I am sending the wrong data to the service or that the WebClient is coded wrong.
QuickBooksDev wrote:
I am not sure if I am sending the wrong data to the service
And all i can say in response, once again, is that you need to do some debugging to find out. There is no detail in your question that can help anyone here to guess what might be going wrong.
-
It also happens if I do not close it.
If postData = NIL Then
MydataStream = MyWebClient.OpenRead(address:=MySend2URL)
Else
'MydataStream
MydataStream = MyWebClient.OpenWrite(address:=MySend2URL, method:="POST")
MydataStream.Write(postArray, 0, postArray.Length)
'MydataStream.Close() ' "The remote server returned an error: (401) Unauthorized."
End If
'MySend2URL &= ":443"
Dim ThisResponse As String = NIL ' MyWebClient.Encoding.GetString(MydataStream)
Dim reader As New StreamReader(MydataStream) ' <=== Dies here with "Stream was not readable."If you don't close it the stream will still contain the data that is in the postarray, with the "current position" at the end of the stream. If you want to read it, you'd need to reset the position. Then again, why would you read from the stream the data that you just wrote to it?
Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^]
-
If you don't close it the stream will still contain the data that is in the postarray, with the "current position" at the end of the stream. If you want to read it, you'd need to reset the position. Then again, why would you read from the stream the data that you just wrote to it?
Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^]
I don't want to read from what I wrote. The data I wrote in not in the stream but in postArray. MydataStream.Write(postArray, 0, postArray.Length) From the MS example Steam = MyWebClient.OpenWrite(... It was my assumption that this was be a response like it is if it were an OpenRead (which does work). The MSDN sample doesn't show it processing a response.
-
I don't want to read from what I wrote. The data I wrote in not in the stream but in postArray. MydataStream.Write(postArray, 0, postArray.Length) From the MS example Steam = MyWebClient.OpenWrite(... It was my assumption that this was be a response like it is if it were an OpenRead (which does work). The MSDN sample doesn't show it processing a response.
-
So what are you saying by just putting in that link? That is the sample we used to do OpenReads which works when there is no postdata. Do I need to do an OpenRead after the OpenWrite? If so should the same Steam be used or another? If the same should the stream be closed after the write and before the read?
-
So what are you saying by just putting in that link? That is the sample we used to do OpenReads which works when there is no postdata. Do I need to do an OpenRead after the OpenWrite? If so should the same Steam be used or another? If the same should the stream be closed after the write and before the read?
-
I am using VB.2013. Code :
Dim MydataStream As Stream = Nothing
MydataStream = MyWebClient.OpenWrite(address:=MySend2URL, method:="POST")
Dim postArray As Byte() = Encoding.ASCII.GetBytes(postData)
MydataStream.Write(postArray, 0, postArray.Length)
MydataStream.Close()
Dim reader As New StreamReader(MydataStream) ' <=== Dies here with "Stream was not readable."How can I get a response that can be used and not the Stream is not readable error.
Found what I had to do. I had to get the target server people to trace what they were getting and tell me what was wrong. Eventually found out that I had to add the below ContentType and remove the leading "?" from the postData. None of the debugging in the .net code was of any help since without the below the postdata was being sent as 1 field and they were not returning anything if the postdata was invalid. MyWebRequest.ContentType ="application/x-wwww.form-urlencoded"