Call a url from C# Windows Application - c#

I have a url which sends sms to the provided mobile numbers. I want to just call this url from windows application but not to open in browser. I just want to execute that url from my windows application.
example of url
http://mycompany.com/sms.aspx?mobilenum=9123444777&Message=this is a test message
I have tried in this way. But it is opening in a browser.
I dont want this to be opened in a browser. I just want to execute this line internally.
System.Diagnostics.ProcessStartInfo sInfo = new
System.Diagnostics.ProcessStartInfo("http://mycompany.com
/sms.aspx?mobilenum=9123444777&Message=this is a test message");
System.Diagnostics.Process.Start(sInfo);
Thanks.

Have you tried to use a HttpWebRequest?
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(#"http://mycompany.com/sms.aspx?mobilenum=9123444777&Message=this%20is%20a%20test%20message");
WebResponse response = request.GetResponse();
response.Close();

You can't "execute" url like that... URL is an address of a resource that you can use only by sending a request to the remote server. So instead you want to post data using a query string (make an HTTP request). You can achieve that for example by using WebClient class...
http://msdn.microsoft.com/en-us/library/system.net.webclient%28v=vs.110%29.aspx

Related

How do I create simple web proxy server using C#?

I have requirement of having proxy server where request from browser will be forwarded to my custom proxy server. Afterwards Proxy server will read request from client and talk to requested resource using some thing like this --
HttpWebRequest request =(HttpWebRequest)WebRequest.Create ("http://www.google.com");
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
then once I have response back in response object write returned HTML to browser , at this point I am stuck and do not know how to write information to browser ?
Can someone please help ???

Why Network Packet Analyzer not capturing Http Post Request through Web Client

I am using upload file method of web client to post transaction to a particular URL. I am using wireshark to capture this network communication, but surprisingly wireshark is not showing this requested URL in my UAT System. As it is a HTTP Request I have filtered it with particular Ip address and Http and it is showing all other request which I am doing through my web browser but not particular request which I am doing through my webclient class.
Sample Code :-
string url = ConfigurationSettings.AppSettings["https_url"];
WebClient wc = new WebClient();
wc.QueryString = q;
string ResultString = string.Empty;
byte[] postBytes = wc.UploadFile(url, "POST", strFileName);
If I am understanding your code correctly, your are uploading your file via HTTPS? If that is the case, your traffic will only show up as SSL(which will not be filtered as HTTP) unless you configure your wireshark to decrypt SSL traffic correctly.
Some information: http://wiki.wireshark.org/SSL
If you don't want to deal with setting up wireshark to decrypt HTTPS traffic, I recommend tweaking your url to send a POST via http, which will then show up in your wireshark sniff.

HTML message or login pages intercepting web service requests

I am busy working on a mobile application that retrieves data from a web service.
Ofcourse everything is working perfectly, I get everything I need to and I can consume the services without much effort... on the emulator.
However, when I move over to testing this application on the device, instead of getting back the data that I am expecting I am getting a website returned. How am I supposed to handle this?
Currently I am using this to call my service: (using system.net - I dont know if this is what I should be using on windows phone 7 either)
WebClient proxy = new WebClient();
string strURI = "http://www.google.co.za";
proxy.OpenReadCompleted +=
new OpenReadCompletedEventHandler(proxy_openreadcompleted);
proxy.OpenReadAsync(new Uri(strURI));
Please note: I am not really calling google, it is just an example. So anyways I am expecting my JSON to be returned, instead I am getting a message from the service provider to change mobile options... I can put this into isolated storage and render it with a browser, however I do not know what the source of the message is so when you click on a button, the forms use relative URLs, so instead of it doing what it is intened to do I just see what it is trying to call.
Is there anyway to get the source of the response? I am looking for a source like http://vodafonelive.mobi/ or something like that. If someone can tell me what to do I would greatly appreciate it, my current thinking is that if I can identify the source I can create a webbrowser task so that my application does not need to handle this, however... since I am calling a specific source I don't know how to identify where the response is comming from.
Any help is appreciated.
This is most likely due to differences in the user agent of the emulator and the device. Check what is sent or set this explicitly to ensure that the 2 behave in the same way. To ensure the server doesn't try and redirect to a different location.
Alternatively, it could be a mobile operator proxy being "helpful" and adjusting the request that goes over their network.
Ok so after spending some time on this, I finally found a way that I could get a response uri (where the response was comming from) by using another method to do the call to the service:
So basically this is the call:
WebRequest request = HttpWebRequest.Create(strURI);
var result = (IAsyncResult)request.BeginGetResponse(ResponseCallback, request);
So in the function ResponseCallBack function I do something like this:
WebRequest request = (HttpWebRequest)result.AsyncState;
WebResponse response = request.EndGetResponse(result);
which then allows me to check the response uri (the source of the intercepted message) and have the native browser handle the html that I was not expecting.
WebBrowserTask webBrowserTask = new WebBrowserTask();
webBrowserTask.Uri = response.ResponseUri;
Thanks for the help though, hopefully this will help someone with a similar issue.

Http Post with Partial URL in C#.NET

I have a web application (which I have no control over) I need to send HTTP post programatically to. Currently I've using HttpWebRequest like
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://someserver.com/blah/blah.aspx");
However the application was returning a "Unknown Server Error (not the IIS error, a custom application error page)" when posting to data. Using Fiddler to compare my Post vs IE post I can see the only difference is in the POST line of the request:
In Internet Explorer Fiddler (RAW view) shows traffic
POST /blah/blah.aspx HTTP/1.1
In my C# program fiddler (RAW view) records traffic as
POST https://someserver.com/blah/blah.aspx HTTP/1.1
This is only difference from both both requests.
From what I've researched so far it seems there is no way to make HttpWebRequest.Create post the relative URL.Note: I see many posts on "how to use relative URLs" but these suggestions do not work, as the actual post is still done using an absolute URL (when you sniff the HTTP traffic)
What is simplest way to accomplish this post with relative URL?
(Traffic is NOT going through a proxy)
Update: For the time being I'm using IE automation to do scheduled perf test, instead of method above. I might look at another scripting language as I did want to test without any browser.
No, you can't do POST without server in a Url.
One possible reason your program fails is if it does not use correct proxy and as result can't resolve server name.
Note: Fiddler shows path and host separately in the view you are talking about.
Configure you program to use Fiddler as proxy (127.0.0.1:8888) and compare requests that you are making with browser's ones. Don't forget to switch Fiddler to "show all proceses".
Here is article on configuring Fiddler for different type of environment including C# code: Fiddler: Configuring clients
objRequest = (HttpWebRequest)WebRequest.Create(url);
objRequest.Proxy= new WebProxy("127.0.0.1", 8888);

WCF REST hosted in IIS 6 , not able to consume it on client

I have hosted my WCF REST Service in IIS6. But when I try to consume any method, it gives me an error 400.
However when I use the same url in IE, the desired response is achieved. How come I can't consume it in my client while I can hit it directly in IE.
Following is the Client code :
string xmlInputValue = XMLUtility<string>.GetDataContractXml("Testing", null);
WebClient wc = new WebClient();
wc.Headers["Content-Type"] = "application/octet-stream";
xmlInputValue = string.Empty;
byte[] buf = new byte[0x10000];
wc.UploadString(new Uri(#"http://localhost/FileUpload/UploadData/PingTest/?123"), "POST", "4567");
Got the answer. While using IIS for hosting the service, endpoint address should be blank
One quick question, why are you setting Content-type as "application/octet-stream"? are you sure service binding is expecting the same content-type. Anyways, I'd capture Fiddler trace (using fiddler tool: http://fiddler2.com) for IE (success scenario) and check how request is sent by IE which is accepted by service and compare with request sent by client application.
HTH,
Amit

Categories

Resources