ASp.Net day

Micro blog



About Satalaj

www.satalaj.com

The best inline translator

Live lookup to see what asp.net developers are searching





unable to connect to the remote server       by Satalaj 9. November 2009 12:12
    


 Some times our web applications or windows service or windows desktop applications throws "unable to connect to the remote server".

Make sure url or IP address is correct.

solution:
Proxy server Make sure that your not using Proxy server to access internet. If you are using proxy server to access internet,
                   contact your administrator and get the network credentials, IP address and port. Add those details in you application
                   to let it use proxy server.

Anti virus:     if you are using ESET antivirus, you need to setup rules init to allow your application in / out request on xxxx port number.

                   symantech: Disable it and then try.

Application: Application is trying to access apache server url. URL maybe case sensitve use correct url. 

                  use command prompt and execute telnet command

Telnet   ip_address   port_number

and see the response.

     

httpwebrequest web-proxy       by Satalaj 25. July 2009 13:53
    

 When you are accessing internet using proxy server you must tell the client to use proxy server details.
Proxy server can be your network machine connected to Internet or any public proxy. To know how client and server communicate with each other refer
http://www.revenmerchantservices.com/post/2010/01/25/client-server-communication.aspx

Lets take an e.g. of IE. Here you can see I'm browsing internet using Proxy server.



Here I told IE to use proxy server while accessing internet. Same way you can see in Google Gtalk, Yahoo chat etc.

Look at gtalk proxy screenshot.



Any requests that goes through IE or any other client needs to be configured to use proxy.

Now, I will show you how you can tell your .Net developed application to use proxy while accessing the web.

I hope you are familiar with
HttpWebRequest and HttpWebRequest  found at System.Net name-space.

you need to create an instance of WebProxy and tell HttpWebRequest object to use this proxy settings.

        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(api);

WebProxy proxy = new WebProxy("192.168.1.100",3321);

// if your proxy server requires credentials to access it use below code
NetworkCredential credentials = new NetworkCredential("UserNAme","Password")

// notify your proxy instance about these credentials

proxy.Credentials = credentials; 

// now your proxy instance is ready to pass along with the request

request.Proxy = proxy;

  HttpWebRequest request = (HttpWebRequest)WebRequest.Create(API);

        // setting up connection with web and posting vai GET method

            HttpWebResponse response = (HttpWebResponse)request.GetResponse();

        // Returns httpwebresponse

           StreamReader reader = new StreamReader(response.GetResponseStream());

        // Read response stream

string strReponse = reader.ReadToEnd();

   
   

Satalaj

     

C# Send SMS       by Satalaj 1. June 2009 09:33
    


  You can send sms using ASP.net web application. First you hunt for good SMS provider API. 
 
 API should be capable of sending long messages more than 160 characters. 
 
It works with http Get Method. Below is the code snippet to deal with an API.

Add namespace called System.Net in your application to create objects of HttpWebRequest and HttpWebResponse

You need to setup communication between your web application and their SMS gateway.

Here are namespaces which you need to add in your asp.net web page.

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Net;


Build your URL to post the SMS vai web using http Get MEthod.


string msg =  HttpUtility.UrlEncode(txtMessage.Text);

//to encode the special characters and white spaces we need to use HttpUtility.UrlEncode

Lets post the Parameters to API.

Parameter Name: Value
1. UID:                xxx
2. PWD:               yyy
3. MSG:               Your+Encoded+message
5.MobileNumber:   91980098900
6. senderID:  SMS

Note*: sender id can be numeric or alphanumeric. It is a text, that will appear on end users handset once he receives the message.

In your case, parameters may be different. You need to read SMS Providers API document and find out what needs to be sent over HTTP get method.


 protected void btnSend_Click(object sender, EventArgs e)
 {

    //The final URL / api will be

    string API =
http://www.yoursmsprovider.com/smsAPI.php?UID=xxx&PWD=xxx&MSG=xxx+yyy+zzz&MobileNumber=9456546546;
        
       //uncomment below code, if it requires credentials to access your network via Proxy server and update appropriate fields
       //(IP address and  port )

       // WebProxy proxy = new WebProxy("192.168.1.100",3321);

       //uncomment below code if it requires credentials to access your network and update required

       //NetworkCredential credentials = new NetworkCredential("UserNAme","Password")
       //proxy.Credentials = credentials;


        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(api);

        // uncomment below line of code and tell your request to go via proxy server.

        //request.Proxy = proxy;

          HttpWebRequest request = (HttpWebRequest)WebRequest.Create(API);

        // setting up connection with web and posting vai GET method

            HttpWebResponse response = (HttpWebResponse)request.GetResponse();

        // Returns httpwebresponse

           StreamReader reader = new StreamReader(response.GetResponseStream());

        // Read response stream


           Response.Write(reader.ReadToEnd());

 }



You will get your transaction Id / or any unique code as a response. That can be used for retrieving the status of your sms, whether it is sent or not.


     

HttpWebRequest Post method       by Satalaj 27. May 2009 09:17
    


       In this post, I will Check "Yahoo online user status" using ASp.net and HttpWebRequest POST method.

 

 
Add Button control in your web page and write down below code to see yahoo user online status.
Your aspx page will look like below one

 <form id="form1" runat="server">

<div>

Status:
<asp:Label
ID="Label1" runat="server" Text="Label" BackColor="#FFFF80" ForeColor
="#0000C0">
</
asp:Label><br />

Yahoo user name:

<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>

<asp:Button ID="Button2" runat="server" OnClick="Button2_Click" Text=":) Check Online Status !" />

</div>

</form>



On click event of button, we will post the user name to Yahoo and Yahoo will response the user online status.

We will use HttpWebRequest post method and HttpWebResponse to receive the response.

Add below name space in your code  

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Net;


  

public partial class Default2 : System.Web.UI.Page

{

protected void Page_Load(object sender, EventArgs e)

{

Label1.Text = "";

}

protected void Button2_Click(object sender, EventArgs e)

{

HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://opi.yahoo.com/online");

request.Timeout = 9 * 1000; // set request timeout = 9 second

request.Method = "Post";

string postData = "u=" + TextBox1.Text + "&m=s&t=8" ;

// data to be posted using HttpWebrequest post method

// we will post parameter u , m and t

// Convert this string into stream of bytes

byte[] arrPostDAta = System.Text.Encoding.GetEncoding(1252).GetBytes(postData);

// set request content length = post data length

request.ContentLength = arrPostDAta.Length;

System.IO.Stream strmPostData = request.GetRequestStream();

// get request stream

// write post data to stream of request

strmPostData.Write(arrPostDAta, 0, arrPostDAta.Length);

strmPostData.Close();

// upload post data and Get Response from server

HttpWebResponse response = (HttpWebResponse)request.GetResponse();

StreamReader reader = new StreamReader(response.GetResponseStream());

Label1.Text = reader.ReadToEnd();

reader.Close();

response.Close();

}

}



HttpWebRequest and HttpWebResponse objects can be found under namepsace System.Net;

Label will show you yahoo online user status.

I learned HttpWebRequest and HttpWebResponse using this article
http://www.west-wind.com/presentations/dotnetWebRequest/dotnetWebRequest.htm


Satalaj

     

HttpWebRequest Timeout       by Satalaj 26. May 2009 11:36
    


You can set time to live on for connection to the server using HttpWebRequest object Timeout property
as shown in below code.

using  System.Net;

 protected void Button1_Click(object sender, EventArgs e)
{

   HttpWebRequest request = (HttpWebRequest)WebRequest.Create(http://opi.yahoo.com/online?u=my_yahooid&m=s&t=8); 

  // setting up connection with web and posting

  request.Timeout = 15 * 1000;  

  // This is timout in millisecond 1 second = 1000 milliseconds 
 
  request.ContentType = "application/x-www-form-urlencoded";  

//   you can set content type of request to  application/x-www-form-urlencoded 

  HttpWebResponse response = (HttpWebResponse)request.GetResponse();


// Returns httpwebresponse StreamReader reader = new StreamReader(response.GetResponseStream());

Response.Write(reader.ReadToEnd());

}

 

     

HttpWebRequest       by Satalaj 26. May 2009 07:54
    

  In this simple 4 lines of code we will ge the yahoo user nline status and

I will demonstrate the use of HttpWebRequest using get method.

Simple HttpWebRequest  C#

We will post parameters u=my_yahooID and Display style m=s and t=8

to yahoo server opi.yahoo.com it will tell the yahoo user online status online / offline.


using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Net;

protected void Button1_Click(object sender, EventArgs e)

{

HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://opi.yahoo.com/online?u=my_yahooid&m=s&t=8");

// setting up connection with web and posting

HttpWebResponse response = (HttpWebResponse)request.GetResponse();       // Returns httpwebresponse

StreamReader reader = new StreamReader(response.GetResponseStream());

Response.Write(reader.ReadToEnd());

}