I am using below code to send a byte array to a site. Why this code is not throwing an exception even when there is no internet connection?.Even when no connection is there I am able to get stream and able to write to it.I expect it to throw an exception at Stream postStream = request1.EndGetRequestStream(result).Anyone got any idea why its behaving like this.
private void UploadHttpFile()
{
HttpWebRequest request = WebRequest.CreateHttp(new Uri(myUrl));
request.ContentType = string.Format("multipart/form-data; boundary={0}", boundary);
request.UserAgent = "Mozilla/4.0 (Windows; U; Windows Vista;)";
request.Method = "POST";
request.UseDefaultCredentials = true;
request.BeginGetRequestStream(GetStream, request);
}
private void GetStream(IAsyncResult result)
{
try
{
HttpWebRequest request1 = (HttpWebRequest)result.AsyncState;
using (Stream postStream = request1.EndGetRequestStream(result))
{
int len = postBody.Length;
len += mainBody.Length;
len += endBody.Length;
byte[] postArray = new byte[len + 1];
Encoding.UTF8.GetBytes(postBody.ToString()).CopyTo(postArray, 0);
Encoding.UTF8.GetBytes(mainBody).CopyTo(postArray, postBody.Length);
Encoding.UTF8.GetBytes(endBody).CopyTo(postArray, postBody.Length + mainBody.Length);
postStream.Write(postArray, 0, postArray.Length);
}
}
I expect it's buffering everything until you're done writing, at which point it will be able to use the content length immediately. If you set:
request.AllowWriteStreamBuffering = false;
then I suspect it will fail at least when you write to the stream.
Btw, your calculation of the required length for postArray appears to be assuming one byte per character, which won't always be the case... and you're calling ToString on postBody which looks like it's redundant. I'm not sure why you're trying to write in a single call anyway... either you could call Write three times:
byte[] postBodyBytes = Encoding.UTF8.GetBytes(postBody);
postStream.Write(postBodyBytes, 0, postBodyBytes.Length);
// etc
or (preferrably) just use a StreamWriter:
using (Stream postStream = request1.EndGetRequestStream(result))
{
using (StreamWriter writer = new StreamWriter(postStream)
{
writer.Write(postBody);
writer.Write(mainBody);
writer.Write(endBody);
}
}
It's also unclear why you've added 1 to the required length when initializing postArray. Are you trying to send an extra "0" byte at the end of the data?
Related
I am creating an android app that connects to my website's api using C# and xamarin. After debugging for a while I realised that when I set the ContentLength the apps seams to hang and then throws an TIMEOUT exception.
I have tried to not set the ContentLength but then the body seams to not send with the request.
public void Post(object data, string route){
string JSON = JsonConvert.SerializeObject(data);
var web = (HttpWebRequest)WebRequest.Create("http://httpbin.org/post");
//web.ContentLenfth = JSON.length;
web.ContentType = "application/json";
web.Method = "POST";
try{
var sw = new StreamWriter(webRequest.GetRequestStream());
sw.Write(JSON);
var webResponse = (HttpWebResponse)webRequest.GetResponse();
var sr = new StreamReader(webResponse.GetResponseStream());
var result = sr.ReadToEnd();
...
}
...
}
If ContentLengt is set the app hangs until the timeout function is called
else the test-url I am posting to tells me that I did not send a body
What do I have to do in order to send a successful POST request ?
You should set the length to be the length of the byte array you are sending (not the length of the string)
You can get the byte array from the json string by doing:
var bytes = Encoding.UTF8.GetBytes(JSON);
Then you can set the content length:
web.ContentLength = bytes.length;
And send the bytes:
using (var requestStream = web.GetRequestStream())
{
requestStream.Write(bytes, 0, bytes.Length);
}
I have read other similar questions and tried the solutions from those questions, but since that did not work, hence I am posting this here.
When I send POST request below, it fails with the following error message:
System.Net.ProtocolViolationException: You must write ContentLength bytes to the request stream before calling [Begin]GetResponse.
at System.Net.HttpWebRequest.GetResponse()
....
....
My GET requests for other URL end points are working fine, I am only having this issue while issuing a POST request. Also, I have already set the ContentLength in the code appropriately. I am still unable to send the POST request. Thoughts?
public void TestSubmitJobWithParams1()
{
const string RestActionPath = "URL_GOES_HERE";
// if you have multipe parameters seperate them with teh '&' delimeter.
var postData = HttpUtility.UrlEncode("MaxNumberOfRowsPerSFSTask") + "=" + HttpUtility.UrlEncode("3000");
var request = (HttpWebRequest)WebRequest.Create(RestActionPath);
request.Method = "POST";
request.Credentials = CredentialCache.DefaultCredentials;
request.PreAuthenticate = true;
request.ContentLength = 0;
request.Timeout = 150000;
request.CachePolicy = new RequestCachePolicy(RequestCacheLevel.BypassCache);
request.ContentType = "application/x-www-form-urlencoded";
byte[] bytes = Encoding.ASCII.GetBytes(postData);
request.ContentLength = bytes.Length;
Stream newStream = request.GetRequestStream();
newStream.Write(bytes, 0, bytes.Length);
string output = string.Empty;
try
{
using (var response = request.GetResponse())
{
using (var stream = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding(1252)))
{
output = stream.ReadToEnd();
}
}
}
catch (WebException ex)
{
if (ex.Status == WebExceptionStatus.ProtocolError)
{
using (var stream = new StreamReader(ex.Response.GetResponseStream()))
{
output = stream.ReadToEnd();
}
}
else if (ex.Status == WebExceptionStatus.Timeout)
{
output = "Request timeout is expired.";
}
}
catch (ProtocolViolationException e)
{
Console.WriteLine(e);
}
Console.WriteLine(output);
Console.ReadLine();
}
A few things:
Firstly, you don't need to set ContentLength directly - just leave it out (defaults to -1). You're actually calling it twice, so remove both calls.
Also, you need to call Close() on the stream before calling GetResponse()
Stream newStream = request.GetRequestStream();
newStream.Write(bytes, 0, bytes.Length);
newStream.Close();
Alternatively, you could have it within a using statement, which handles the closing and disposal for you):
using (var newStream = request.GetRequestStream())
{
newStream.Write(bytes, 0, bytes.Length);
}
You also don't technically need to use HttpUtility.UrlEncode() for the postData since there's nothing in your string that would violate a Url's integrity. Just do:
string postData = "MaxNumberOfRowsPerSFSTask=3000");
Let me know if that solves it for you.
For a more thorough rundown, check this out: http://msdn.microsoft.com/en-us/library/system.net.httpwebrequest.getresponse.aspx
Specifically, the section about ProtocolViolationException and also where it says:
When using the POST method, you must get the request stream, write the data to be posted, and close the stream. This method blocks waiting for content to post; if there is no time-out set and you do not provide content, the calling thread blocks indefinitely.
I am running a server with a mysql database on it.
I am now making a C# program which has to put some data into the database.
For security reasons I would send the data to a php script which inserts the data in the local mysql database.
I am trying the code below, but when I use fiddler to check if the url is called, it doesn't show up, so it seems as if the url is never called.
My code looks like this:
string result = string.Empty;
string data2 = string.Empty;
string[] postdata = new string[8];
postdata[0] = "Date";
postdata[1] = log.EndTime.ToString();
postdata[2] = "Name";
postdata[3] = log.OwnerTask.Schedule.Name;
postdata[4] = "Status";
postdata[5] = log.ParsedStatus;
postdata[6] = "Message";
postdata[7] = log.ParsedMessage;
string Url = "http://x.x.x.x/send.php";
System.Text.ASCIIEncoding ascii = new ASCIIEncoding();
for (int i = 0; i < postdata.Length; i += 2)
{
data2 += string.Format("&{0}={1}", postdata[i], postdata[i + 1]);
}
data2 = data2.Remove(0, 1);
byte[] bytesarr = ascii.GetBytes(data2);
try
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url);
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = bytesarr.Length;
System.IO.Stream streamwriter = request.GetRequestStream();
streamwriter.Write(bytesarr, 0, bytesarr.Length);
streamwriter.Close();
}
Can somebody help me out? Point me in the right direction?
Thanks
More focused answer, based on the spot-on comment provided
After writing to the stream, you should invoke GetResponseStream to actually perform the request:
System.IO.Stream streamwriter = request.GetRequestStream();
streamwriter.Write(bytesarr, 0, bytesarr.Length);
streamwriter.Close();
var response = request.GetResponseStream(); // this will execute the request
// [go on ...]
}
Old answer, spotted the issue but missed the purpose of OP code, left for reference
I'm not able at the moment to verify it in VS but this
System.IO.Stream streamwriter = request.GetRequestStream();
^^^^^^^
should be
System.IO.Stream streamwriter = request.GetResponseStream();
^^^^^^^^
as in: you want to parse the response, not the request (which your code never executes, that's why you don't see the URL being hit).
Rather than add on to my question here, I'm adding a new one as, once I looked at my code with my X-Ray vision goggles attached, I don't grok it.
I don't even remember where I got this code, but it's an adaptation of an example I found somewhere. Yet it doesn't seem that the data is even being sent to the server. To be specific, this code:
public static string SendXMLFile(string xmlFilepath, string uri)
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);
request.KeepAlive = false;
request.ProtocolVersion = HttpVersion.Version10;
request.Method = "POST";
StringBuilder sb = new StringBuilder();
using (StreamReader sr = new StreamReader(xmlFilepath))
{
String line;
while ((line = sr.ReadLine()) != null)
{
// test to see if it's finding any lines
//MessageBox.Show(line); <= works fine
sb.AppendLine(line);
}
byte[] postBytes = Encoding.UTF8.GetBytes(sb.ToString());
request.ContentLength = postBytes.Length;
// Did the sb get into the byte array?
//MessageBox.Show(request.ContentLength.ToString()); <= shows "112" (seems right)
request.KeepAlive = false;
request.ContentType = "application/xml";
try
{
Stream requestStream = request.GetRequestStream();
// now test this: MessageBox.Show() below causing exception? See https://stackoverflow.com/questions/22358231/why-is-the-httpwebrequest-body-val-null-after-crossing-the-rubicon
//MessageBox.Show(string.Format("requestStream length is {0}", requestStream.Length.ToString()));
requestStream.Write(postBytes, 0, postBytes.Length);
MessageBox.Show(string.Format("requestStream length is {0}", requestStream.Length.ToString()));
requestStream.Close();
using (var response = (HttpWebResponse)request.GetResponse())
{
return response.ToString();
}
}
catch (Exception ex)
{
MessageBox.Show("SendXMLFile exception " + ex.Message);
request.Abort();
return string.Empty;
}
}
}
...seems to be doing this:
0) Reads the contents of the file at xmlFilepath and puts it into a StreamReader ("sr")
1) A StringBuilder ("sb") is populated with the contents of the StreamReader
2) The contents of the StringBuilder are put into an array of Bytes ("postBytes")
- then here comes the weird part (or so it seems to me, after analyzing the code more closely):
3) The contents of the array of bytes are written to a Stream ("requestStream")
4) The Stream is closed (???)
5) The HttpWebRequest ("request") attempts to return a HttpWebResponse by calling GetResponse()
but what does "request" contain? The contents (of the StreamReader => StringBuilder => Array of Bytes => Stream) are never assigned to it! It seems as if the Stream exists for the sole purpose of writing lines of code, heating up the processor, and slowing down the operation!
Is this code nonsensical, or am I just not grokking it?
GetRequestStream() returns a stream that forwards writes through the HttpWebRequest to the network.
Your code is unnecessarily long, but correct.
However, response.ToString() is wrong; you want to read response.GetResponseStream() with a StreamReader.
how can i upload a large string (in my case XML with BLOB) with POST without getting Timeout with GetResponse?
Changing the timeout helps, but this isn't really a solution.
If the Server is really death or the POST was interrupted i have to wait for the extrem large timeout.
Any Idea?
HttpWebRequest webRequest = null;
string response = "";
byte[] bytes = Encoding.UTF8.GetBytes(xml);
try
{
webRequest = (HttpWebRequest)WebRequest.Create("http://" + this.host + ":" + this.port);
webRequest.ContentType = "application/x-www-form-urlencoded";
webRequest.Method = "POST";
webRequest.Timeout = 5000;
webRequest.ContentLength = bytes.Length;
using (Stream requeststream = webRequest.GetRequestStream())
{
requeststream.Write(bytes, 0, bytes.Length);
requeststream.Close();
}
using (HttpWebResponse webResponse = (HttpWebResponse)webRequest.GetResponse())
{
using (StreamReader sr = new StreamReader(webResponse.GetResponseStream()))
{
response = sr.ReadToEnd().Trim();
sr.Close();
}
webResponse.Close();
}
}
catch(Exception ex)
{
MessageBox.Show(ex.ToString());
}
return response;
Yes, this is pretty much expected http behaviour.
Options:
have a large timeout (you've already done this), and accept that it could take a long time to legitimately time out (as opposed to taking a while because of bandwidth)
maybe you can apply gzip on the request (and tell the server you're sending it compressed); I honestly don't know if this is supported automatically, but it could certainly be done by the api explicitly checking for a particular header and applying gzip decompression on the payload
change the api to perform a number of small uploads, and a completion message
live with it