I am writing a program that will submit a XML to a website. The code written works fine, but sometimes it just stops working for some reason, throwing a
System.Net.ProtocolViolationException. I can close the program and re-run - it starts working again just fine.
Here is the code that I am using:
private string Summit(string xml)
{
string result = string.Empty;
StringBuilder sb = new StringBuilder();
try {
WebRequest request = WebRequest.Create(this.targetUrl);
request.Timeout = 800 * 1000;
RequestState requestState = new RequestState(xml);
requestState.Request = request;
request.ContentType = "text/xml";
// Set the 'Method' property to 'POST' to post data to a Uri.
requestState.Request.Method = "POST";
requestState.Request.ContentType = "text/xml";
// Start the Asynchronous 'BeginGetRequestStream' method call.
IAsyncResult r = (IAsyncResult)request.BeginGetRequestStream(new AsyncCallback(ReadCallBack), requestState);
// Pause the current thread until the async operation completes.
// Console.WriteLine("main thread waiting...");
allDone.WaitOne();
// Assign the response object of 'WebRequest' to a 'WebResponse' variable.
WebResponse response = null;
try {
response =request.GetResponse();
} catch (System.Net.ProtocolViolationException ex) {
response = null;
request.Abort();
request = null;
requestState = null;
return "";
}
//Console.WriteLine("The string has been posted.");
//Console.WriteLine("Please wait for the response...");
Stream streamResponse = response.GetResponseStream();
StreamReader streamRead = new StreamReader(streamResponse);
Char[] readBuff = new Char[256];
int count = streamRead.Read(readBuff, 0, 256);
//StringBuilder sb = new StringBuilder();
while (count > 0) {
String outputData = new String(readBuff, 0, count);
sb.Append(outputData);
count = streamRead.Read(readBuff, 0, 256);
}
// Close the Stream Object.
streamResponse.Close();
streamRead.Close();
//allDone.WaitOne();
// Release the HttpWebResponse Resource.
response.Close();
//return sb.ToString();
} catch (WebException webex) {
Debug.WriteLine(webex.Message);
} catch (System.Web.Services.Protocols.SoapException soapex) {
Debug.WriteLine(soapex.Message);
} catch (System.Net.ProtocolViolationException ex) {
Debug.WriteLine(ex.Message);
} catch (Exception ex) {
Debug.WriteLine(ex.Message);
}
return sb.ToString();
}
private static void ReadCallBack(IAsyncResult asyncResult)
{
try {
RequestState myRequestState = (RequestState)asyncResult.AsyncState;
WebRequest myWebRequest2 = myRequestState.Request;
// End of the Asynchronus request.
Stream responseStream = myWebRequest2.EndGetRequestStream(asyncResult);
//Convert the string into a byte array.
ASCIIEncoding encoder = new ASCIIEncoding();
byte[] ByteArray = encoder.GetBytes(myRequestState.Xml);
// Write data to the stream.
responseStream.Write(ByteArray, 0, myRequestState.Xml.Length);
responseStream.Close();
} catch (WebException e) {
Console.WriteLine("\nReadCallBack Exception raised!");
Console.WriteLine("\nMessage:{0}", e.Message);
Console.WriteLine("\nStatus:{0}", e.Status);
}
allDone.Set();
}
response =request.GetResponse() is when it fails and gives an error
You must provide a request body if you set ContentLength>0 or
SendChunked==true. Do this by calling [Begin]GetRequestStream before
[Begin]GetResponse.
Any help would be greatly appreciated.
This gets tricky since we're doing async calls.
Do this in the following order:
request.BeginGetRequestStream(new AsyncCallback(GetRequestStreamCallback), request)
Then in 'GetRequestStreamCallback(IAsyncResult asynchronousResult)' call:
request.BeginGetResponse(new AsyncCallback(GetResponseCallback), request)
Lastly, in the GetResponse, be sure to close the stream:
response.Close();
allDone.Set();
MSDN Does a really good job explaining it: http://msdn.microsoft.com/en-us/library/system.net.httpwebrequest.begingetrequeststream.aspx
Related
Below is the link which is https and return the JSON. but the code gets stuck on the HttpWebResponse and nothing happens. It does not go in the catch block also.
public static string GetWebSource(string URL)
{
StringBuilder sb = new StringBuilder();
if (URL != "")
{
// used to build entire input
try
{
// used on each read operation
byte[] buf = new byte[8192];
// prepare the web page we will be asking for
HttpWebRequest request = (HttpWebRequest)
WebRequest.Create("https://www.nseindia.com/api/option-chain-indices?symbol=NIFTY");
request.ContentType = "application/json";
request.Method = "GET";
// execute the request
HttpWebResponse response = (HttpWebResponse)
request.GetResponse();
// we will read data via the response stream
Stream resStream = response.GetResponseStream();
string tempString = null;
int count = 0;
do
{
// fill the buffer with data
count = resStream.Read(buf, 0, buf.Length);
// make sure we read some data
if (count != 0)
{
// translate from bytes to ASCII text
tempString = Encoding.UTF8.GetString(buf, 0, count);
// continue building the string
sb.Append(tempString);
}
}
while (count > 0); // any more data to read?
}
catch (Exception ex)
{
}
}
// print out page source
return sb.ToString();
}
I'm trying use HttpWebRequest, and my BeginGetRequestStream works but it never enters the BeginGetResponse function and i have no idea why.. i've searched for a couple of hours and have not found a solution that works
public void Initialize(IScheduler scheduler)
{
if(_isCloud)
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(_cloudMappingServer + "/Mapping/GetAllCentralPoints");
request.ContentType = "application/x-www-form-urlencoded";
request.Method = "POST";
request.BeginGetRequestStream(new AsyncCallback(ReleaseReadCallback), request);
// Instruct the thread to wait until we resume it
_waitHandle.WaitOne();
_waitHandle.Dispose();
}
}
private void ReleaseReadCallback(IAsyncResult asynchronousResult)
{
try
{
HttpWebRequest httpRequest = (HttpWebRequest)asynchronousResult.AsyncState;
using (Stream postStream = httpRequest.EndGetRequestStream(asynchronousResult))
{
using (MemoryStream memStream = new MemoryStream())
{
string queryString = string.Empty;
byte[] bytes = System.Text.Encoding.UTF8.GetBytes(queryString);
memStream.Write(bytes, 0, bytes.Length);
memStream.Position = 0;
byte[] tempBuffer = new byte[memStream.Length];
memStream.Read(tempBuffer, 0, tempBuffer.Length);
postStream.Write(tempBuffer, 0, tempBuffer.Length);
}
}
httpRequest.BeginGetResponse(new AsyncCallback(ReleaseResponseCallback), httpRequest);
}
catch (Exception ex)
{
var test = ex;
}
}
private void ReleaseResponseCallback(IAsyncResult asynchronousResult)
{
HttpWebRequest responseRequest = (HttpWebRequest)asynchronousResult.AsyncState;
string responseString = string.Empty;
try
{
using (HttpWebResponse resp = (HttpWebResponse)responseRequest.EndGetResponse(asynchronousResult))
{
using (StreamReader streamRead = new StreamReader(resp.GetResponseStream()))
{
responseString = streamRead.ReadToEnd();
try
{
JsonSerializerSettings settings = new JsonSerializerSettings();
List<CentralPointViewModel> _allCentralPointViewModel = JsonConvert.DeserializeObject<List<CentralPointViewModel>>(responseString, settings);
}
catch (JsonReaderException)
{
responseString = responseString.Replace('\"'.ToString(), string.Empty);
string[] responseArray = responseString.Split(';');
}
catch (JsonSerializationException)
{
responseString = responseString.Replace('\"'.ToString(), string.Empty);
}
}
}
}
catch (Exception ex)
{
}
}
It never enters the ReleaseResponseCallback function! I am able to make my server call but the response never reaches me or I am not properly receiving it.. Any help is appreciated
I tried to scrap the page using HtmlAgilityPack.dll but some url get into the function, I got the error and I can't catch it in try-catch block. So can anyone help me out?
Error:
An unhandled exception of type 'System.StackOverflowException' occurred in HtmlAgilityPack.dll
public void HtmlLoad(string url)
{
try
{
HttpWebRequest myHttpWebRequest = null; //Declare an HTTP-specific implementation of the WebRequest class.
HttpWebResponse myHttpWebResponse = null; //Declare an HTTP-specific implementation of the WebResponse class
//Create Request //
myHttpWebRequest = (HttpWebRequest)HttpWebRequest.Create(url);
myHttpWebRequest.Method = "GET";
myHttpWebRequest.ContentType = "text/html; encoding='utf-8'";
//Get Response
myHttpWebResponse = (HttpWebResponse)myHttpWebRequest.GetResponse();
HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
Stream data = myHttpWebResponse.GetResponseStream();//client.OpenRead(url);
doc.Load(data);
data.Close();
}
catch (Exception ex) { throw ex; }
}
You can try this clean
public static async Task<int> HtmlLoadAsync(string url/*, bool addUserAgent = false*/)
{
try
{
var client = new HttpClient();
//if (addUserAgent) OPTIONAL
//{
// client.DefaultRequestHeaders.UserAgent.ParseAdd(UserAgent);
//}
//client.Timeout = TimeOut;
var response = client.GetStringAsync(url);
var urlContents = await response;
var document = new HtmlAgilityPack.HtmlDocument();
document.LoadHtml(urlContents);
// process document now
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
return 0;
}
Now call it
private async void Process()
{
await HtmlLoadAsync("http://....");
}
I am creating an app that will talk to a HTTP Listener service.
I have taken code from this Posting Posting with HttpWebRequest on Windows Phone 7
This code seems to work almost perfectly, it just doesn't work on the very first call.
The call is making it to the service and is not throwing an error. Just the return value is empty.
On the second call, I am getting the return value without problem. If I go to another page and back again, I also get the return value first time. This is only happening on the first call when the app is first started.
Any ideas would be greatly appreciated. Code Below.
SERVICE
DiagnosticLog.Write(99, DateTime.Now, "Phone Listener Called");
IAsyncResult phoneResult = PhoneListener.BeginGetContext(new AsyncCallback(Phone), PhoneListener);
HttpListenerContext context = null;
HttpListenerResponse response = null;
HttpListener _listener = (HttpListener)result.AsyncState;
context = _listener.EndGetContext(result);
HttpListenerRequest request = context.Request;
response = context.Response;
//response.ContentType = "text/plain";
string postData = string.Empty;
using (StreamReader sr = new StreamReader(request.InputStream))
{
DiagnosticLog.Write(99, DateTime.Now, "Reading Stream");
postData = sr.ReadToEnd();
}
byte[] buffer;
System.IO.Stream output = response.OutputStream;
if (postData == "islogin")
{
buffer = System.Text.Encoding.UTF8.GetBytes("logincorrect");
// Get a response stream and write the response to it.
response.ContentLength64 = buffer.Length;
output = response.OutputStream;
output.Write(buffer, 0, buffer.Length);
DiagnosticLog.Write(99, DateTime.Now, "Good Return sent");
output.Close();
}
else
{
buffer = System.Text.Encoding.UTF8.GetBytes("loginincorrect");
// Get a response stream and write the response to it.
response.ContentLength64 = buffer.Length;
output = response.OutputStream;
output.Write(buffer, 0, buffer.Length);
DiagnosticLog.Write(99, DateTime.Now, "Bad Return sent");
output.Close();
}
CODE (phone app)
WebRequest request = WebRequest.Create("http://127.0.0.1:8080/phone");
request.Method = "POST";
request.BeginGetRequestStream(ar =>
{
var requestStream = request.EndGetRequestStream(ar);
using (StreamWriter sw = new StreamWriter(requestStream))
{
sw.Write("islogin");
}
request.BeginGetResponse(a =>
{
try
{
WebResponse response = request.EndGetResponse(a);
Stream responseStream = response.GetResponseStream();
using(var sr = new StreamReader(responseStream))
{
Storage.ReturnValue = sr.ReadToEnd().ToString();
}
}
catch(WebException ex)
{
Deployment.Current.Dispatcher.BeginInvoke(() =>
{
MessageBox.Show(ex.ToString());
});
}
}, null);
}, null);
txtLoginUsername.Text = Storage.ReturnValue;
The answer is that the phone continues, so when the page updates, there is still no data present.
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);
}