HttpWebRequest getRequestStream hangs on multiple runs - c#

I've written some code to send and read text from a listener. This runs fine on the 1st and 2nd exchange, but on the 3rd send there's a long delay between calling GetRequestStream() and the actual writing of the data.
I've disposed the outstream on the send side, as well as the stream reader, and the input stream on the read side as recommended here: Does anyone know why I receive an HttpWebRequest Timeout?
And it still hangs on the 3rd attempt to send info. It definitely seems to be hanging at GetRequestStrean() in SendMessage():
public void SendMessage(string message)
{
HttpWebRequest request;
string sendUrl;
sendUrl = "http://" + termIPAddress + ":" + sendPort + "/";
Uri uri = new Uri(sendUrl);
Console.WriteLine("http://" + termIPAddress + ":" + sendPort + "/");
ServicePoint servicePoint = ServicePointManager.FindServicePoint(uri);
servicePoint.BindIPEndPointDelegate = new BindIPEndPoint(BindIPEndPointCallback);
servicePoint.ConnectionLeaseTimeout = 300;
request = (HttpWebRequest)WebRequest.Create(sendUrl);
request.KeepAlive = false;
request.Method = "POST";
request.ProtocolVersion = HttpVersion.Version11;
request.ContentType = "application/x-www-form-urlencoded";
request.Headers.Add("SourceIP", localIPAddress);
request.Headers.Add("MachineName", localName);
requestStarted = true;
byte[] buffer = System.Text.Encoding.UTF8.GetBytes(message);
request.ContentLength = buffer.Length;
try
{
using (Stream output = request.GetRequestStream())
{
output.Write(buffer, 0, buffer.Length);
output.Close();
request = null;
}
}
catch(WebException wE)
{
Console.WriteLine(wE.Message);
}
}
And this is the read portion :
public string GetMessage()
{
Console.WriteLine("Entering actual listener");
string s;
string sourceIP;
NameValueCollection headerList;
HttpListenerContext context = terminalListener.GetContext();
HttpListenerRequest request = context.Request;
headerList = request.Headers;
sourceIP = headerList.GetValues("SourceIP")[0];
termName = headerList.GetValues("MachineName")[0];
termIPAddress = sourceIP;
using (System.IO.Stream body = request.InputStream)
{
System.Text.Encoding encoding = request.ContentEncoding;
using (System.IO.StreamReader reader = new System.IO.StreamReader(body, encoding))
{
s = reader.ReadToEnd();
body.Close();
reader.Close();
}
}
return termName + " : " + s;
}
I also tried to add an IP End Point bind but have to be honest, I don't fully understand this piece of the code:
private IPEndPoint BindIPEndPointCallback(ServicePoint servicePoint, IPEndPoint remoteEndPoint, int retryCount)
{
int portNumber = Convert.ToInt32(sendPort);
IPEndPoint IEP = new IPEndPoint(IPAddress.Parse(localIPAddress), 0);
Console.WriteLine(Convert.ToString(IEP));
return IEP;
}

You just forgot to call HttpWebRequest.GetResponse and therefore run out of connection limit.
So, you should change your code as follows:
try
{
using (Stream output = request.GetRequestStream())
output.Write(buffer, 0, buffer.Length);
var response = request.GetResponse() as HttpWebResponse;
//TODO: check response.StatusCode, etc.
}
catch(WebException wE)
{
Console.WriteLine(wE.Message);
}
Also, in some cases you might want to adjust default connection limit:
ServicePointManager.DefaultConnectionLimit
Or use persistent connections:
HttpWebRequest.KeepAlive

Related

C# REST API Authenticate with Bearer Token

Trying to write a tool which downloads a .zip file from a server via REST API.
It works without any problems with SOAP-UI but my tool doesn't want to download any files.
Always getting this error:
Cannot send a content-body with this verb-type.
My POST-Requests work fine but GET-Requests make problems. I Think there is a problem with my Webrequest-Header.
The authentication has to look like this:
Bearer "Access Token"
Here is my code:
class RestProvider
{
protected string method;
protected string endpoint;
protected string resource;
protected string parameters;
public RestProvider(string method, string endpoint, string resource, string parameters)
{
this.method = method;
this.endpoint = endpoint;
this.resource = resource;
this.parameters = parameters;
}
public string GetResponse()
{
string resultString = string.Empty;
ASCIIEncoding enc = new ASCIIEncoding();
byte[] paramData = enc.GetBytes(parameters);
if (this.method == "post")
{
try
{
WebRequest request = WebRequest.Create(this.endpoint + this.resource);
request.Method = this.method;
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = paramData.Length;
Stream stream = request.GetRequestStream();
stream.Write(paramData, 0, paramData.Length);
stream.Close();
WebResponse response = request.GetResponse();
stream = response.GetResponseStream();
StreamReader sr = new StreamReader(stream);
resultString = sr.ReadToEnd();
sr.Close();
stream.Close();
}
catch(Exception ex)
{
resultString = "{\"errorMessages\":[\"" + ex.Message.ToString() + "\"],\"errors\":{}}";
}
}
else if(this.method == "get")
{
try
{
WebRequest request = WebRequest.Create(this.endpoint + this.resource);
request.Headers["Authorization"] = "Bearer " + Convert.ToBase64String(Encoding.Default.GetBytes(this.parameters));
request.Method = this.method;
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = paramData.Length;
Stream stream = request.GetRequestStream();
stream.Write(paramData, 0, paramData.Length);
stream.Close();
WebResponse response = request.GetResponse();
stream = response.GetResponseStream();
StreamReader sr = new StreamReader(stream);
resultString = sr.ReadToEnd();
sr.Close();
stream.Close();
}
catch (Exception ex)
{
resultString = "{\"errorMessages\":[\"" + ex.Message.ToString() + "\"],\"errors\":{}}";
}
}
return resultString;
}
}
Any ideas?

C# httpwebrequest with many request

i have list word i need to send all this word on webrequest POST method
like here :
this function to request>
private void RequesteUrl(string word)
{
// try
// {
CookieContainer WeBCookies = new CookieContainer();
HttpWebRequest url = (HttpWebRequest)WebRequest.Create("http://mbc.com/index.php");
url.Method = "POST";
url.UserAgent = "Mozilla/5.0 (Windows NT 10.0; rv:44.0) Gecko/20100101 Firefox/44.0";
url.Referer = "http://mbc.com/index.php?";
var PostData = User.Text + "&password=" + word;
var Data = Encoding.ASCII.GetBytes(PostData);
url.ContentLength = Data.Length;
url.Host = "www.mbc.com";
url.ContentType = "application/x-www-form-urlencoded";
// url.Headers.Add(HttpRequestHeader.Cookie, webBrowser1.Document.Cookie);
url.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
url.KeepAlive = true;
url.Headers.Add("Cookie: "+tryit);
using (var stream = url.GetRequestStream())
{
stream.Write(Data, 0, Data.Length);
}
HttpWebResponse WebResponse = (HttpWebResponse)url.GetResponse();
int status = (int)WebResponse.StatusCode;
Stream ST = WebResponse.GetResponseStream();
StreamReader STreader = new StreamReader(ST);
string RR = STreader.ReadToEnd();
// MessageBox.Show (status.ToString() + " " + word);
// }
// catch (Exception ex)
// {
// MessageBox.Show(ex.Message);
// }
}
and i use this to load :
foreach (string selectword in Lists)
{
RequesteUrl(selectword);
}
but foreach not complete all list , he do to two from list only !
Yes, the reason is that you are not releasing resources after your request is complete. Since HTTP/1.1 only allows two simultaneous connections per server, after two requests you will run out of available connections. That is why it hangs.
You need to close the HttpWebResponse, and also good idea to close the streamreader and stream....
using(HttpWebResponse WebResponse = (HttpWebResponse)url.GetResponse())
{
int status = (int)WebResponse.StatusCode;
using(Stream ST = WebResponse.GetResponseStream())
using(StreamReader STreader = new StreamReader(ST))
string RR = STreader.ReadToEnd();
}

How do I copy a HttpRequest to another web service?

I have two identical web services currently installed on the same PC for testing purposes. A request that is received by the first service, is suposed go to the second one as well. My intention was to do this using a HttpModule. I handle the request during application.BeginRequest.
public void AsyncForwardRequest(HttpRequest request)
{
try
{
// Prepare web request...
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(WSaddress);
req.Credentials = CredentialCache.DefaultCredentials;
req.Headers.Add("SOAPAction", request.Headers["SOAPAction"]);
req.ContentType = request.Headers["Content-Type"];
req.Accept = request.Headers["Accept"];
req.Method = request.HttpMethod;
// Send the data.
long posStream = request.InputStream.Position;
long len = request.InputStream.Length;
byte[] buff = new byte[len];
request.InputStream.Seek(0, SeekOrigin.Begin);
if (len < int.MaxValue && request.InputStream.Read(buff, 0, (int)len) > 0)
{
using (Stream stm = req.GetRequestStream())
{
stm.Write(buff, 0, (int)len);
}
request.InputStream.Position = posStream;
DebugOutputStream(request.InputStream);
request.InputStream.Seek(0, SeekOrigin.Begin);
IAsyncResult result = (IAsyncResult)req.BeginGetResponse(new AsyncCallback(RespCallback), req);
}
}
catch (Exception ex)
{
App.Error2(String.Format("RequestDuplicatorModule - BeginRequest; ERROR begin request: {0}", ex.Message), ex);
}
private static void RespCallback(IAsyncResult asynchronousResult)
{
try
{
// State of request is asynchronous.
var req = (HttpWebRequest)asynchronousResult.AsyncState;
WebResponse resp = (HttpWebResponse)req.EndGetResponse(asynchronousResult);
Stream responseStream = resp.GetResponseStream();
System.IO.Stream stream = responseStream;
Byte[] arr = new Byte[1024 * 100];
stream.Read(arr, 0, 1024 * 100);
if (arr.Length > 0)
{
string body = (new ASCIIEncoding()).GetString(arr);
Debug.Print(body);
}
}
catch (WebException ex)
{
App.Error2(String.Format("RequestDuplicatorModule - BeginRequest; ERROR begin request: {0}", ex.Message), ex);
}
}
This (HttpWebResponse)req.EndGetResponse(asynchronousResult) throws an exception: 500 Internal Server Error and fails. The original request goes through fine.
request.InnerStream:
...
Does this have anything to do with the web service addresses being different? In my case they're:
http://localhost/WS/service.asmx
http://localhost/WS_replicate/service.asmx
I would at least implement this, to be able to read the content of a httpwebrequest that has a status 500:
catch (WebException ex)
{
HttpWebResponse webResponse = (HttpWebResponse)ex.Response;
Stream dataStream = webResponse.GetResponseStream();
if (dataStream != null)
{
StreamReader reader = new StreamReader(dataStream);
response = reader.ReadToEnd();
}
}
Not answering your question though, i would suggest having a simple amsx that reads the request, and posts the request to the two different web services. If you need some code for that let me know.
Additional advantages would be that the asmx will be easier to support for the developer as well as the one dealing with the deployment. You could add configuration options to make the actual url's dynamic.
Was able to modify the AsyncForwardRequest method so that I can actually edit the SoapEnvelope itself:
string buffString = System.Text.UTF8Encoding.UTF8.GetString(buff);
using (Stream stm = req.GetRequestStream())
{
bool stmIsReadable = stm.CanRead; //false, stream is not readable, how to get
//around this?
//solution: read Byte Array into a String,
//modify <wsa:to>, write String back into a
//Buffer, write Buffer to Stream
buffString = buffString.Replace("http://localhost/WS/Service.asmx", WSaddress);
//write modded string to buff
buff = System.Text.UTF8Encoding.UTF8.GetBytes(buffString);
stm.Write(buff, 0, (int)buff.Length);
}

How to Download the File using HttpWebRequest and HttpWebResponse class(Cookies,Credentials,etc.)

Thanks icktoofay,
I tried using HttpWebRequest and HttpWebResponse.
When I request for URL by passing the Credentials like UserName And Password.
I will get the Session Id Back in the Response.
After getting that Session Id, How to Move Further.
The authenticated user are tracked using credentials/cookies.
I'm Having the Exact Url of the File to be downloaded and credentials.
If you want to use Cookies I will. I need to read the File Data and write/save it in a Specified Location.
The code I'm using is;
string username = "";
string password = "";
string reqString = "https://xxxx.com?FileNAme=asfhasf.mro" + "?" +
"username=" + username + &password=" + password;
byte[] requestData = Encoding.UTF8.GetBytes(reqString);
string s1;
CookieContainer cc = new CookieContainer();
var request = (HttpWebRequest)WebRequest.Create(loginUri);
request.Proxy = null;
request.CookieContainer = cc;
request.Method = "POST";
HttpWebResponse ws = (HttpWebResponse)request.GetResponse();
Stream str = ws.GetResponseStream();
//ws.Cookies
//var request1 = (HttpWebRequest)WebRequest.Create(loginUri);
byte[] inBuf = new byte[100000];
int bytesToRead = (int) inBuf.Length;
int bytesRead = 0;
while (bytesToRead > 0)
{
int n = str.Read(inBuf, bytesRead,bytesToRead);
if (n==0)
break;
bytesRead += n;
bytesToRead -= n;
}
FileStream fstr = new FileStream("weather.jpg", FileMode.OpenOrCreate,
FileAccess.Write);
fstr.Write(inBuf, 0, bytesRead);
str.Close();
fstr.Close();
This is how I do it:
const string baseurl = "http://www.some......thing.com/";
CookieContainer cookie;
The first method logins to web server and gets session id:
public Method1(string user, string password) {
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(baseurl);
req.Method = "POST";
req.ContentType = "application/x-www-form-urlencoded";
string login = string.Format("go=&Fuser={0}&Fpass={1}", user, password);
byte[] postbuf = Encoding.ASCII.GetBytes(login);
req.ContentLength = postbuf.Length;
Stream rs = req.GetRequestStream();
rs.Write(postbuf,0,postbuf.Length);
rs.Close();
cookie = req.CookieContainer = new CookieContainer();
WebResponse resp = req.GetResponse();
resp.Close();
}
The other method gets the file from server:
string GetPage(string path) {
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(path);
req.CookieContainer = cookie;
WebResponse resp = req.GetResponse();
string t = new StreamReader(resp.GetResponseStream(), Encoding.Default).ReadToEnd();
return IsoToWin1250(t);
}
Note that I return the page as string. You would probably better return it as bytes[] to save to disk. If your jpeg files are small (they usually are not gigabyte in size), you can simply put them to memory stream, and then save to disk. It will take some 2 or 3 simple lines in C#, and not 30 lines of tough code with potential dangerous memory leaks like you provided above.
public override DownloadItems GetItemStream(string itemID, object config = null, string downloadURL = null, string filePath = null, string)
{
DownloadItems downloadItems = new DownloadItems();
try
{
if (!string.IsNullOrEmpty(filePath))
{
using (FileStream fileStream = new System.IO.FileStream(filePath, System.IO.FileMode.OpenOrCreate, System.IO.FileAccess.Write))
{
if (!string.IsNullOrEmpty(downloadURL))
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(downloadURL);
request.Method = WebRequestMethods.Http.Get;
request.PreAuthenticate = true;
request.UseDefaultCredentials = true;
const int BUFFER_SIZE = 16 * 1024;
{
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
{
using (var responseStream = response.GetResponseStream())
{
var buffer = new byte[BUFFER_SIZE];
int bytesRead;
do
{
bytesRead = responseStream.Read(buffer, 0, BUFFER_SIZE);
fileStream.Write(buffer, 0, bytesRead);
} while (bytesRead > 0);
}
}
fileStream.Close();
downloadItems.IsSuccess = true;
}
}
else
downloadItems.IsSuccess = false;
}
}
}
catch (Exception ex)
{
downloadItems.IsSuccess = false;
downloadItems.Exception = ex;
}
return downloadItems;
}

KeepAliveException when using HttpWebRequest.GetResponse

I am trying to POST an attachment to CouchDB using the HttpWebRequest. However, when I attempt "response = (HttpWebResponse)httpWebRequest.GetResponse();" I receive a WebException with the message "The underlying connection was closed: A connection that was expected to be kept alive was closed by the server."
I have found some articles stating that setting the keepalive to false and httpversion to 1.0 resolves the situation. I am finding that it does not yeilding the exact same error, plus I do not want to take that approach as I do not want to use the 1.0 version due to how it handles the connection.
Any suggestions or ideas are welcome. I'll try them all until one works!
public ServerResponse PostAttachment(Server server, Database db, Attachment attachment)
{
Stream dataStream;
HttpWebResponse response = null;
StreamReader sr = null;
byte[] buffer;
string json;
string boundary = "----------------------------" + DateTime.Now.Ticks.ToString("x");
string headerTemplate = "Content-Disposition: form-data; name=\"_attachments\"; filename=\"" + attachment.Filename + "\"\r\n Content-Type: application/octet-stream\r\n\r\n";
byte[] headerbytes = System.Text.Encoding.UTF8.GetBytes(headerTemplate);
byte[] boundarybytes = System.Text.Encoding.ASCII.GetBytes("\r\n--" + boundary + "\r\n");
HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create("http://" + server.Host + ":" +
server.Port.ToString() + "/" + db.Name + "/" + attachment.Document.Id);
httpWebRequest.ContentType = "multipart/form-data; boundary=" + boundary;
httpWebRequest.Method = "POST";
httpWebRequest.KeepAlive = true;
httpWebRequest.ContentLength = attachment.Stream.Length + headerbytes.Length + boundarybytes.Length;
if (!string.IsNullOrEmpty(server.EncodedCredentials))
httpWebRequest.Headers.Add("Authorization", server.EncodedCredentials);
if (!attachment.Stream.CanRead)
throw new System.NotSupportedException("The stream cannot be read.");
// Get the request stream
try
{
dataStream = httpWebRequest.GetRequestStream();
}
catch (Exception e)
{
throw new WebException("Failed to get the request stream.", e);
}
buffer = new byte[server.BufferSize];
int bytesRead;
dataStream.Write(headerbytes,0,headerbytes.Length);
attachment.Stream.Position = 0;
while ((bytesRead = attachment.Stream.Read(buffer, 0, buffer.Length)) > 0)
{
dataStream.Write(buffer, 0, bytesRead);
}
dataStream.Write(boundarybytes, 0, boundarybytes.Length);
dataStream.Close();
// send the request and get the response
try
{
response = (HttpWebResponse)httpWebRequest.GetResponse();
}
catch (Exception e)
{
throw new WebException("Invalid response received from server.", e);
}
// get the server's response json
try
{
dataStream = response.GetResponseStream();
sr = new StreamReader(dataStream);
json = sr.ReadToEnd();
}
catch (Exception e)
{
throw new WebException("Failed to access the response stream.", e);
}
// close up all our streams and response
sr.Close();
dataStream.Close();
response.Close();
// Deserialize the server response
return ConvertTo.JsonToServerResponse(json);
}
After a considerable amount of research on the topic, I have decided to use PUT. While Futon uses the POST method, it is undocumented. For anyone reading this in the future, use the PUT method, it will make your life much easier.

Categories

Resources